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());
}
}
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());
}
}
}
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());
}
}
}
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);
}
}
}
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;
}
Aggregations