use of com.tencent.polaris.api.pojo.ServiceInfo 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.ServiceInfo in project polaris-java by polarismesh.
the class ConsulAPIConnector method syncGetServices.
@Override
public Services syncGetServices(ServiceUpdateTask serviceUpdateTask) {
Services services = new ServicesByProto(new ArrayList<>());
try {
CatalogServicesRequest request = CatalogServicesRequest.newBuilder().setQueryParams(QueryParams.DEFAULT).build();
ArrayList<String> serviceList = new ArrayList<>(this.consulClient.getCatalogServices(request).getValue().keySet());
for (String s : serviceList) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setService(s);
services.getServices().add(serviceInfo);
}
} catch (ConsulException e) {
throw ServerErrorResponseException.build(ErrorCode.SERVER_USER_ERROR.ordinal(), String.format("Get services of %s instances sync failed.", serviceUpdateTask.getServiceEventKey().getServiceKey()));
}
return services;
}
use of com.tencent.polaris.api.pojo.ServiceInfo in project polaris-java by polarismesh.
the class RouterExample method main.
public static void main(String[] args) throws Exception {
InitResult initResult = ExampleUtils.initConsumerConfiguration(args);
String namespace = initResult.getNamespace();
String service = initResult.getService();
try (SDKContext sdkContext = SDKContext.initContext()) {
ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
RouterAPI routerAPI = RouterAPIFactory.createRouterAPIByContext(sdkContext);
// 1. 拉取全量服务实例
GetAllInstancesRequest getAllInstancesRequest = new GetAllInstancesRequest();
getAllInstancesRequest.setNamespace(namespace);
getAllInstancesRequest.setService(service);
InstancesResponse allInstanceResp = consumerAPI.getAllInstance(getAllInstancesRequest);
ServiceInstances dstInstances = allInstanceResp.toServiceInstances();
// 2. 执行服务路由
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
// 被调服务
System.out.printf("instances count before routing is %s%n", dstInstances.getInstances().size());
// 主调方信息
ServiceInfo srcSourceInfo = new ServiceInfo();
Map<String, String> labels = new HashMap<>();
labels.put("env", "test");
srcSourceInfo.setMetadata(labels);
RouterNamesGroup routerNamesGroup = new RouterNamesGroup();
List<String> coreRouters = new ArrayList<>();
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_RULE);
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_METADATA);
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_NEARBY);
// 设置走规则路由
routerNamesGroup.setCoreRouters(coreRouters);
processRoutersRequest.setDstInstances(dstInstances);
processRoutersRequest.setSourceService(srcSourceInfo);
processRoutersRequest.setRouters(routerNamesGroup);
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
System.out.printf("instances count after routing is %s%n", processRoutersResponse.getServiceInstances().getInstances().size());
// 3. 执行负载均衡
ProcessLoadBalanceRequest processLoadBalanceRequest = new ProcessLoadBalanceRequest();
processLoadBalanceRequest.setDstInstances(processRoutersResponse.getServiceInstances());
processLoadBalanceRequest.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(processLoadBalanceRequest);
System.out.printf("instances after lb is %s%n", processLoadBalanceResponse.getTargetInstance());
}
}
use of com.tencent.polaris.api.pojo.ServiceInfo in project polaris-java by polarismesh.
the class APIFacade method getInstances.
public static List<?> getInstances(String namespace, String service, Map<String, String> srcLabels, Map<String, String> dstLabels) {
if (!inited.get()) {
LOGGER.info("polaris not inited, updateServiceCallResult fail");
return null;
}
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service);
if (MapUtils.isNotEmpty(srcLabels)) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setMetadata(srcLabels);
getInstancesRequest.setServiceInfo(serviceInfo);
}
if (MapUtils.isNotEmpty(dstLabels)) {
getInstancesRequest.setMetadata(dstLabels);
}
InstancesResponse instancesResp = consumerAPI.getInstances(getInstancesRequest);
ServiceInstances serviceInstances = instancesResp.toServiceInstances();
return serviceInstances.getInstances();
}
use of com.tencent.polaris.api.pojo.ServiceInfo in project polaris-java by polarismesh.
the class PrometheusPushHandlerTest method mockCallerService.
private ServiceInfo mockCallerService() {
ServiceInfo caller = new ServiceInfo();
caller.setService("consumer");
caller.setNamespace("Production");
return caller;
}
Aggregations