Search in sources :

Example 1 with NacosServiceRegistry

use of com.alibaba.cloud.nacos.registry.NacosServiceRegistry in project fizz-gateway-community by wehotel.

the class FizzNacosHelper method getServiceRegistration.

public static FizzNacosServiceRegistration getServiceRegistration(ApplicationContext applicationContext, Properties nacosProperties) {
    Properties ps = new Properties();
    for (String propertyName : nacosProperties.stringPropertyNames()) {
        String propertyValue = nacosProperties.getProperty(propertyName);
        if (propertyName.endsWith(PropertyKeyConst.USERNAME)) {
            ps.setProperty(PropertyKeyConst.USERNAME, propertyValue);
        } else if (propertyName.endsWith(PropertyKeyConst.PASSWORD)) {
            ps.setProperty(PropertyKeyConst.PASSWORD, propertyValue);
        } else {
            String pn = propertyName.substring(ndl);
            if (pn.indexOf(Consts.S.DASH) > -1) {
                pn = PropertiesUtils.normalize(pn);
            }
            ps.setProperty(pn, propertyValue);
        }
    }
    FizzNacosProperties fizzNacosProperties = new FizzNacosProperties(ps);
    PropertiesUtils.setBeanPropertyValue(fizzNacosProperties, ps);
    fizzNacosProperties.setApplicationContext(applicationContext);
    if (fizzNacosProperties.getId() == null) {
        String id = fizzNacosProperties.getServerAddr();
        String namespace = fizzNacosProperties.getNamespace();
        if (StringUtils.isNotBlank(namespace)) {
            id = id + '_' + namespace;
        }
        String group = fizzNacosProperties.getGroup();
        if (StringUtils.isNotBlank(group)) {
            id = id + '_' + group;
        }
        fizzNacosProperties.setId(id);
    }
    Environment env = applicationContext.getEnvironment();
    if (fizzNacosProperties.getService() == null) {
        fizzNacosProperties.setService(env.getProperty("spring.application.name"));
    }
    String ip = fizzNacosProperties.getIp();
    if (StringUtils.isBlank(ip)) {
        ip = System.getProperty("nacos.discovery.ip");
        if (StringUtils.isBlank(ip)) {
            ip = System.getenv("nacos.discovery.ip");
        }
        if (StringUtils.isNotBlank(ip)) {
            fizzNacosProperties.setIp(ip);
        }
    }
    if (fizzNacosProperties.getPort() == -1) {
        fizzNacosProperties.setPort(Integer.parseInt(env.getProperty("server.port")));
    }
    fizzNacosProperties.setNamingLoadCacheAtStart("false");
    fizzNacosProperties.init();
    NacosServiceManager nacosServiceManager = new NacosServiceManager();
    NacosServiceRegistry nacosServiceRegistry = new NacosServiceRegistry(nacosServiceManager, fizzNacosProperties);
    // ReflectionUtils.set(nacosServiceRegistry, "nacosServiceManager", nacosServiceManager);
    NacosRegistration nacosRegistration = new NacosRegistration(null, fizzNacosProperties, applicationContext);
    Properties nps = fizzNacosProperties.getNacosProperties();
    NamingService namingService = nacosServiceManager.getNamingService(nps);
    return new FizzNacosServiceRegistration(fizzNacosProperties.getId(), nacosRegistration, nacosServiceRegistry, namingService);
}
Also used : NacosServiceManager(com.alibaba.cloud.nacos.NacosServiceManager) NamingService(com.alibaba.nacos.api.naming.NamingService) Environment(org.springframework.core.env.Environment) NacosRegistration(com.alibaba.cloud.nacos.registry.NacosRegistration) Properties(java.util.Properties) NacosServiceRegistry(com.alibaba.cloud.nacos.registry.NacosServiceRegistry)

Aggregations

NacosServiceManager (com.alibaba.cloud.nacos.NacosServiceManager)1 NacosRegistration (com.alibaba.cloud.nacos.registry.NacosRegistration)1 NacosServiceRegistry (com.alibaba.cloud.nacos.registry.NacosServiceRegistry)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 Properties (java.util.Properties)1 Environment (org.springframework.core.env.Environment)1