Search in sources :

Example 31 with ServiceInfo

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);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo)

Example 32 with ServiceInfo

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;
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList)

Example 33 with ServiceInfo

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;
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) DiscoveryClientException(io.basc.framework.cloud.DiscoveryClientException) ArrayList(java.util.ArrayList) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 34 with ServiceInfo

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);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) InstancesChangeNotifier(com.alibaba.nacos.client.naming.event.InstancesChangeNotifier) NamingGrpcClientProxy(com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy) Properties(java.util.Properties) Test(org.junit.Test)

Example 35 with ServiceInfo

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;
    }));
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Service(com.alibaba.nacos.api.naming.pojo.Service) AbstractSelector(com.alibaba.nacos.api.selector.AbstractSelector) Request(com.alibaba.nacos.api.remote.request.Request) ServerListFactory(com.alibaba.nacos.common.remote.client.ServerListFactory) RequestFuture(com.alibaba.nacos.api.remote.RequestFuture) Instance(com.alibaba.nacos.api.naming.pojo.Instance) QueryServiceResponse(com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse) InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) Response(com.alibaba.nacos.api.remote.response.Response) ListView(com.alibaba.nacos.api.naming.pojo.ListView) Collectors(java.util.stream.Collectors) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) ServiceListResponse(com.alibaba.nacos.api.naming.remote.response.ServiceListResponse) List(java.util.List) Stream(java.util.stream.Stream) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NoneSelector(com.alibaba.nacos.api.selector.NoneSelector) Connection(com.alibaba.nacos.common.remote.client.Connection) ConnectionType(com.alibaba.nacos.common.remote.ConnectionType) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) NamingRemoteConstants(com.alibaba.nacos.api.naming.remote.NamingRemoteConstants) RequestCallBack(com.alibaba.nacos.api.remote.RequestCallBack) RpcClient(com.alibaba.nacos.common.remote.client.RpcClient) HashSet(java.util.HashSet) SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) InstanceResponse(com.alibaba.nacos.api.naming.remote.response.InstanceResponse) NacosException(com.alibaba.nacos.api.exception.NacosException) ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) ExpectedException(org.junit.rules.ExpectedException) NamingGrpcRedoService(com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService) Before(org.junit.Before) Properties(java.util.Properties) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) DefaultRequestFuture(com.alibaba.nacos.api.remote.DefaultRequestFuture) ServerListChangedEvent(com.alibaba.nacos.client.naming.event.ServerListChangedEvent) Assert(org.junit.Assert) SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) Test(org.junit.Test)

Aggregations

ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)74 Test (org.junit.Test)44 Instance (com.alibaba.nacos.api.naming.pojo.Instance)29 ArrayList (java.util.ArrayList)17 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)12 Properties (java.util.Properties)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 NacosException (com.alibaba.nacos.api.exception.NacosException)8 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)8 Before (org.junit.Before)8 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)7 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)5 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)4 SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)4 PushDataWrapper (com.alibaba.nacos.naming.push.v2.PushDataWrapper)4 HashMap (java.util.HashMap)4 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)3 Secured (com.alibaba.nacos.auth.annotation.Secured)3 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)3