use of cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getOneInstance.
/**
* 利用ConsumerAPI进行路由、负载均衡
*
* @param service
* @return
*/
public static ServiceInstance getOneInstance(String service) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getOneInstance");
PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
String namespace = polarisAgentProperties.getNamespace();
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setService(service);
getOneInstanceRequest.setNamespace(namespace);
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);
getOneInstanceRequest.setServiceInfo(sourceService);
}
Map<String, String> metadata = InvokeContextHolder.get().getMetadata();
if (metadata != null) {
getOneInstanceRequest.setMetadata(metadata);
}
InstancesResponse response = PolarisAPIFactory.getConsumerApi().getOneInstance(getOneInstanceRequest);
List<Instance> instances = response.toServiceInstances().getInstances();
if (CollectionUtils.isNotEmpty(instances)) {
LOGGER.info("success to route and loadBalance by Polaris with instance:{}", instances.get(0));
}
return new PolarisServiceInstance(instances.get(0));
}
Aggregations