use of com.tencent.polaris.api.pojo.DefaultInstance 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.DefaultInstance in project polaris-java by polarismesh.
the class ConsulAPIConnector method syncGetServiceInstances.
@Override
public List<DefaultInstance> syncGetServiceInstances(ServiceUpdateTask serviceUpdateTask) {
List<DefaultInstance> instanceList = new ArrayList<>();
try {
HealthServicesRequest request = HealthServicesRequest.newBuilder().setQueryParams(new QueryParams(ConsistencyMode.DEFAULT)).build();
Response<List<HealthService>> response = this.consulClient.getHealthServices(serviceUpdateTask.getServiceEventKey().getService(), request);
if (response.getValue() == null || response.getValue().isEmpty()) {
return Collections.emptyList();
}
for (HealthService service : response.getValue()) {
DefaultInstance instance = new DefaultInstance();
instance.setId(service.getService().getId());
instance.setService(service.getService().getService());
instance.setHost(service.getService().getAddress());
instance.setPort(service.getService().getPort());
instanceList.add(instance);
}
} catch (ConsulException e) {
throw ServerErrorResponseException.build(ErrorCode.SERVER_USER_ERROR.ordinal(), String.format("Get service instances of %s sync failed.", serviceUpdateTask.getServiceEventKey().getServiceKey()));
}
return instanceList;
}
Aggregations