Search in sources :

Example 1 with NacosServiceManager

use of com.alibaba.cloud.nacos.NacosServiceManager in project spring-cloud-alibaba by alibaba.

the class NacosServiceDiscoveryTest method testGetServices.

@Test
public void testGetServices() throws NacosException {
    ListView<String> nacosServices = new ListView<>();
    nacosServices.setData(new LinkedList<>());
    nacosServices.getData().add(serviceName + "1");
    nacosServices.getData().add(serviceName + "2");
    nacosServices.getData().add(serviceName + "3");
    NacosDiscoveryProperties nacosDiscoveryProperties = mock(NacosDiscoveryProperties.class);
    NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
    NamingService namingService = mock(NamingService.class);
    when(nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties())).thenReturn(namingService);
    when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
    when(namingService.getServicesOfServer(eq(1), eq(Integer.MAX_VALUE), eq("DEFAULT"))).thenReturn(nacosServices);
    NacosServiceDiscovery serviceDiscovery = new NacosServiceDiscovery(nacosDiscoveryProperties, nacosServiceManager);
    List<String> services = serviceDiscovery.getServices();
    assertThat(services.size()).isEqualTo(3);
    assertThat(services.contains(serviceName + "1"));
    assertThat(services.contains(serviceName + "2"));
    assertThat(services.contains(serviceName + "3"));
}
Also used : ListView(com.alibaba.nacos.api.naming.pojo.ListView) NacosServiceManager(com.alibaba.cloud.nacos.NacosServiceManager) NamingService(com.alibaba.nacos.api.naming.NamingService) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) Test(org.junit.jupiter.api.Test)

Example 2 with NacosServiceManager

use of com.alibaba.cloud.nacos.NacosServiceManager 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)

Example 3 with NacosServiceManager

use of com.alibaba.cloud.nacos.NacosServiceManager in project spring-cloud-alibaba by alibaba.

the class NacosServiceDiscoveryTest method testGetInstances.

@Test
public void testGetInstances() throws NacosException {
    ArrayList<Instance> instances = new ArrayList<>();
    HashMap<String, String> map = new HashMap<>();
    map.put("test-key", "test-value");
    map.put("secure", "true");
    instances.add(serviceInstance(serviceName, true, host, port, map));
    NacosDiscoveryProperties nacosDiscoveryProperties = mock(NacosDiscoveryProperties.class);
    NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
    NamingService namingService = mock(NamingService.class);
    when(nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties())).thenReturn(namingService);
    when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
    when(namingService.selectInstances(eq(serviceName), eq("DEFAULT"), eq(true))).thenReturn(instances);
    NacosServiceDiscovery serviceDiscovery = new NacosServiceDiscovery(nacosDiscoveryProperties, nacosServiceManager);
    List<ServiceInstance> serviceInstances = serviceDiscovery.getInstances(serviceName);
    assertThat(serviceInstances.size()).isEqualTo(1);
    ServiceInstance serviceInstance = serviceInstances.get(0);
    assertThat(serviceInstance.getServiceId()).isEqualTo(serviceName);
    assertThat(serviceInstance.getHost()).isEqualTo(host);
    assertThat(serviceInstance.getPort()).isEqualTo(port);
    assertThat(serviceInstance.isSecure()).isEqualTo(true);
    assertThat(serviceInstance.getUri().toString()).isEqualTo(getUri(serviceInstance));
    assertThat(serviceInstance.getMetadata().get("test-key")).isEqualTo("test-value");
}
Also used : NacosServiceManager(com.alibaba.cloud.nacos.NacosServiceManager) NacosMockTest.serviceInstance(com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) HashMap(java.util.HashMap) NamingService(com.alibaba.nacos.api.naming.NamingService) ArrayList(java.util.ArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) Test(org.junit.jupiter.api.Test)

Aggregations

NacosServiceManager (com.alibaba.cloud.nacos.NacosServiceManager)3 NamingService (com.alibaba.nacos.api.naming.NamingService)3 NacosDiscoveryProperties (com.alibaba.cloud.nacos.NacosDiscoveryProperties)2 Test (org.junit.jupiter.api.Test)2 NacosRegistration (com.alibaba.cloud.nacos.registry.NacosRegistration)1 NacosServiceRegistry (com.alibaba.cloud.nacos.registry.NacosServiceRegistry)1 NacosMockTest.serviceInstance (com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 ListView (com.alibaba.nacos.api.naming.pojo.ListView)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 Environment (org.springframework.core.env.Environment)1