Search in sources :

Example 1 with SubscribeServiceResponse

use of com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse in project nacos by alibaba.

the class NamingGrpcClientProxyTest method testSubscribe.

@Test
public void testSubscribe() 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);
    ServiceInfo actual = client.subscribe(SERVICE_NAME, GROUP_NAME, CLUSTERS);
    Assert.assertEquals(info, actual);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) Test(org.junit.Test)

Example 2 with SubscribeServiceResponse

use of com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse 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)

Example 3 with SubscribeServiceResponse

use of com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse in project nacos by alibaba.

the class NamingGrpcClientProxy method doSubscribe.

/**
 * Execute subscribe operation.
 *
 * @param serviceName service name
 * @param groupName   group name
 * @param clusters    clusters, current only support subscribe all clusters, maybe deprecated
 * @return current service info of subscribe service
 * @throws NacosException nacos exception
 */
public ServiceInfo doSubscribe(String serviceName, String groupName, String clusters) throws NacosException {
    SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, groupName, serviceName, clusters, true);
    SubscribeServiceResponse response = requestToServer(request, SubscribeServiceResponse.class);
    redoService.subscriberRegistered(serviceName, groupName, clusters);
    return response.getServiceInfo();
}
Also used : SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)

Example 4 with SubscribeServiceResponse

use of com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse in project nacos by alibaba.

the class SubscribeServiceRequestHandler method handle.

@Override
@Secured(action = ActionTypes.READ)
public SubscribeServiceResponse handle(SubscribeServiceRequest request, RequestMeta meta) throws NacosException {
    String namespaceId = request.getNamespace();
    String serviceName = request.getServiceName();
    String groupName = request.getGroupName();
    String app = request.getHeader("app", "unknown");
    String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
    Service service = Service.newService(namespaceId, groupName, serviceName, true);
    Subscriber subscriber = new Subscriber(meta.getClientIp(), meta.getClientVersion(), app, meta.getClientIp(), namespaceId, groupedServiceName, 0, request.getClusters());
    ServiceInfo serviceInfo = ServiceUtil.selectInstancesWithHealthyProtection(serviceStorage.getData(service), metadataManager.getServiceMetadata(service).orElse(null), subscriber);
    if (request.isSubscribe()) {
        clientOperationService.subscribeService(service, subscriber, meta.getConnectionId());
    } else {
        clientOperationService.unsubscribeService(service, subscriber, meta.getConnectionId());
    }
    return new SubscribeServiceResponse(ResponseCode.SUCCESS.getCode(), "success", serviceInfo);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 5 with SubscribeServiceResponse

use of com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse in project nacos by alibaba.

the class SubscribeServiceRequestHandlerTest method testHandle.

@Test
public void testHandle() throws NacosException {
    Instance instance = new Instance();
    instance.setIp("1.1.1.1");
    List<Instance> instances = Arrays.asList(instance);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setGroupName("A");
    serviceInfo.setGroupName("B");
    serviceInfo.setName("C");
    serviceInfo.setHosts(instances);
    Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(serviceMetadata));
    SubscribeServiceRequest subscribeServiceRequest = new SubscribeServiceRequest();
    subscribeServiceRequest.setNamespace("A");
    subscribeServiceRequest.setGroupName("B");
    subscribeServiceRequest.setServiceName("C");
    subscribeServiceRequest.setSubscribe(true);
    SubscribeServiceResponse subscribeServiceResponse = subscribeServiceRequestHandler.handle(subscribeServiceRequest, new RequestMeta());
    Assert.assertEquals(subscribeServiceResponse.getServiceInfo().getName(), "C");
    Mockito.verify(clientOperationService).subscribeService(Mockito.any(), Mockito.any(), Mockito.anyString());
    subscribeServiceRequest.setSubscribe(false);
    subscribeServiceResponse = subscribeServiceRequestHandler.handle(subscribeServiceRequest, new RequestMeta());
    Assert.assertEquals(subscribeServiceResponse.getServiceInfo().getName(), "C");
    Mockito.verify(clientOperationService).subscribeService(Mockito.any(), Mockito.any(), Mockito.anyString());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) Instance(com.alibaba.nacos.api.naming.pojo.Instance) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Aggregations

SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)5 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)4 SubscribeServiceRequest (com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest)3 Test (org.junit.Test)3 Instance (com.alibaba.nacos.api.naming.pojo.Instance)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 ListView (com.alibaba.nacos.api.naming.pojo.ListView)1 Service (com.alibaba.nacos.api.naming.pojo.Service)1 NamingRemoteConstants (com.alibaba.nacos.api.naming.remote.NamingRemoteConstants)1 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)1 InstanceResponse (com.alibaba.nacos.api.naming.remote.response.InstanceResponse)1 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)1 ServiceListResponse (com.alibaba.nacos.api.naming.remote.response.ServiceListResponse)1 DefaultRequestFuture (com.alibaba.nacos.api.remote.DefaultRequestFuture)1 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)1 RequestFuture (com.alibaba.nacos.api.remote.RequestFuture)1 Request (com.alibaba.nacos.api.remote.request.Request)1 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)1 Response (com.alibaba.nacos.api.remote.response.Response)1 AbstractSelector (com.alibaba.nacos.api.selector.AbstractSelector)1