Search in sources :

Example 21 with Instance

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());
        }
    }
}
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) ConfigurationImpl(com.tencent.polaris.factory.config.ConfigurationImpl) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 22 with Instance

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());
                }
            });
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ExecutorService(java.util.concurrent.ExecutorService) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 23 with Instance

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());
        }
    }
}
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 24 with Instance

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();
    }
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) WatchServiceResponse(com.tencent.polaris.api.rpc.WatchServiceResponse) ExampleUtils(com.tencent.polaris.discovery.example.utils.ExampleUtils) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) CountDownLatch(java.util.concurrent.CountDownLatch) WatchServiceRequest(com.tencent.polaris.api.rpc.WatchServiceRequest)

Example 25 with Instance

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;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) List(java.util.List) InstanceInvoker(cn.polarismesh.agent.plugin.dubbo2.entity.InstanceInvoker)

Aggregations

Instance (com.tencent.polaris.api.pojo.Instance)53 ArrayList (java.util.ArrayList)15 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)14 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)12 Server (com.netflix.loadbalancer.Server)9 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)9 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)6 Configuration (com.tencent.polaris.api.config.Configuration)6 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)6 HashMap (java.util.HashMap)6 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)5 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)5 HashSet (java.util.HashSet)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 PolarisException (com.tencent.polaris.api.exception.PolarisException)4 ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)4 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)4 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)4