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