Search in sources :

Example 1 with ProcessRoutersRequest

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();
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList)

Example 2 with ProcessRoutersRequest

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;
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) HashMap(java.util.HashMap) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)

Example 3 with 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;
}
Also used : ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) Instance(com.tencent.polaris.api.pojo.Instance) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer)

Example 4 with ProcessRoutersRequest

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));
    }
}
Also used : DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with ProcessRoutersRequest

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));
    }
}
Also used : DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)9 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)6 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)5 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)5 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)5 ArrayList (java.util.ArrayList)4 ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Server (com.netflix.loadbalancer.Server)2 Instance (com.tencent.polaris.api.pojo.Instance)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)2 HashMap (java.util.HashMap)2 PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)1 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)1 PolarisServer (com.tencent.cloud.polaris.pojo.PolarisServer)1 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 GetAllInstancesRequest (com.tencent.polaris.api.rpc.GetAllInstancesRequest)1 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)1