use of com.tencent.polaris.api.pojo.ServiceInfo in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getOneInstance.
/**
* 利用ConsumerAPI进行路由、负载均衡
*
* @param service
* @return
*/
public static ServiceInstance getOneInstance(String service) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getOneInstance");
PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
String namespace = polarisAgentProperties.getNamespace();
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setService(service);
getOneInstanceRequest.setNamespace(namespace);
String srcNamespace = polarisAgentProperties.getNamespace();
String srcService = polarisAgentProperties.getService();
if (StringUtils.isNotBlank(srcNamespace) || StringUtils.isNotBlank(srcService)) {
ServiceInfo sourceService = new ServiceInfo();
sourceService.setNamespace(srcNamespace);
sourceService.setService(srcService);
getOneInstanceRequest.setServiceInfo(sourceService);
}
Map<String, String> metadata = InvokeContextHolder.get().getMetadata();
if (metadata != null) {
getOneInstanceRequest.setMetadata(metadata);
}
InstancesResponse response = PolarisAPIFactory.getConsumerApi().getOneInstance(getOneInstanceRequest);
List<Instance> instances = response.toServiceInstances().getInstances();
if (CollectionUtils.isNotEmpty(instances)) {
LOGGER.info("success to route and loadBalance by Polaris with instance:{}", instances.get(0));
}
return new PolarisServiceInstance(instances.get(0));
}
use of com.tencent.polaris.api.pojo.ServiceInfo in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getRoutedServiceInstance.
/**
* 路由服务实例(不具有熔断功能)
*
* @param dstInstances
* @return
*/
public static ServiceInstances getRoutedServiceInstance(ServiceInstances dstInstances) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getRoutedServiceInstance");
PolarisAgentProperties agentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
// 执行服务路由
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
// 主调方信息
ServiceInfo srcSourceInfo = new ServiceInfo();
String srcService = agentProperties.getService();
String srcNamespace = agentProperties.getNamespace();
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
srcSourceInfo.setNamespace(srcNamespace);
srcSourceInfo.setService(srcService);
processRoutersRequest.setSourceService(srcSourceInfo);
}
ProcessRoutersRequest.RouterNamesGroup routerNamesGroup = new ProcessRoutersRequest.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 = PolarisAPIFactory.getRouterApi().processRouters(processRoutersRequest);
LOGGER.info("success to route by Polaris with instance size:{}", processRoutersResponse.getServiceInstances().getInstances().size());
return processRoutersResponse.getServiceInstances();
}
use of com.tencent.polaris.api.pojo.ServiceInfo in project spring-cloud-tencent by Tencent.
the class PolarisDiscoveryHandler method getFilteredInstances.
/**
* 获取服务路由后的实例列表
*
* @param service 服务名
* @return 服务实例列表
*/
public InstancesResponse getFilteredInstances(String service) {
String namespace = polarisProperties.getNamespace();
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service);
String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
getInstancesRequest.setMethod(method);
String localNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
String localService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService) || null != allTransitiveCustomMetadata) {
ServiceInfo sourceService = new ServiceInfo();
sourceService.setNamespace(localNamespace);
sourceService.setService(localService);
sourceService.setMetadata(allTransitiveCustomMetadata);
getInstancesRequest.setServiceInfo(sourceService);
}
return polarisConsumer.getInstances(getInstancesRequest);
}
use of com.tencent.polaris.api.pojo.ServiceInfo in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancerCompositeRule method buildProcessRoutersRequest.
ProcessRoutersRequest buildProcessRoutersRequest(ServiceInstances serviceInstances, Object key) {
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
processRoutersRequest.setDstInstances(serviceInstances);
// metadata router
if (polarisMetadataRouterProperties.isEnabled()) {
Map<String, String> transitiveLabels = getRouterLabels(key, PolarisRouterContext.TRANSITIVE_LABELS);
processRoutersRequest.putRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA, transitiveLabels);
}
// nearby router
if (polarisNearByRouterProperties.isEnabled()) {
Map<String, String> nearbyRouterMetadata = new HashMap<>();
nearbyRouterMetadata.put(NearbyRouter.ROUTER_ENABLED, "true");
processRoutersRequest.putRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY, nearbyRouterMetadata);
}
// rule based router
// set dynamic switch for rule based router
boolean ruleBasedRouterEnabled = polarisRuleBasedRouterProperties.isEnabled();
Map<String, String> ruleRouterMetadata = new HashMap<>();
ruleRouterMetadata.put(RuleBasedRouter.ROUTER_ENABLED, String.valueOf(ruleBasedRouterEnabled));
processRoutersRequest.putRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED, ruleRouterMetadata);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setNamespace(MetadataContext.LOCAL_NAMESPACE);
serviceInfo.setService(MetadataContext.LOCAL_SERVICE);
if (ruleBasedRouterEnabled) {
Map<String, String> ruleRouterLabels = getRouterLabels(key, PolarisRouterContext.RULE_ROUTER_LABELS);
// The label information that the rule based routing depends on
// is placed in the metadata of the source service for transmission.
// Later, can consider putting it in routerMetadata like other routers.
serviceInfo.setMetadata(ruleRouterLabels);
}
processRoutersRequest.setSourceService(serviceInfo);
return processRoutersRequest;
}
use of com.tencent.polaris.api.pojo.ServiceInfo in project spring-cloud-tencent by Tencent.
the class PolarisServiceStatusChangeListenerTest method testOnResourceUpdated.
@Test
public void testOnResourceUpdated() {
PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener();
polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher);
// Service update event
ServiceEventKey serviceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.SERVICE);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setNamespace(NAMESPACE_TEST);
serviceInfo.setService(SERVICE_PROVIDER);
// Need update
ServicesByProto oldServices = new ServicesByProto(Collections.emptyList());
ServicesByProto newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
// No need update
oldServices = new ServicesByProto(Collections.singletonList(serviceInfo));
newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
// Instance update event
ServiceEventKey instanceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.INSTANCE);
DefaultInstance instance = new DefaultInstance();
instance.setNamespace(NAMESPACE_TEST);
instance.setService(SERVICE_PROVIDER);
instance.setHost(HOST);
instance.setPort(PORT);
try {
Field instances = ServiceInstancesByProto.class.getDeclaredField("instances");
instances.setAccessible(true);
// Need update
ServiceInstancesByProto oldInstances = new ServiceInstancesByProto();
instances.set(oldInstances, Collections.emptyList());
ServiceInstancesByProto newInstances = new ServiceInstancesByProto();
instances.set(newInstances, Collections.singletonList(instance));
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
// No need update
oldInstances = new ServiceInstancesByProto();
instances.set(oldInstances, Collections.singletonList(instance));
newInstances = new ServiceInstancesByProto();
instances.set(newInstances, Collections.singletonList(instance));
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
} catch (NoSuchFieldException | IllegalAccessException e) {
Assertions.fail("Exception encountered.", e);
}
}
Aggregations