use of com.tencent.polaris.api.rpc.GetInstancesRequest in project polaris-java by polarismesh.
the class ServiceDynamicRuleTest method testServiceDynamicRule.
@Test
public void testServiceDynamicRule() {
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(NAMESPACE_PRODUCTION);
getInstancesRequest.setService(RULE_ROUTER_SERVICE);
Map<String, String> map = new HashMap<>();
map.put("uid", "144115217417489762");
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setNamespace(NAMESPACE_PRODUCTION);
serviceInfo.setService(RULE_ROUTER_SERVICE);
serviceInfo.setMetadata(map);
// 设置主调方服务信息 即 Metadata等规则信息
getInstancesRequest.setServiceInfo(serviceInfo);
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
InstancesResponse oneInstance = consumerAPI.getInstances(getInstancesRequest);
Assert.assertEquals(MATCH_META_COUNT, oneInstance.getInstances().length);
}
}
use of com.tencent.polaris.api.rpc.GetInstancesRequest in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getInstances.
/**
* 利用ConsumerAPI进行路由
*
* @param service
* @return
*/
public static ServiceInstances getInstances(String service) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getInstances");
PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
String namespace = polarisAgentProperties.getNamespace();
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service);
String srcNamespace = polarisAgentProperties.getNamespace();
String srcService = polarisAgentProperties.getService();
if (StringUtils.isNotBlank(srcNamespace) || StringUtils.isNotBlank(srcService)) {
ServiceInfo sourceService = new ServiceInfo();
sourceService.setNamespace(srcNamespace);
sourceService.setService(srcService);
getInstancesRequest.setServiceInfo(sourceService);
}
InstancesResponse response = PolarisAPIFactory.getConsumerApi().getInstances(getInstancesRequest);
LOGGER.info("success to route by Polaris with instance size:{}", response.getInstances().length);
return response.toServiceInstances();
}
use of com.tencent.polaris.api.rpc.GetInstancesRequest in project spring-cloud-tencent by Tencent.
the class PolarisDiscoveryHandler method getFilteredInstances.
/**
* 获取服务路由后的实例列表
*
* @param service 服务名
* @return 服务实例列表
*/
public InstancesResponse getFilteredInstances(String service) {
String namespace = polarisProperties.getNamespace();
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service);
String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
getInstancesRequest.setMethod(method);
String localNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
String localService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService) || null != allTransitiveCustomMetadata) {
ServiceInfo sourceService = new ServiceInfo();
sourceService.setNamespace(localNamespace);
sourceService.setService(localService);
sourceService.setMetadata(allTransitiveCustomMetadata);
getInstancesRequest.setServiceInfo(sourceService);
}
return polarisConsumer.getInstances(getInstancesRequest);
}
use of com.tencent.polaris.api.rpc.GetInstancesRequest in project polaris-java by polarismesh.
the class APIFacade method getInstances.
public static List<?> getInstances(String namespace, String service, Map<String, String> srcLabels, Map<String, String> dstLabels) {
if (!inited.get()) {
LOGGER.info("polaris not inited, updateServiceCallResult fail");
return null;
}
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service);
if (MapUtils.isNotEmpty(srcLabels)) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setMetadata(srcLabels);
getInstancesRequest.setServiceInfo(serviceInfo);
}
if (MapUtils.isNotEmpty(dstLabels)) {
getInstancesRequest.setMetadata(dstLabels);
}
InstancesResponse instancesResp = consumerAPI.getInstances(getInstancesRequest);
ServiceInstances serviceInstances = instancesResp.toServiceInstances();
return serviceInstances.getInstances();
}
use of com.tencent.polaris.api.rpc.GetInstancesRequest in project polaris-java-agent by polarismesh.
the class PolarisOperator method getAvailableInstances.
/**
* 调用CONSUMER_API获取实例信息
*
* @param service 服务的service
* @return Polaris选择的Instance对象
*/
public Instance[] getAvailableInstances(String service) {
init();
if (!inited.get()) {
LOGGER.error("[POLARIS] fail to getInstances {}, polaris init failed", service);
return null;
}
GetInstancesRequest request = new GetInstancesRequest();
request.setNamespace(polarisConfig.getNamespace());
request.setService(service);
InstancesResponse instances = consumerAPI.getInstances(request);
return instances.getInstances();
}
Aggregations