use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisRoutingLoadBalancer method getReachableServers.
@Override
public List<Server> getReachableServers() {
List<Server> allServers = super.getAllServers();
if (CollectionUtils.isEmpty(allServers)) {
return allServers;
}
ServiceInstances serviceInstances = null;
if (allServers.get(0) instanceof PolarisServer) {
serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances();
} else {
String serviceName;
// notice the difference between different service registries
if (StringUtils.isNotBlank(allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery();
} else {
serviceName = allServers.get(0).getMetaInfo().getAppName();
}
if (StringUtils.isBlank(serviceName)) {
throw new IllegalStateException("PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
}
ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, serviceName);
List<Instance> instances = new ArrayList<>(8);
for (Server server : allServers) {
DefaultInstance instance = new DefaultInstance();
instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE);
instance.setService(serviceName);
instance.setHealthy(server.isAlive());
instance.setProtocol(server.getScheme());
instance.setId(server.getId());
instance.setHost(server.getHost());
instance.setPort(server.getPort());
instance.setZone(server.getZone());
instance.setWeight(100);
instances.add(instance);
}
serviceInstances = new DefaultServiceInstances(serviceKey, instances);
}
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
processRoutersRequest.setDstInstances(serviceInstances);
String srcNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
String srcService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
processRoutersRequest.setMethod(method);
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setNamespace(srcNamespace);
serviceInfo.setService(srcService);
serviceInfo.setMetadata(transitiveCustomMetadata);
processRoutersRequest.setSourceService(serviceInfo);
}
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
List<Server> filteredInstances = new ArrayList<>();
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 PolarisWeightedRandomRule method choose.
@Override
public Server choose(Object key) {
List<Server> allServers = getLoadBalancer().getReachableServers();
if (CollectionUtils.isEmpty(allServers)) {
return null;
}
Server server = allServers.get(0);
if (!(server instanceof PolarisServer)) {
throw new IllegalStateException("PolarisDiscoveryRule only support PolarisServer instances");
}
PolarisServer polarisServer = (PolarisServer) server;
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(polarisServer.getServiceInstances());
request.setLbPolicy(POLICY);
ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter.processLoadBalance(request);
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
return new PolarisServer(polarisServer.getServiceInstances(), targetInstance);
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class Consumer method invokeByNameResolution.
private static String invokeByNameResolution(String namespace, String service, String value, ConsumerAPI consumerAPI) {
System.out.println("namespace " + namespace + ", service " + service);
// 1. we need to do naming resolution to get a load balanced host and port
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setNamespace(namespace);
getOneInstanceRequest.setService(service);
InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
Instance[] instances = oneInstance.getInstances();
System.out.println("instances count is " + instances.length);
Instance targetInstance = instances[0];
System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
// 2. invoke the server by the resolved address
String urlStr = String.format("http://%s:%d%s?value=%s", targetInstance.getHost(), targetInstance.getPort(), PATH, value);
long startMillis = System.currentTimeMillis();
HttpResult httpResult = httpGet(urlStr);
long delay = System.currentTimeMillis() - startMillis;
System.out.printf("invoke %s, code is %d, delay is %d%n", urlStr, httpResult.code, delay);
// 3. report the invoke result to polaris-java, to eliminate the fail address
RetStatus status = RetStatus.RetSuccess;
if (httpResult.code != 200) {
status = RetStatus.RetFail;
}
ServiceCallResult result = new ServiceCallResult();
result.setNamespace(namespace);
result.setService(service);
result.setHost(targetInstance.getHost());
result.setPort(targetInstance.getPort());
result.setRetCode(httpResult.code);
result.setDelay(delay);
result.setRetStatus(status);
consumerAPI.updateServiceCallResult(result);
System.out.println("success to call updateServiceCallResult");
return httpResult.message;
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class WeightedRandomBalance method sumTotalWeight.
private int sumTotalWeight(ServiceInstances svcInstances) {
List<Instance> instances = svcInstances.getInstances();
int totalWeight = 0;
if (CollectionUtils.isNotEmpty(instances)) {
for (Instance instance : instances) {
totalWeight += instance.getWeight();
}
}
return totalWeight;
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class ConsistentHashLoadBalance method buildConsistentHashRing.
/**
* 构建一致性hash环
*
* @param serviceInstances 服务实例
* @return 一致性hash环
*/
private TreeMap<Integer, Instance> buildConsistentHashRing(ServiceInstances serviceInstances) {
List<Instance> instances = serviceInstances.getInstances();
TreeMap<Integer, Instance> virtualNodeRing = new TreeMap<>();
for (Instance instance : instances) {
for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
// 新增虚拟节点
virtualNodeRing.put(hashStrategy.getHashCode(instance.getHost() + ":" + instance.getPort() + VIRTUAL_NODE_SUFFIX + i), instance);
}
}
return virtualNodeRing;
}
Aggregations