Search in sources :

Example 16 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project spring-cloud-tencent by Tencent.

the class PolarisWeightedRule method choose.

@Override
public Server choose(Object key) {
    List<Server> servers = getLoadBalancer().getReachableServers();
    if (CollectionUtils.isEmpty(servers)) {
        return null;
    }
    ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(servers);
    ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
    request.setDstInstances(serviceInstances);
    request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
    ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);
    Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
    return new PolarisServer(serviceInstances, targetInstance);
}
Also used : ProcessLoadBalanceRequest(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) Server(com.netflix.loadbalancer.Server) Instance(com.tencent.polaris.api.pojo.Instance) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer)

Example 17 with ServiceInstances

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

Example 18 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRuleTest method testBuildRuleBasedRouteRequest.

@Test
public void testBuildRuleBasedRouteRequest() {
    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);
        ServiceInstances serviceInstances = assembleServiceInstances();
        PolarisRouterContext routerContext = assembleRouterContext();
        ProcessRoutersRequest request = compositeRule.buildProcessRoutersRequest(serviceInstances, routerContext);
        Map<String, String> routerMetadata = request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED);
        Assert.assertEquals(1, routerMetadata.size());
        Assert.assertEquals(0, request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA).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("true", 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 19 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRuleTest method assembleServers.

private List<Server> assembleServers() {
    ServiceInstances serviceInstances = assembleServiceInstances();
    List<Server> servers = new LinkedList<>();
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    return servers;
}
Also used : DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) LinkedList(java.util.LinkedList)

Example 20 with ServiceInstances

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

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