Search in sources :

Example 1 with ProcessRoutersResponse

use of com.tencent.polaris.router.api.rpc.ProcessRoutersResponse 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 ProcessRoutersResponse

use of com.tencent.polaris.router.api.rpc.ProcessRoutersResponse 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 3 with ProcessRoutersResponse

use of com.tencent.polaris.router.api.rpc.ProcessRoutersResponse in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRuleTest method testRouter.

@Test
public void testRouter() {
    when(polarisRuleBasedRouterProperties.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);
        ProcessRoutersResponse assembleResponse = assembleProcessRoutersResponse();
        when(routerAPI.processRouters(any())).thenReturn(assembleResponse);
        List<Server> servers = compositeRule.doRouter(assembleServers(), assembleRouterContext());
        Assert.assertEquals(assembleResponse.getServiceInstances().getInstances().size(), servers.size());
    }
}
Also used : ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) Test(org.junit.Test)

Example 4 with ProcessRoutersResponse

use of com.tencent.polaris.router.api.rpc.ProcessRoutersResponse 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;
}
Also used : Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer)

Example 5 with ProcessRoutersResponse

use of com.tencent.polaris.router.api.rpc.ProcessRoutersResponse 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());
    }
}
Also used : HashMap(java.util.HashMap) RouterNamesGroup(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) ArrayList(java.util.ArrayList) RouterAPI(com.tencent.polaris.router.api.core.RouterAPI) SDKContext(com.tencent.polaris.client.api.SDKContext) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) ProcessLoadBalanceRequest(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) InitResult(com.tencent.polaris.router.example.ExampleUtils.InitResult) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Aggregations

ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)7 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)5 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)4 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)4 ArrayList (java.util.ArrayList)4 Server (com.netflix.loadbalancer.Server)3 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)3 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)2 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)2 Instance (com.tencent.polaris.api.pojo.Instance)2 RouterNamesGroup (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup)2 PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)1 ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)1 PolarisServer (com.tencent.cloud.polaris.pojo.PolarisServer)1 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)1 DefaultRouterChainGroup (com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)1 RouteInfo (com.tencent.polaris.api.plugin.route.RouteInfo)1 ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)1