use of com.tencent.polaris.api.pojo.Instance 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));
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancerCompositeRule method doRouter.
List<Server> doRouter(List<Server> allServers, Object key) {
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(allServers);
// filter instance by routers
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, key);
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
List<Server> filteredInstances = new ArrayList<>();
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServer(serviceInstances, instance));
}
return filteredInstances;
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisServiceDiscovery method getInstances.
/**
* Return all instances for the given service.
* @param serviceId id of service
* @return list of instances
* @throws PolarisException polarisException
*/
public List<ServiceInstance> getInstances(String serviceId) throws PolarisException {
List<ServiceInstance> instances = new ArrayList<>();
InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
for (Instance instance : serviceInstances.getInstances()) {
instances.add(new PolarisServiceInstance(instance));
}
return instances;
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisServerList method getServers.
private List<Server> getServers() {
InstancesResponse allInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
ServiceInstances serviceInstances = allInstances.toServiceInstances();
List<Server> polarisServers = new ArrayList<>();
for (Instance instance : serviceInstances.getInstances()) {
polarisServers.add(new PolarisServer(serviceInstances, instance));
}
return polarisServers;
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancerCompositeRuleTest method assembleServiceInstances.
private ServiceInstances assembleServiceInstances() {
ServiceKey serviceKey = new ServiceKey(testNamespace, testCalleeService);
List<Instance> instances = new LinkedList<>();
instances.add(new DefaultInstance());
instances.add(new DefaultInstance());
instances.add(new DefaultInstance());
instances.add(new DefaultInstance());
instances.add(new DefaultInstance());
return new DefaultServiceInstances(serviceKey, instances);
}
Aggregations