use of com.tencent.polaris.api.pojo.Instance 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.pojo.Instance 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.pojo.Instance 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.pojo.Instance in project polaris-java by polarismesh.
the class WatchServiceExample method main.
public static void main(String[] args) throws Exception {
ExampleUtils.InitResult initResult = ExampleUtils.initConsumerConfiguration(args, false);
String namespace = initResult.getNamespace();
String service = initResult.getService();
try (ConsumerAPI consumerAPI = ExampleUtils.createConsumerAPI(initResult.getConfig())) {
System.out.println("namespace " + namespace);
WatchServiceRequest request = WatchServiceRequest.builder().namespace(namespace).service(service).listeners(Collections.singletonList(new ServiceWatcher())).build();
request.setNamespace(namespace);
WatchServiceResponse response = consumerAPI.watchService(request);
List<Instance> instances = response.getResponse().toServiceInstances().getInstances();
System.out.println("instance count is " + instances.size());
System.out.println("print all instance " + instances);
new CountDownLatch(1).await();
}
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java-agent by polarismesh.
the class PolarisLoadBalance method doSelect.
@Override
@SuppressWarnings("unchecked")
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
if (null == invokers || invokers.size() == 0) {
return null;
}
String service = url.getServiceInterface();
LOGGER.info("[POLARIS] select instance for service {} by PolarisLoadBalance", service);
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
List<Instance> instances = (List<Instance>) ((List<?>) invokers);
Instance instance = PolarisSingleton.getPolarisOperator().loadBalance(service, key, instances);
return (InstanceInvoker<T>) instance;
}
Aggregations