package com.ssj.dubbo.config;

import java.lang.management.ManagementFactory;
import java.util.Set;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.dubbo.config.RegistryConfig;

@Configuration
public class DubboCustomConfig {

    @Value("${dubbo.application.name}")
    private String app_name;


    @Autowired
    private RegistryConfig registryConfig;

    @Bean
    public RegistryConfig registryConfig() throws Exception {
        String path="/www/data/dubbo/";
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
        Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        String  serverPort = objectNames.iterator().next().getKeyProperty("port");
        registryConfig.setFile(path+app_name+"_"+serverPort+".cache");
        return registryConfig;
    }

}
