Search in sources :

Example 11 with Configuration

use of com.tencent.polaris.api.config.Configuration 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 12 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class GetServicesTest method testRoundTrip.

@Test
public void testRoundTrip() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < 5; i++) {
            namingServer.getNamingService().addService(new ServiceKey(NAMESPACE_TEST, "get_services_test_" + i));
        }
        ServicesResponse response = consumerAPI.getServices(GetServicesRequest.builder().namespace(NAMESPACE_TEST).build());
        Assert.assertFalse(CollectionUtils.isEmpty(response.getServices()));
        Assert.assertEquals(5, response.getServices().size());
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ServicesResponse(com.tencent.polaris.api.rpc.ServicesResponse) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) Test(org.junit.Test)

Example 13 with Configuration

use of com.tencent.polaris.api.config.Configuration 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)

Example 14 with Configuration

use of com.tencent.polaris.api.config.Configuration 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 15 with Configuration

use of com.tencent.polaris.api.config.Configuration 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)

Aggregations

Configuration (com.tencent.polaris.api.config.Configuration)25 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)18 Test (org.junit.Test)16 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)15 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)9 PolarisException (com.tencent.polaris.api.exception.PolarisException)7 Instance (com.tencent.polaris.api.pojo.Instance)6 HashMap (java.util.HashMap)6 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)3 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)3 ConfigurationImpl (com.tencent.polaris.factory.config.ConfigurationImpl)3 IOException (java.io.IOException)3 ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)2 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)2 HttpServer (com.sun.net.httpserver.HttpServer)1 StaticMetadataManager (com.tencent.cloud.common.metadata.StaticMetadataManager)1 DiscoveryPropertiesAutoConfiguration (com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration)1 PolarisDiscoveryProperties (com.tencent.cloud.polaris.PolarisDiscoveryProperties)1 APIConfig (com.tencent.polaris.api.config.global.APIConfig)1