Search in sources :

Example 11 with ServiceInstances

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

the class PolarisServiceDiscovery method getInstances.

/**
 * Return all instances for the given service.
 * @param serviceId id of service
 * @return list of instances
 * @throws PolarisException polarisException
 */
public List<ServiceInstance> getInstances(String serviceId) throws PolarisException {
    List<ServiceInstance> instances = new ArrayList<>();
    InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
    ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
    for (Instance instance : serviceInstances.getInstances()) {
        instances.add(new PolarisServiceInstance(instance));
    }
    return instances;
}
Also used : ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) PolarisServiceInstance(com.tencent.cloud.common.pojo.PolarisServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Instance(com.tencent.polaris.api.pojo.Instance) ArrayList(java.util.ArrayList) PolarisServiceInstance(com.tencent.cloud.common.pojo.PolarisServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) PolarisServiceInstance(com.tencent.cloud.common.pojo.PolarisServiceInstance)

Example 12 with ServiceInstances

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

the class PolarisServerList method getServers.

private List<Server> getServers() {
    InstancesResponse allInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
    ServiceInstances serviceInstances = allInstances.toServiceInstances();
    List<Server> polarisServers = new ArrayList<>();
    for (Instance instance : serviceInstances.getInstances()) {
        polarisServers.add(new PolarisServer(serviceInstances, instance));
    }
    return polarisServers;
}
Also used : 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) ArrayList(java.util.ArrayList) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 13 with ServiceInstances

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

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

the class PolarisLoadBalancer method getReachableServers.

@Override
public List<Server> getReachableServers() {
    ServiceInstances serviceInstances;
    if (polarisLoadBalancerProperties.getDiscoveryType().equals(ContextConstant.POLARIS)) {
        serviceInstances = getPolarisDiscoveryServiceInstances();
    } else {
        serviceInstances = getExtendDiscoveryServiceInstances();
    }
    if (serviceInstances == null || CollectionUtils.isEmpty(serviceInstances.getInstances())) {
        return Collections.emptyList();
    }
    List<Server> servers = new LinkedList<>();
    for (Instance instance : serviceInstances.getInstances()) {
        servers.add(new PolarisServer(serviceInstances, instance));
    }
    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) Instance(com.tencent.polaris.api.pojo.Instance) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) LinkedList(java.util.LinkedList)

Example 15 with ServiceInstances

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

the class PolarisLoadBalancer method getExtendDiscoveryServiceInstances.

private ServiceInstances getExtendDiscoveryServiceInstances() {
    List<Server> allServers = super.getAllServers();
    if (CollectionUtils.isEmpty(allServers)) {
        return null;
    }
    ServiceInstances serviceInstances;
    if (StringUtils.isBlank(name)) {
        throw new IllegalStateException("PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
    }
    ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, name);
    List<Instance> instances = new ArrayList<>(8);
    for (Server server : allServers) {
        DefaultInstance instance = new DefaultInstance();
        instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
        instance.setService(name);
        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);
    return serviceInstances;
}
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) Instance(com.tencent.polaris.api.pojo.Instance) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances)

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