use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class NacosNamingService method selectInstances.
@Override
public List<Instance> selectInstances(String serviceName, String groupName, List<String> clusters, boolean healthy, boolean subscribe) throws NacosException {
ServiceInfo serviceInfo;
String clusterString = StringUtils.join(clusters, ",");
if (subscribe) {
serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString);
if (null == serviceInfo) {
serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString);
}
} else {
serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, 0, false);
}
return selectInstances(serviceInfo, healthy);
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class NacosNamingService method getAllInstances.
@Override
public List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters, boolean subscribe) throws NacosException {
ServiceInfo serviceInfo;
String clusterString = StringUtils.join(clusters, ",");
if (subscribe) {
serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString);
if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) {
serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString);
}
} else {
serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, 0, false);
}
List<Instance> list;
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
return new ArrayList<Instance>();
}
return list;
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project framework by wcnnkh.
the class NacosClient method getServices.
public List<String> getServices() {
List<ServiceInfo> serviceInfos;
try {
serviceInfos = namingService.getSubscribeServices();
} catch (NacosException e) {
throw new DiscoveryClientException("获取全部服务信息错误", e);
}
if (CollectionUtils.isEmpty(serviceInfos)) {
return Collections.emptyList();
}
List<String> names = new ArrayList<String>(serviceInfos.size());
for (ServiceInfo info : serviceInfos) {
if (groupName == null || groupName.equals(info.getGroupName())) {
names.add(info.getName());
}
}
return names;
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class NamingClientProxyDelegateTest method testSubscribe.
@Test
public void testSubscribe() throws NacosException, NoSuchFieldException, IllegalAccessException {
String ns = "ns1";
ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
Properties props = new Properties();
props.setProperty("serverAddr", "localhost");
InstancesChangeNotifier notifier = new InstancesChangeNotifier();
NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier);
NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class);
Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy");
grpcClientProxyField.setAccessible(true);
grpcClientProxyField.set(delegate, mockGrpcClient);
String serviceName = "service1";
String groupName = "group1";
String clusters = "cluster1";
ServiceInfo info = new ServiceInfo();
info.setName(serviceName);
info.setGroupName(groupName);
info.setClusters(clusters);
when(mockGrpcClient.subscribe(serviceName, groupName, clusters)).thenReturn(info);
ServiceInfo actual = delegate.subscribe(serviceName, groupName, clusters);
Assert.assertEquals(info, actual);
verify(mockGrpcClient, times(1)).subscribe(serviceName, groupName, clusters);
verify(holder, times(1)).processServiceInfo(info);
}
use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.
the class NamingGrpcClientProxyTest method testUnsubscribe.
@Test
public void testUnsubscribe() throws Exception {
SubscribeServiceResponse res = new SubscribeServiceResponse();
ServiceInfo info = new ServiceInfo(GROUP_NAME + "@@" + SERVICE_NAME + "@@" + CLUSTERS);
res.setServiceInfo(info);
when(this.rpcClient.request(any())).thenReturn(res);
client.unsubscribe(SERVICE_NAME, GROUP_NAME, CLUSTERS);
verify(this.rpcClient, times(1)).request(argThat(request -> {
if (request instanceof SubscribeServiceRequest) {
SubscribeServiceRequest request1 = (SubscribeServiceRequest) request;
// not subscribe
return !request1.isSubscribe();
}
return false;
}));
}
Aggregations