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