Search in sources :

Example 6 with Subscriber

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

the class NamingSubscriberServiceAggregationImplTest method setUp.

@Before
public void setUp() throws Exception {
    aggregation = new NamingSubscriberServiceAggregationImpl(local, memberManager);
    Subscriber subscriber = new Subscriber("local", "", "", "", namespace, serviceName, 0);
    when(local.getSubscribers(namespace, serviceName)).thenReturn(Collections.singletonList(subscriber));
    when(local.getSubscribers(service)).thenReturn(Collections.singletonList(subscriber));
    when(local.getFuzzySubscribers(namespace, serviceName)).thenReturn(Collections.singletonList(subscriber));
    when(local.getFuzzySubscribers(service)).thenReturn(Collections.singletonList(subscriber));
    members = new HashMap<>();
    members.put("1", Mockito.mock(Member.class));
    when(memberManager.getServerList()).thenReturn(members);
}
Also used : Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) Member(com.alibaba.nacos.core.cluster.Member) Before(org.junit.Before)

Example 7 with Subscriber

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

the class ServiceController method subscribers.

/**
 * get subscriber list.
 *
 * @param request http request
 * @return Jackson object node
 */
@GetMapping("/subscribers")
@Secured(action = ActionTypes.READ)
public ObjectNode subscribers(HttpServletRequest request) {
    int pageNo = NumberUtils.toInt(WebUtils.optional(request, "pageNo", "1"));
    int pageSize = NumberUtils.toInt(WebUtils.optional(request, "pageSize", "1000"));
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    boolean aggregation = Boolean.parseBoolean(WebUtils.optional(request, "aggregation", String.valueOf(Boolean.TRUE)));
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    int count = 0;
    try {
        List<Subscriber> subscribers = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation);
        int start = (pageNo - 1) * pageSize;
        if (start < 0) {
            start = 0;
        }
        int end = start + pageSize;
        count = subscribers.size();
        if (end > count) {
            end = count;
        }
        result.replace("subscribers", JacksonUtils.transferToJsonNode(subscribers.subList(start, end)));
        result.put("count", count);
        return result;
    } catch (Exception e) {
        Loggers.SRV_LOG.warn("query subscribers failed!", e);
        result.replace("subscribers", JacksonUtils.createEmptyArrayNode());
        result.put("count", count);
        return result;
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) NacosException(com.alibaba.nacos.api.exception.NacosException) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 8 with Subscriber

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

the class UpgradeOpsController method listInstance.

/**
 * Get all instance of input service.
 *
 * @param request http request
 * @return list of instance
 * @throws Exception any error during list
 */
@GetMapping("/instance/list")
@Secured(action = ActionTypes.READ)
public Object listInstance(@RequestParam(defaultValue = "v2", required = false) String ver, HttpServletRequest request) throws Exception {
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    String agent = WebUtils.getUserAgent(request);
    String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY);
    String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY);
    int udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0"));
    boolean healthyOnly = Boolean.parseBoolean(WebUtils.optional(request, "healthyOnly", "false"));
    boolean isCheck = Boolean.parseBoolean(WebUtils.optional(request, "isCheck", "false"));
    String app = WebUtils.optional(request, "app", StringUtils.EMPTY);
    String env = WebUtils.optional(request, "env", StringUtils.EMPTY);
    String tenant = WebUtils.optional(request, "tid", StringUtils.EMPTY);
    Subscriber subscriber = new Subscriber(clientIP + ":" + udpPort, agent, app, clientIP, namespaceId, serviceName, udpPort, clusters);
    return getInstanceOperator(ver).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 9 with Subscriber

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

the class SubscribeManagerTest method getSubscribersWithFalse.

@Test
public void getSubscribersWithFalse() {
    String serviceName = "test";
    String namespaceId = "public";
    boolean aggregation = Boolean.FALSE;
    try {
        List<Subscriber> clients = new ArrayList<Subscriber>();
        Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName, 0);
        clients.add(subscriber);
        Mockito.when(this.local.getFuzzySubscribers(Mockito.anyString(), Mockito.anyString())).thenReturn(clients);
        List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation);
        Assert.assertNotNull(list);
        Assert.assertEquals(1, list.size());
        Assert.assertEquals("public", list.get(0).getNamespaceId());
    } catch (Exception ignored) {
    }
}
Also used : Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with Subscriber

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

the class SubscribeManagerTest method testGetSubscribersFuzzy.

@Test
public void testGetSubscribersFuzzy() {
    String serviceName = "test";
    String namespaceId = "public";
    boolean aggregation = Boolean.TRUE;
    try {
        List<Subscriber> clients = new ArrayList<Subscriber>();
        Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, "testGroupName@@test_subscriber", 0);
        clients.add(subscriber);
        Mockito.when(this.aggregation.getFuzzySubscribers(Mockito.anyString(), Mockito.anyString())).thenReturn(clients);
        List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation);
        Assert.assertNotNull(list);
        Assert.assertEquals(1, list.size());
        Assert.assertEquals("testGroupName@@test_subscriber", list.get(0).getServiceName());
    } catch (Exception ignored) {
    }
}
Also used : Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) ArrayList(java.util.ArrayList) Test(org.junit.Test)

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