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"));
}
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);
}
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");
}
Aggregations