use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancer method getReachableServers.
@Override
public List<Server> getReachableServers() {
ServiceInstances serviceInstances;
if (polarisLoadBalancerProperties.getDiscoveryType().equals(ContextConstant.POLARIS)) {
serviceInstances = getPolarisDiscoveryServiceInstances();
} else {
serviceInstances = getExtendDiscoveryServiceInstances();
}
if (serviceInstances == null || CollectionUtils.isEmpty(serviceInstances.getInstances())) {
return Collections.emptyList();
}
List<Server> servers = new LinkedList<>();
for (Instance instance : serviceInstances.getInstances()) {
servers.add(new PolarisServer(serviceInstances, instance));
}
return servers;
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancer method getExtendDiscoveryServiceInstances.
private ServiceInstances getExtendDiscoveryServiceInstances() {
List<Server> allServers = super.getAllServers();
if (CollectionUtils.isEmpty(allServers)) {
return null;
}
ServiceInstances serviceInstances;
if (StringUtils.isBlank(name)) {
throw new IllegalStateException("PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
}
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, name);
List<Instance> instances = new ArrayList<>(8);
for (Server server : allServers) {
DefaultInstance instance = new DefaultInstance();
instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
instance.setService(name);
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);
return serviceInstances;
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class LoadBalancerUtils method transferServersToServiceInstances.
public static ServiceInstances transferServersToServiceInstances(List<Server> servers) {
List<Instance> instances = new ArrayList<>(servers.size());
String serviceName = null;
for (Server server : servers) {
if (server instanceof PolarisServer) {
Instance instance = ((PolarisServer) server).getInstance();
instances.add(instance);
if (serviceName == null) {
serviceName = instance.getService();
}
}
}
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName);
return new DefaultServiceInstances(serviceKey, instances);
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisWeightedRule method choose.
@Override
public Server choose(Object key) {
List<Server> servers = getLoadBalancer().getReachableServers();
if (CollectionUtils.isEmpty(servers)) {
return null;
}
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(servers);
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(serviceInstances);
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
return new PolarisServer(serviceInstances, targetInstance);
}
use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.
the class PolarisServiceRegistry method getStatus.
@Override
public Object getStatus(Registration registration) {
String serviceName = registration.getServiceId();
InstancesResponse instancesResponse = polarisDiscoveryHandler.getInstances(serviceName);
Instance[] instances = instancesResponse.getInstances();
if (null == instances || instances.length == 0) {
return null;
}
for (Instance instance : instances) {
if (instance.getHost().equalsIgnoreCase(registration.getHost()) && instance.getPort() == polarisDiscoveryProperties.getPort()) {
return instance.isHealthy() ? "UP" : "DOWN";
}
}
return null;
}
Aggregations