Search in sources :

Example 1 with ExpressionSelector

use of com.alibaba.nacos.api.selector.ExpressionSelector in project nacos by alibaba.

the class NamingHttpClientProxy method getServiceList.

@Override
public ListView<String> getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException {
    Map<String, String> params = new HashMap<String, String>(16);
    params.put("pageNo", String.valueOf(pageNo));
    params.put("pageSize", String.valueOf(pageSize));
    params.put(CommonParams.NAMESPACE_ID, namespaceId);
    params.put(CommonParams.GROUP_NAME, groupName);
    if (selector != null) {
        switch(SelectorType.valueOf(selector.getType())) {
            case none:
                break;
            case label:
                ExpressionSelector expressionSelector = (ExpressionSelector) selector;
                params.put(SELECTOR_PARAM, JacksonUtils.toJson(expressionSelector));
                break;
            default:
                break;
        }
    }
    String result = reqApi(UtilAndComs.nacosUrlBase + "/service/list", params, HttpMethod.GET);
    JsonNode json = JacksonUtils.toObj(result);
    ListView<String> listView = new ListView<String>();
    listView.setCount(json.get("count").asInt());
    listView.setData(JacksonUtils.toObj(json.get("doms").toString(), new TypeReference<List<String>>() {
    }));
    return listView;
}
Also used : ListView(com.alibaba.nacos.api.naming.pojo.ListView) HashMap(java.util.HashMap) ExpressionSelector(com.alibaba.nacos.api.selector.ExpressionSelector) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 2 with ExpressionSelector

use of com.alibaba.nacos.api.selector.ExpressionSelector in project nacos by alibaba.

the class NamingMaintainService_ITCase method createAndUpdateService.

@Test
public void createAndUpdateService() throws NacosException {
    String serviceName = randomDomainName();
    // register service
    Service preService = new Service();
    preService.setName(serviceName);
    preService.setGroupName(Constants.DEFAULT_GROUP);
    preService.setProtectThreshold(1.0f);
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put(serviceName, "this is a register metadata");
    preService.setMetadata(metadata);
    ExpressionSelector selector = new ExpressionSelector();
    selector.setExpression("CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B");
    System.out.println("service info : " + preService);
    namingMaintainService.createService(preService, selector);
    Service remoteService = namingMaintainService.queryService(serviceName);
    System.out.println("remote service info : " + remoteService);
    Assert.assertEquals(preService.toString(), remoteService.toString());
    // update service
    Service nowService = new Service();
    nowService.setName(serviceName);
    nowService.setGroupName(Constants.DEFAULT_GROUP);
    nowService.setProtectThreshold(1.0f);
    metadata.clear();
    metadata.put(serviceName, "this is a update metadata");
    nowService.setMetadata(metadata);
    namingMaintainService.updateService(nowService, new NoneSelector());
    remoteService = namingMaintainService.queryService(serviceName);
    System.out.println("remote service info : " + remoteService);
    Assert.assertEquals(nowService.toString(), remoteService.toString());
}
Also used : HashMap(java.util.HashMap) ExpressionSelector(com.alibaba.nacos.api.selector.ExpressionSelector) NamingMaintainService(com.alibaba.nacos.api.naming.NamingMaintainService) Service(com.alibaba.nacos.api.naming.pojo.Service) NamingService(com.alibaba.nacos.api.naming.NamingService) NoneSelector(com.alibaba.nacos.api.selector.NoneSelector) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ExpressionSelector

use of com.alibaba.nacos.api.selector.ExpressionSelector in project nacos by alibaba.

the class SelectInstances_ITCase method getServiceListWithSelector.

@Test
@Ignore("TODO nacos 2.0 can't support selector for now")
public void getServiceListWithSelector() throws NacosException, InterruptedException {
    String serviceName = randomDomainName();
    Instance instance = new Instance();
    instance.setIp("128.0.0.1");
    instance.setPort(999);
    instance.setServiceName(serviceName);
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("registerSource", "dubbo");
    instance.setMetadata(metadata);
    naming.registerInstance(serviceName, instance);
    naming.registerInstance(serviceName, "127.0.0.1", 80, "c1");
    naming.registerInstance(serviceName, "127.0.0.2", 80, "c2");
    TimeUnit.SECONDS.sleep(10);
    ExpressionSelector expressionSelector = new ExpressionSelector();
    expressionSelector.setExpression("INSTANCE.label.registerSource = 'dubbo'");
    ListView<String> serviceList = naming.getServicesOfServer(1, 10, expressionSelector);
    Assert.assertTrue(serviceList.getData().contains(serviceName));
    serviceName = randomDomainName();
    instance.setServiceName(serviceName);
    metadata.put("registerSource", "spring");
    instance.setMetadata(metadata);
    naming.registerInstance(serviceName, instance);
    TimeUnit.SECONDS.sleep(10);
    expressionSelector.setExpression("INSTANCE.label.registerSource = 'spring'");
    serviceList = naming.getServicesOfServer(1, 10, expressionSelector);
    Assert.assertTrue(serviceList.getData().contains(serviceName));
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) HashMap(java.util.HashMap) ExpressionSelector(com.alibaba.nacos.api.selector.ExpressionSelector) Ignore(org.junit.Ignore) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ExpressionSelector

use of com.alibaba.nacos.api.selector.ExpressionSelector in project nacos by alibaba.

the class NamingClientProxyDelegateTest method testGetServiceList.

@Test
public void testGetServiceList() 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);
    AbstractSelector selector = new ExpressionSelector();
    int pageNo = 1;
    int pageSize = 10;
    String groupName = "group2";
    delegate.getServiceList(pageNo, pageSize, groupName, selector);
    verify(mockGrpcClient, times(1)).getServiceList(pageNo, pageSize, groupName, selector);
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) AbstractSelector(com.alibaba.nacos.api.selector.AbstractSelector) InstancesChangeNotifier(com.alibaba.nacos.client.naming.event.InstancesChangeNotifier) NamingGrpcClientProxy(com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy) ExpressionSelector(com.alibaba.nacos.api.selector.ExpressionSelector) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with ExpressionSelector

use of com.alibaba.nacos.api.selector.ExpressionSelector in project nacos by alibaba.

the class NamingClientProxyDelegateTest method testUpdateService.

@Test
public void testUpdateService() throws NacosException {
    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);
    Service service = new Service();
    try {
        delegate.updateService(service, new ExpressionSelector());
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) InstancesChangeNotifier(com.alibaba.nacos.client.naming.event.InstancesChangeNotifier) ExpressionSelector(com.alibaba.nacos.api.selector.ExpressionSelector) Service(com.alibaba.nacos.api.naming.pojo.Service) Properties(java.util.Properties) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Aggregations

ExpressionSelector (com.alibaba.nacos.api.selector.ExpressionSelector)6 Test (org.junit.Test)4 Service (com.alibaba.nacos.api.naming.pojo.Service)3 HashMap (java.util.HashMap)3 NamingMaintainService (com.alibaba.nacos.api.naming.NamingMaintainService)2 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)2 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)2 Properties (java.util.Properties)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 ListView (com.alibaba.nacos.api.naming.pojo.ListView)1 AbstractSelector (com.alibaba.nacos.api.selector.AbstractSelector)1 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)1 NamingGrpcClientProxy (com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Field (java.lang.reflect.Field)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1