Search in sources :

Example 6 with GetInstancesRequest

use of com.tencent.polaris.api.rpc.GetInstancesRequest in project polaris-java by polarismesh.

the class ServiceDynamicRuleTest method testServiceDynamicRule.

@Test
public void testServiceDynamicRule() {
    GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
    getInstancesRequest.setNamespace(NAMESPACE_PRODUCTION);
    getInstancesRequest.setService(RULE_ROUTER_SERVICE);
    Map<String, String> map = new HashMap<>();
    map.put("uid", "144115217417489762");
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setNamespace(NAMESPACE_PRODUCTION);
    serviceInfo.setService(RULE_ROUTER_SERVICE);
    serviceInfo.setMetadata(map);
    // 设置主调方服务信息 即 Metadata等规则信息
    getInstancesRequest.setServiceInfo(serviceInfo);
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        InstancesResponse oneInstance = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MATCH_META_COUNT, oneInstance.getInstances().length);
    }
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Configuration(com.tencent.polaris.api.config.Configuration) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) HashMap(java.util.HashMap) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 7 with GetInstancesRequest

use of com.tencent.polaris.api.rpc.GetInstancesRequest in project polaris-java-agent by polarismesh.

the class PolarisServiceRouter method getInstances.

/**
 * 利用ConsumerAPI进行路由
 *
 * @param service
 * @return
 */
public static ServiceInstances getInstances(String service) {
    LogUtils.logInvoke(PolarisServiceRouter.class, "getInstances");
    PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
    String namespace = polarisAgentProperties.getNamespace();
    GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
    getInstancesRequest.setNamespace(namespace);
    getInstancesRequest.setService(service);
    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);
        getInstancesRequest.setServiceInfo(sourceService);
    }
    InstancesResponse response = PolarisAPIFactory.getConsumerApi().getInstances(getInstancesRequest);
    LOGGER.info("success to route by Polaris with instance size:{}", response.getInstances().length);
    return response.toServiceInstances();
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 8 with GetInstancesRequest

use of com.tencent.polaris.api.rpc.GetInstancesRequest in project spring-cloud-tencent by Tencent.

the class PolarisDiscoveryHandler method getFilteredInstances.

/**
 * 获取服务路由后的实例列表
 *
 * @param service 服务名
 * @return 服务实例列表
 */
public InstancesResponse getFilteredInstances(String service) {
    String namespace = polarisProperties.getNamespace();
    GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
    getInstancesRequest.setNamespace(namespace);
    getInstancesRequest.setService(service);
    String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
    getInstancesRequest.setMethod(method);
    String localNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
    String localService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
    Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
    if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService) || null != allTransitiveCustomMetadata) {
        ServiceInfo sourceService = new ServiceInfo();
        sourceService.setNamespace(localNamespace);
        sourceService.setService(localService);
        sourceService.setMetadata(allTransitiveCustomMetadata);
        getInstancesRequest.setServiceInfo(sourceService);
    }
    return polarisConsumer.getInstances(getInstancesRequest);
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest)

Example 9 with GetInstancesRequest

use of com.tencent.polaris.api.rpc.GetInstancesRequest 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 10 with GetInstancesRequest

use of com.tencent.polaris.api.rpc.GetInstancesRequest in project polaris-java-agent by polarismesh.

the class PolarisOperator method getAvailableInstances.

/**
 * 调用CONSUMER_API获取实例信息
 *
 * @param service 服务的service
 * @return Polaris选择的Instance对象
 */
public Instance[] getAvailableInstances(String service) {
    init();
    if (!inited.get()) {
        LOGGER.error("[POLARIS] fail to getInstances {}, polaris init failed", service);
        return null;
    }
    GetInstancesRequest request = new GetInstancesRequest();
    request.setNamespace(polarisConfig.getNamespace());
    request.setService(service);
    InstancesResponse instances = consumerAPI.getInstances(request);
    return instances.getInstances();
}
Also used : GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Aggregations

GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)10 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)9 Configuration (com.tencent.polaris.api.config.Configuration)5 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)5 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)4 Test (org.junit.Test)4 Instance (com.tencent.polaris.api.pojo.Instance)3 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)3 PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)1 CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)1 HashMap (java.util.HashMap)1