Search in sources :

Example 21 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances 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)

Example 22 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project polaris-java by polarismesh.

the class APIFacade method getInstances.

public static List<?> getInstances(String namespace, String service, Map<String, String> srcLabels, Map<String, String> dstLabels) {
    if (!inited.get()) {
        LOGGER.info("polaris not inited, updateServiceCallResult fail");
        return null;
    }
    GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
    getInstancesRequest.setNamespace(namespace);
    getInstancesRequest.setService(service);
    if (MapUtils.isNotEmpty(srcLabels)) {
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setMetadata(srcLabels);
        getInstancesRequest.setServiceInfo(serviceInfo);
    }
    if (MapUtils.isNotEmpty(dstLabels)) {
        getInstancesRequest.setMetadata(dstLabels);
    }
    InstancesResponse instancesResp = consumerAPI.getInstances(getInstancesRequest);
    ServiceInstances serviceInstances = instancesResp.toServiceInstances();
    return serviceInstances.getInstances();
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 23 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project polaris-java by polarismesh.

the class SyncFlow method commonSyncGetOneInstance.

/**
 * 获取单个实例
 *
 * @param request 请求对象
 * @return 实例应答
 * @throws PolarisException 异常
 */
public InstancesResponse commonSyncGetOneInstance(CommonInstancesRequest request) throws PolarisException {
    syncGetServiceInstances(request);
    ServiceInstances dstInstances = request.getDstInstances();
    if (CollectionUtils.isEmpty(dstInstances.getInstances())) {
        return new InstancesResponse(dstInstances);
    }
    ServiceInstances routerInstances = BaseFlow.processServiceRouters(request.getRouteInfo(), request.getDstInstances(), extensions.getConfigRouterChainGroup());
    LoadBalancer loadBalancer = extensions.getLoadBalancer();
    Instance instance = BaseFlow.processLoadBalance(loadBalancer, request.getCriteria(), routerInstances);
    return new InstancesResponse(dstInstances, instance);
}
Also used : ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Instance(com.tencent.polaris.api.pojo.Instance) LoadBalancer(com.tencent.polaris.api.plugin.loadbalance.LoadBalancer) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 24 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project polaris-java by polarismesh.

the class SyncFlow method commonSyncGetInstances.

/**
 * 获取多个实例列表
 *
 * @param request 请求对象
 * @return 实例应答
 * @throws PolarisException 异常
 */
public InstancesResponse commonSyncGetInstances(CommonInstancesRequest request) throws PolarisException {
    syncGetServiceInstances(request);
    ServiceInstances dstInstances = request.getDstInstances();
    if (CollectionUtils.isEmpty(dstInstances.getInstances())) {
        return new InstancesResponse(dstInstances);
    }
    ServiceInstances routerInstances = BaseFlow.processServiceRouters(request.getRouteInfo(), request.getDstInstances(), extensions.getConfigRouterChainGroup());
    return new InstancesResponse(routerInstances);
}
Also used : ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 25 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project polaris-java by polarismesh.

the class DefaultRouterAPI method processRouters.

@Override
public ProcessRoutersResponse processRouters(ProcessRoutersRequest request) throws PolarisException {
    checkAvailable("RouterAPI");
    RouterValidator.validateProcessRouterRequest(request);
    RouterNamesGroup requestRouterGroup = request.getRouters();
    List<ServiceRouter> beforeRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getBeforeRouters())) {
        beforeRouters = Extensions.loadServiceRouters(requestRouterGroup.getBeforeRouters(), extensions.getPlugins(), true);
    } else {
        beforeRouters = extensions.getConfigRouterChainGroup().getBeforeRouters();
    }
    List<ServiceRouter> afterRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getAfterRouters())) {
        afterRouters = Extensions.loadServiceRouters(requestRouterGroup.getAfterRouters(), extensions.getPlugins(), true);
    } else {
        afterRouters = extensions.getConfigRouterChainGroup().getAfterRouters();
    }
    List<ServiceRouter> coreRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getCoreRouters())) {
        coreRouters = Extensions.loadServiceRouters(requestRouterGroup.getCoreRouters(), extensions.getPlugins(), true);
    } else {
        coreRouters = extensions.getConfigRouterChainGroup().getCoreRouters();
    }
    ServiceInstances dstInstances = request.getDstInstances();
    RouteInfo routeInfo = new RouteInfo(request.getSourceService(), dstInstances, request.getMethod());
    // 获取路由规则
    DefaultFlowControlParam engineFlowControlParam = new DefaultFlowControlParam();
    BaseFlow.buildFlowControlParam(request, config, engineFlowControlParam);
    Set<ServiceEventKey> routerKeys = new HashSet<>();
    ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(dstInstances.getNamespace(), dstInstances.getService()), EventType.ROUTING);
    routerKeys.add(dstSvcEventKey);
    ServiceEventKey srcSvcEventKey = null;
    if (null != routeInfo.getSourceService() && StringUtils.isNotBlank(routeInfo.getSourceService().getNamespace()) && StringUtils.isNotBlank(routeInfo.getSourceService().getService())) {
        srcSvcEventKey = new ServiceEventKey(new ServiceKey(routeInfo.getSourceService().getNamespace(), routeInfo.getSourceService().getService()), EventType.ROUTING);
        routerKeys.add(srcSvcEventKey);
    }
    DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
    svcKeysProvider.setSvcEventKeys(routerKeys);
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, svcKeysProvider, engineFlowControlParam);
    routeInfo.setDestRouteRule(resourcesResponse.getServiceRule(dstSvcEventKey));
    if (null != srcSvcEventKey) {
        routeInfo.setSourceRouteRule(resourcesResponse.getServiceRule(srcSvcEventKey));
    }
    // 执行路由
    DefaultRouterChainGroup routerChainGroup = new DefaultRouterChainGroup(beforeRouters, coreRouters, afterRouters);
    ServiceInstances svcInstances = BaseFlow.processServiceRouters(routeInfo, dstInstances, routerChainGroup);
    return new ProcessRoutersResponse(svcInstances);
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) RouterNamesGroup(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) DefaultFlowControlParam(com.tencent.polaris.client.flow.DefaultFlowControlParam) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) RouteInfo(com.tencent.polaris.api.plugin.route.RouteInfo) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup) HashSet(java.util.HashSet)

Aggregations

ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)25 Instance (com.tencent.polaris.api.pojo.Instance)12 Server (com.netflix.loadbalancer.Server)8 ArrayList (java.util.ArrayList)8 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)7 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)7 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)6 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)6 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)5 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)4 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)4 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)4 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)4 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)4 ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)3 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)3 Services (com.tencent.polaris.api.pojo.Services)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3