Search in sources :

Example 1 with GetOneInstanceRequest

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

the class ConsumerTest method testGetNotExistsService.

@Test
public void testGetNotExistsService() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest request = new GetOneInstanceRequest();
            request.setNamespace(NAMESPACE_TEST);
            request.setService(NOT_EXISTS_SERVICE);
            InstancesResponse oneInstance = consumerAPI.getOneInstance(request);
            Assert.assertFalse(oneInstance.isServiceExist());
        }
        // 把实例加上去,可以重新获取
        InstanceParameter parameter = new InstanceParameter();
        parameter.setHealthy(true);
        parameter.setIsolated(false);
        parameter.setWeight(100);
        namingServer.getNamingService().batchAddInstances(new ServiceKey(NAMESPACE_TEST, NOT_EXISTS_SERVICE), 10100, 10, parameter);
        GetOneInstanceRequest request = new GetOneInstanceRequest();
        request.setNamespace(NAMESPACE_TEST);
        request.setService(NOT_EXISTS_SERVICE);
        InstancesResponse oneInstance = consumerAPI.getOneInstance(request);
        Assert.assertEquals(1, oneInstance.getInstances().length);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstanceParameter(com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 2 with GetOneInstanceRequest

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

the class ConsumerTest method testUseBuiltinAsDiscover.

@Test
public void testUseBuiltinAsDiscover() {
    ValidParam validParam = validParams.get(Operation.ALL_HEALTHY);
    Configuration configuration = TestUtils.configWithEnvAddress();
    ConfigurationImpl configImpl = (ConfigurationImpl) configuration;
    configImpl.setDefault();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < ITERATE_COUNT; i++) {
            GetOneInstanceRequest request = new GetOneInstanceRequest();
            request.setNamespace(NAMESPACE_TEST);
            request.setService(validParam.getServiceName());
            InstancesResponse instancesResponse = consumerAPI.getOneInstance(request);
            Assert.assertEquals(1, instancesResponse.getInstances().length);
            Instance instance = instancesResponse.getInstances()[0];
            Assert.assertTrue(instance.isHealthy());
            Assert.assertFalse(instance.isIsolated());
            Assert.assertEquals(100, instance.getWeight());
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) ConfigurationImpl(com.tencent.polaris.factory.config.ConfigurationImpl) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 3 with GetOneInstanceRequest

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

the class ConsumerTest method concurrentTestSyncGetOneInstance.

public void concurrentTestSyncGetOneInstance(Operation operation) {
    ValidParam validParam = validParams.get(operation);
    Configuration configuration = TestUtils.configWithEnvAddress();
    ExecutorService executorService = Executors.newCachedThreadPool();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < ITERATE_COUNT; i++) {
            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    GetOneInstanceRequest request = new GetOneInstanceRequest();
                    request.setNamespace(NAMESPACE_TEST);
                    request.setService(validParam.getServiceName());
                    InstancesResponse instancesResponse = consumerAPI.getOneInstance(request);
                    Assert.assertEquals(1, instancesResponse.getInstances().length);
                    Instance instance = instancesResponse.getInstances()[0];
                    Assert.assertTrue(instance.isHealthy());
                    Assert.assertFalse(instance.isIsolated());
                    Assert.assertEquals(100, instance.getWeight());
                }
            });
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ExecutorService(java.util.concurrent.ExecutorService) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 4 with GetOneInstanceRequest

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

the class ConsumerTest method commonTestSyncGetOneInstance.

public void commonTestSyncGetOneInstance(Operation operation) {
    ValidParam validParam = validParams.get(operation);
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < ITERATE_COUNT; i++) {
            GetOneInstanceRequest request = new GetOneInstanceRequest();
            request.setNamespace(NAMESPACE_TEST);
            request.setService(validParam.getServiceName());
            InstancesResponse instancesResponse = consumerAPI.getOneInstance(request);
            Assert.assertEquals(1, instancesResponse.getInstances().length);
            Instance instance = instancesResponse.getInstances()[0];
            Assert.assertTrue(instance.isHealthy());
            Assert.assertFalse(instance.isIsolated());
            Assert.assertEquals(100, instance.getWeight());
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) 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)

Example 5 with GetOneInstanceRequest

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

the class MetadataRouterTest method testFailoverNoneScene.

@Test
public void testFailoverNoneScene() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        // 该服务下有四个实例,只有一个80端口实例满足metadata路由,但是不健康,降级策略采用默认不降级
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace(NAMESPACE_PRODUCTION);
            getInstances2.setService(METADATA_SERVICE);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-1");
            getInstances2.setMetadata(map);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                Assert.assertEquals(ErrorCode.METADATA_MISMATCH, e.getCode());
            }
        }
    }
}
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)

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