Search in sources :

Example 11 with Subscriber

use of com.alibaba.nacos.naming.pojo.Subscriber 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 12 with Subscriber

use of com.alibaba.nacos.naming.pojo.Subscriber in project nacos by alibaba.

the class InstanceOperatorClientImplTest method testListInstance.

@Test
public void testListInstance() {
    Mockito.when(pushService.canEnablePush(Mockito.anyString())).thenReturn(true);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setGroupName("DEFAULT_GROUP");
    serviceInfo.setName("B");
    Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
    ServiceMetadata metadata = new ServiceMetadata();
    Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
    Subscriber subscriber = new Subscriber("2.2.2.2", "", "app", "1.1.1.1", "A", "B", 8848);
    instanceOperatorClient.listInstance("A", "B", subscriber, "C", true);
    Mockito.verify(clientOperationService).subscribeService(Mockito.any(), Mockito.any(), Mockito.anyString());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Example 13 with Subscriber

use of com.alibaba.nacos.naming.pojo.Subscriber in project nacos by alibaba.

the class PushExecuteTask method run.

@Override
public void run() {
    try {
        PushDataWrapper wrapper = generatePushData();
        ClientManager clientManager = delayTaskEngine.getClientManager();
        for (String each : getTargetClientIds()) {
            Client client = clientManager.getClient(each);
            if (null == client) {
                // means this client has disconnect
                continue;
            }
            Subscriber subscriber = clientManager.getClient(each).getSubscriber(service);
            delayTaskEngine.getPushExecutor().doPushWithCallback(each, subscriber, wrapper, new NamingPushCallback(each, subscriber, wrapper.getOriginalData(), delayTask.isPushToAll()));
        }
    } catch (Exception e) {
        Loggers.PUSH.error("Push task for service" + service.getGroupedServiceName() + " execute failed ", e);
        delayTaskEngine.addTask(service, new PushDelayTask(service, 1000L));
    }
}
Also used : PushDataWrapper(com.alibaba.nacos.naming.push.v2.PushDataWrapper) Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) ClientManager(com.alibaba.nacos.naming.core.v2.client.manager.ClientManager) Client(com.alibaba.nacos.naming.core.v2.client.Client) NoRequiredRetryException(com.alibaba.nacos.naming.push.v2.NoRequiredRetryException)

Example 14 with Subscriber

use of com.alibaba.nacos.naming.pojo.Subscriber in project nacos by alibaba.

the class InstanceControllerV2 method list.

/**
 * Get all instance of input service.
 *
 * @param namespaceId namespace id
 * @param serviceName service name
 * @param clusters    service clusters
 * @param clientIP    service clientIP
 * @param udpPort     udpPort
 * @param healthyOnly healthyOnly
 * @param app         app
 * @param request     http request
 * @return list of instance
 * @throws Exception any error during list
 */
@GetMapping("/list")
@Secured(action = ActionTypes.READ)
public Object list(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = StringUtils.EMPTY) String clusters, @RequestParam(defaultValue = StringUtils.EMPTY) String clientIP, @RequestParam(defaultValue = "0") Integer udpPort, @RequestParam(defaultValue = "false") Boolean healthyOnly, @RequestParam(defaultValue = StringUtils.EMPTY) String app, HttpServletRequest request) throws Exception {
    NamingUtils.checkServiceNameFormat(serviceName);
    String agent = WebUtils.getUserAgent(request);
    Subscriber subscriber = new Subscriber(clientIP + ":" + udpPort, agent, app, clientIP, namespaceId, serviceName, udpPort, clusters);
    return instanceServiceV2.listInstance(namespaceId, serviceName, subscriber, clusters, healthyOnly);
}
Also used : Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 15 with Subscriber

use of com.alibaba.nacos.naming.pojo.Subscriber in project nacos by alibaba.

the class NamingSubscriberServiceV2ImplTest method setUp.

@Before
public void setUp() throws Exception {
    subscriberService = new NamingSubscriberServiceV2Impl(clientManager, indexesManager, null, null, null, upgradeJudgement, switchDomain);
    ReflectionTestUtils.setField(subscriberService, "delayTaskEngine", delayTaskEngine);
    when(indexesManager.getAllClientsSubscribeService(service)).thenReturn(Collections.singletonList(testClientId));
    when(indexesManager.getAllClientsSubscribeService(service1)).thenReturn(Collections.singletonList(testClientId));
    Collection<Service> services = new LinkedList<>();
    services.add(service);
    services.add(service1);
    when(indexesManager.getSubscribedService()).thenReturn(services);
    when(clientManager.getClient(testClientId)).thenReturn(client);
    when(client.getSubscriber(service)).thenReturn(new Subscriber("1.1.1.1:1111", "Test", "unknown", "1.1.1.1", "N", service.getGroupedServiceName(), 0));
    when(client.getSubscriber(service1)).thenReturn(new Subscriber("1.1.1.1:1111", "Test", "unknown", "1.1.1.1", "N", service1.getGroupedServiceName(), 0));
    when(upgradeJudgement.isUseGrpcFeatures()).thenReturn(true);
}
Also used : Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) LinkedList(java.util.LinkedList) Before(org.junit.Before)

Aggregations

Subscriber (com.alibaba.nacos.naming.pojo.Subscriber)15 Test (org.junit.Test)6 Secured (com.alibaba.nacos.auth.annotation.Secured)5 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)4 ArrayList (java.util.ArrayList)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 Before (org.junit.Before)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)1 Member (com.alibaba.nacos.core.cluster.Member)1 Client (com.alibaba.nacos.naming.core.v2.client.Client)1 ClientManager (com.alibaba.nacos.naming.core.v2.client.manager.ClientManager)1 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)1 NoRequiredRetryException (com.alibaba.nacos.naming.push.v2.NoRequiredRetryException)1 PushDataWrapper (com.alibaba.nacos.naming.push.v2.PushDataWrapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 LinkedList (java.util.LinkedList)1