use of com.tencent.polaris.router.api.rpc.ProcessRoutersRequest 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.router.api.rpc.ProcessRoutersRequest 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.router.api.rpc.ProcessRoutersRequest in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancerCompositeRule method doRouter.
List<Server> doRouter(List<Server> allServers, Object key) {
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(allServers);
// filter instance by routers
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, key);
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
List<Server> filteredInstances = new ArrayList<>();
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServer(serviceInstances, instance));
}
return filteredInstances;
}
use of com.tencent.polaris.router.api.rpc.ProcessRoutersRequest in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancerCompositeRuleTest method testBuildNearbyRouteRequest.
@Test
public void testBuildNearbyRouteRequest() {
when(polarisNearByRouterProperties.isEnabled()).thenReturn(true);
when(polarisLoadBalancerProperties.getStrategy()).thenReturn("");
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(testCallerService);
setTransitiveMetadata();
PolarisLoadBalancerCompositeRule compositeRule = new PolarisLoadBalancerCompositeRule(routerAPI, polarisLoadBalancerProperties, polarisNearByRouterProperties, polarisMetadataRouterProperties, polarisRuleBasedRouterProperties, config);
ServiceInstances serviceInstances = assembleServiceInstances();
PolarisRouterContext routerContext = assembleRouterContext();
ProcessRoutersRequest request = compositeRule.buildProcessRoutersRequest(serviceInstances, routerContext);
Map<String, String> routerMetadata = request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY);
Assert.assertEquals(0, request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA).size());
Assert.assertEquals(1, routerMetadata.size());
Assert.assertEquals("true", routerMetadata.get(NearbyRouter.ROUTER_ENABLED));
Assert.assertEquals(1, request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size());
Assert.assertEquals("false", request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).get(RuleBasedRouter.ROUTER_ENABLED));
}
}
use of com.tencent.polaris.router.api.rpc.ProcessRoutersRequest in project spring-cloud-tencent by Tencent.
the class PolarisLoadBalancerCompositeRuleTest method testBuildMetadataRouteRequest.
@Test
public void testBuildMetadataRouteRequest() {
when(polarisMetadataRouterProperties.isEnabled()).thenReturn(true);
when(polarisLoadBalancerProperties.getStrategy()).thenReturn("");
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(testCallerService);
setTransitiveMetadata();
PolarisLoadBalancerCompositeRule compositeRule = new PolarisLoadBalancerCompositeRule(routerAPI, polarisLoadBalancerProperties, polarisNearByRouterProperties, polarisMetadataRouterProperties, polarisRuleBasedRouterProperties, config);
ServiceInstances serviceInstances = assembleServiceInstances();
PolarisRouterContext routerContext = assembleRouterContext();
ProcessRoutersRequest request = compositeRule.buildProcessRoutersRequest(serviceInstances, routerContext);
Map<String, String> routerMetadata = request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA);
Assert.assertEquals(1, routerMetadata.size());
Assert.assertEquals(0, request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY).size());
Assert.assertEquals(1, request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size());
Assert.assertEquals("false", request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).get(RuleBasedRouter.ROUTER_ENABLED));
}
}
Aggregations