Search in sources :

Example 31 with Instance

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

the class PolarisServiceRouter method getOneInstance.

/**
 * 利用ConsumerAPI进行路由、负载均衡
 *
 * @param service
 * @return
 */
public static ServiceInstance getOneInstance(String service) {
    LogUtils.logInvoke(PolarisServiceRouter.class, "getOneInstance");
    PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
    String namespace = polarisAgentProperties.getNamespace();
    GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
    getOneInstanceRequest.setService(service);
    getOneInstanceRequest.setNamespace(namespace);
    String srcNamespace = polarisAgentProperties.getNamespace();
    String srcService = polarisAgentProperties.getService();
    if (StringUtils.isNotBlank(srcNamespace) || StringUtils.isNotBlank(srcService)) {
        ServiceInfo sourceService = new ServiceInfo();
        sourceService.setNamespace(srcNamespace);
        sourceService.setService(srcService);
        getOneInstanceRequest.setServiceInfo(sourceService);
    }
    Map<String, String> metadata = InvokeContextHolder.get().getMetadata();
    if (metadata != null) {
        getOneInstanceRequest.setMetadata(metadata);
    }
    InstancesResponse response = PolarisAPIFactory.getConsumerApi().getOneInstance(getOneInstanceRequest);
    List<Instance> instances = response.toServiceInstances().getInstances();
    if (CollectionUtils.isNotEmpty(instances)) {
        LOGGER.info("success to route and loadBalance by Polaris with instance:{}", instances.get(0));
    }
    return new PolarisServiceInstance(instances.get(0));
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Instance(com.tencent.polaris.api.pojo.Instance) PolarisServiceInstance(cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) PolarisServiceInstance(cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance)

Example 32 with Instance

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

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

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

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

the class PolarisLoadBalancerCompositeRuleTest method assembleServiceInstances.

private ServiceInstances assembleServiceInstances() {
    ServiceKey serviceKey = new ServiceKey(testNamespace, testCalleeService);
    List<Instance> instances = new LinkedList<>();
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    return new DefaultServiceInstances(serviceKey, instances);
}
Also used : DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) LinkedList(java.util.LinkedList)

Aggregations

Instance (com.tencent.polaris.api.pojo.Instance)53 ArrayList (java.util.ArrayList)15 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)14 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)12 Server (com.netflix.loadbalancer.Server)9 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)9 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)6 Configuration (com.tencent.polaris.api.config.Configuration)6 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)6 HashMap (java.util.HashMap)6 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)5 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)5 HashSet (java.util.HashSet)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 PolarisException (com.tencent.polaris.api.exception.PolarisException)4 ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)4 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)4 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)4