use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project spring-cloud-alibaba by alibaba.
the class NacosDiscoveryEndpoint method nacosDiscovery.
/**
* @return nacos discovery endpoint
*/
@ReadOperation
public Map<String, Object> nacosDiscovery() {
Map<String, Object> result = new HashMap<>();
result.put("NacosDiscoveryProperties", nacosDiscoveryProperties);
NamingService namingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());
List<ServiceInfo> subscribe = Collections.emptyList();
try {
subscribe = namingService.getSubscribeServices();
for (ServiceInfo serviceInfo : subscribe) {
List<Instance> instances = namingService.getAllInstances(serviceInfo.getName(), serviceInfo.getGroupName());
serviceInfo.setHosts(instances);
}
} catch (Exception e) {
log.error("get subscribe services from nacos fail,", e);
}
result.put("subscribe", subscribe);
return result;
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class DiskCacheTest method setUp.
@Before
public void setUp() {
System.out.println(CACHE_DIR);
serviceInfo = new ServiceInfo("G@@testName", "testClusters");
instance = new Instance();
instance.setClusterName("testClusters");
instance.setIp("1.1.1.1");
instance.setPort(1234);
instance.setServiceName("testName");
instance.addMetadata("chinese", "中文");
serviceInfo.setHosts(Collections.singletonList(instance));
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class ServiceInfoHolderTest method testProcessServiceInfoWithPushEmpty.
@Test
public void testProcessServiceInfoWithPushEmpty() {
ServiceInfo oldInfo = new ServiceInfo("a@@b@@c");
Instance instance1 = createInstance("1.1.1.1", 1);
Instance instance2 = createInstance("1.1.1.2", 2);
List<Instance> hosts = new ArrayList<>();
hosts.add(instance1);
hosts.add(instance2);
oldInfo.setHosts(hosts);
Properties prop = new Properties();
prop.setProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION, "true");
ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
holder.processServiceInfo(oldInfo);
ServiceInfo newInfo = new ServiceInfo("a@@b@@c");
final ServiceInfo actual = holder.processServiceInfo(newInfo);
Assert.assertEquals(oldInfo.getKey(), actual.getKey());
Assert.assertEquals(2, actual.getHosts().size());
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class ServiceInfoHolderTest method testProcessServiceInfo.
@Test
public void testProcessServiceInfo() {
ServiceInfo info = new ServiceInfo("a@@b@@c");
Instance instance1 = createInstance("1.1.1.1", 1);
Instance instance2 = createInstance("1.1.1.2", 2);
List<Instance> hosts = new ArrayList<>();
hosts.add(instance1);
hosts.add(instance2);
info.setHosts(hosts);
Properties prop = new Properties();
ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
ServiceInfo actual1 = holder.processServiceInfo(info);
Assert.assertEquals(info, actual1);
Instance newInstance1 = createInstance("1.1.1.1", 1);
newInstance1.setWeight(2.0);
Instance instance3 = createInstance("1.1.1.3", 3);
List<Instance> hosts2 = new ArrayList<>();
hosts2.add(newInstance1);
hosts2.add(instance3);
ServiceInfo info2 = new ServiceInfo("a@@b@@c");
info2.setHosts(hosts2);
ServiceInfo actual2 = holder.processServiceInfo(info2);
Assert.assertEquals(info2, actual2);
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class ServiceInfoHolderTest method testGetServiceInfo.
@Test
public void testGetServiceInfo() {
ServiceInfo info = new ServiceInfo("a@@b@@c");
Instance instance1 = createInstance("1.1.1.1", 1);
List<Instance> hosts = new ArrayList<>();
hosts.add(instance1);
info.setHosts(hosts);
Properties prop = new Properties();
ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
ServiceInfo expect = holder.processServiceInfo(info);
String serviceName = "b";
String groupName = "a";
String clusters = "c";
ServiceInfo actual = holder.getServiceInfo(serviceName, groupName, clusters);
Assert.assertEquals(expect.getKey(), actual.getKey());
Assert.assertEquals(expect.getHosts().size(), actual.getHosts().size());
Assert.assertEquals(expect.getHosts().get(0), actual.getHosts().get(0));
}
Aggregations