use of com.tencent.polaris.api.pojo.DefaultServiceInstances in project polaris-java-agent by polarismesh.
the class PolarisOperator method loadBalance.
public Instance loadBalance(String service, String hashKey, List<Instance> instances) {
ServiceKey serviceKey = new ServiceKey(polarisConfig.getNamespace(), service);
DefaultServiceInstances defaultServiceInstances = new DefaultServiceInstances(serviceKey, instances);
ProcessLoadBalanceRequest processLoadBalanceRequest = new ProcessLoadBalanceRequest();
processLoadBalanceRequest.setDstInstances(defaultServiceInstances);
Criteria criteria = new Criteria();
criteria.setHashKey(hashKey);
processLoadBalanceRequest.setCriteria(criteria);
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(processLoadBalanceRequest);
return processLoadBalanceResponse.getTargetInstance();
}
use of com.tencent.polaris.api.pojo.DefaultServiceInstances 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);
}
use of com.tencent.polaris.api.pojo.DefaultServiceInstances 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.DefaultServiceInstances 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.DefaultServiceInstances 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;
}
Aggregations