Search in sources :

Example 6 with GetOneInstanceRequest

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

the class MetadataRouterTest method testFailoverAllScene.

@Test
public void testFailoverAllScene() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        // 但是不健康,降级策略采用默认返回所有健康实例,其中80端口实例为不健康,故80端口的实例不会返回
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace("Production");
            getInstances2.setService(METADATA_SERVICE);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-1");
            getInstances2.setMetadata(map);
            getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERALL);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                e.printStackTrace();
            }
            Assert.assertNotEquals(80, ins.getInstances()[0].getPort());
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) Configuration(com.tencent.polaris.api.config.Configuration) HashMap(java.util.HashMap) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 7 with GetOneInstanceRequest

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

the class MetadataRouterTest method testFailoverNotKeyScene.

@Test
public void testFailoverNotKeyScene() {
    // 传入Env-set:1-1 ,应该返回第4个实例,因为前三个都包含Env-set这个key
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace("Production");
            getInstances2.setService(METADATA_SERVICE);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-1");
            getInstances2.setMetadata(map);
            // TODO: 通过配置文件来设置该配置
            getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                Assert.fail(e.getMessage());
            }
            Assert.assertEquals(90, ins.getInstances()[0].getPort());
            Assert.assertEquals("127.0.0.1", ins.getInstances()[0].getHost());
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) Configuration(com.tencent.polaris.api.config.Configuration) HashMap(java.util.HashMap) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 8 with GetOneInstanceRequest

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

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

the class Consumer method invokeByNameResolution.

private static String invokeByNameResolution(String namespace, String service, String value, ConsumerAPI consumerAPI) {
    System.out.println("namespace " + namespace + ", service " + service);
    // 1. we need to do naming resolution to get a load balanced host and port
    GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
    getOneInstanceRequest.setNamespace(namespace);
    getOneInstanceRequest.setService(service);
    InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
    Instance[] instances = oneInstance.getInstances();
    System.out.println("instances count is " + instances.length);
    Instance targetInstance = instances[0];
    System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
    // 2. invoke the server by the resolved address
    String urlStr = String.format("http://%s:%d%s?value=%s", targetInstance.getHost(), targetInstance.getPort(), PATH, value);
    long startMillis = System.currentTimeMillis();
    HttpResult httpResult = httpGet(urlStr);
    long delay = System.currentTimeMillis() - startMillis;
    System.out.printf("invoke %s, code is %d, delay is %d%n", urlStr, httpResult.code, delay);
    // 3. report the invoke result to polaris-java, to eliminate the fail address
    RetStatus status = RetStatus.RetSuccess;
    if (httpResult.code != 200) {
        status = RetStatus.RetFail;
    }
    ServiceCallResult result = new ServiceCallResult();
    result.setNamespace(namespace);
    result.setService(service);
    result.setHost(targetInstance.getHost());
    result.setPort(targetInstance.getPort());
    result.setRetCode(httpResult.code);
    result.setDelay(delay);
    result.setRetStatus(status);
    consumerAPI.updateServiceCallResult(result);
    System.out.println("success to call updateServiceCallResult");
    return httpResult.message;
}
Also used : RetStatus(com.tencent.polaris.api.pojo.RetStatus) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 10 with GetOneInstanceRequest

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

the class GetOneInstanceExample method main.

public static void main(String[] args) throws Exception {
    InitResult initResult = ExampleUtils.initConsumerConfiguration(args, false);
    String namespace = initResult.getNamespace();
    String service = initResult.getService();
    try (ConsumerAPI consumerAPI = ExampleUtils.createConsumerAPI(initResult.getConfig())) {
        System.out.println("namespace " + namespace + ", service " + service);
        GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
        getOneInstanceRequest.setNamespace(namespace);
        getOneInstanceRequest.setService(service);
        InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
        Instance[] instances = oneInstance.getInstances();
        System.out.println("instances count is " + instances.length);
        Instance targetInstance = instances[0];
        System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
    }
}
Also used : InitResult(com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Aggregations

GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)12 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)12 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)10 Configuration (com.tencent.polaris.api.config.Configuration)9 Test (org.junit.Test)7 Instance (com.tencent.polaris.api.pojo.Instance)6 PolarisException (com.tencent.polaris.api.exception.PolarisException)5 HashMap (java.util.HashMap)5 PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)1 PolarisServiceInstance (cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance)1 RetStatus (com.tencent.polaris.api.pojo.RetStatus)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)1 InitResult (com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult)1 ConfigurationImpl (com.tencent.polaris.factory.config.ConfigurationImpl)1 InstanceParameter (com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter)1 ExecutorService (java.util.concurrent.ExecutorService)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1