Search in sources :

Example 21 with Configuration

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

the class ConfigurationImpl method setDefault.

@Override
public void setDefault(Object defaultObject) {
    if (null == global) {
        global = new GlobalConfigImpl();
    }
    if (null == consumer) {
        consumer = new ConsumerConfigImpl();
    }
    if (null == provider) {
        provider = new ProviderConfigImpl();
    }
    if (null == configFile) {
        configFile = new ConfigFileConfigImpl();
    }
    if (null != defaultObject) {
        Configuration configuration = (Configuration) defaultObject;
        global.setDefault(configuration.getGlobal());
        consumer.setDefault(configuration.getConsumer());
        provider.setDefault(configuration.getProvider());
        configFile.setDefault(configuration.getConfigFile());
    }
}
Also used : ConfigFileConfigImpl(com.tencent.polaris.factory.config.configuration.ConfigFileConfigImpl) Configuration(com.tencent.polaris.api.config.Configuration) GlobalConfigImpl(com.tencent.polaris.factory.config.global.GlobalConfigImpl) ConsumerConfigImpl(com.tencent.polaris.factory.config.consumer.ConsumerConfigImpl) ProviderConfigImpl(com.tencent.polaris.factory.config.provider.ProviderConfigImpl)

Example 22 with Configuration

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

the class ConsumerTest method commonTestSyncGetAllInstances.

public void commonTestSyncGetAllInstances(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++) {
            GetAllInstancesRequest request = new GetAllInstancesRequest();
            request.setNamespace(NAMESPACE_TEST);
            request.setService(validParam.getServiceName());
            InstancesResponse instancesResponse = consumerAPI.getAllInstance(request);
            Assert.assertEquals(validParam.getCountAll(), instancesResponse.getInstances().length);
            Assert.assertEquals(validParam.getCountHasWeight() * 100, instancesResponse.getTotalWeight());
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 23 with Configuration

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

the class MetadataRouterTest method testFailoverNotKeyScene2.

@Test
public void testFailoverNotKeyScene2() {
    // 传入Env-set:1-1 ,应该返回第2个实例,因为第一个都包含Env-set这个key
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        // 降级策略采用默认返回所有非metadata健康实例,故只返回不满足metadata的那个实例
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace(NAMESPACE_PRODUCTION);
            getInstances2.setService(METADATA_SERVICE_1);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-1");
            getInstances2.setMetadata(map);
            getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                e.printStackTrace();
            }
            Assert.assertEquals(81, 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 24 with Configuration

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

the class MetadataRouterTest method testNormalScene.

@Test
public void testNormalScene() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        // 该服务下有四个实例,其中有两个满足metadata路由(分别为port70,100),80 90端口不满足
        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-0");
            getInstances2.setMetadata(map);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                e.printStackTrace();
            }
            int port = ins.getInstances()[0].getPort();
            Assert.assertNotEquals(90, port);
            Assert.assertNotEquals(80, port);
        }
    }
}
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 25 with Configuration

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

the class TestUtils method configWithEnvAddress.

/**
 * 从环境变量中获取服务端地址并创建配置
 *
 * @return 配置的服务端地址
 */
public static Configuration configWithEnvAddress() {
    Configuration configuration = ConfigAPIFactory.defaultConfig();
    String[] addresses = getServerAddressFromEnv();
    if (null != addresses) {
        ConfigurationImpl configurationImpl = (ConfigurationImpl) configuration;
        configurationImpl.setDefault();
        configurationImpl.getGlobal().getServerConnector().setAddresses(Arrays.asList(addresses.clone()));
    }
    return configuration;
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ConfigurationImpl(com.tencent.polaris.factory.config.ConfigurationImpl)

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