Search in sources :

Example 1 with RouterAPI

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

ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 GetAllInstancesRequest (com.tencent.polaris.api.rpc.GetAllInstancesRequest)1 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 RouterAPI (com.tencent.polaris.router.api.core.RouterAPI)1 ProcessLoadBalanceRequest (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest)1 ProcessLoadBalanceResponse (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse)1 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)1 RouterNamesGroup (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup)1 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)1 InitResult (com.tencent.polaris.router.example.ExampleUtils.InitResult)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1