Search in sources :

Example 1 with PolarisServiceInstance

use of cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance 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)

Aggregations

PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)1 PolarisServiceInstance (cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance)1 Instance (com.tencent.polaris.api.pojo.Instance)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)1 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1