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