Search in sources :

Example 1 with GetAllInstancesRequest

use of com.tencent.polaris.api.rpc.GetAllInstancesRequest in project polaris-java by polarismesh.

the class GetAllInstancesExample method main.

public static void main(String[] args) throws Exception {
    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 + ", service " + service);
        GetAllInstancesRequest allInstancesRequest = new GetAllInstancesRequest();
        allInstancesRequest.setNamespace(namespace);
        allInstancesRequest.setService(service);
        InstancesResponse instancesResponse = consumerAPI.getAllInstance(allInstancesRequest);
        System.out.println("instances count is " + instancesResponse.getInstances().length);
    }
}
Also used : InitResult(com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 2 with GetAllInstancesRequest

use of com.tencent.polaris.api.rpc.GetAllInstancesRequest in project sofa-rpc by sofastack.

the class PolarisWatcher method watchService.

private void watchService() {
    try {
        GetAllInstancesRequest getAllInstancesRequest = new GetAllInstancesRequest();
        getAllInstancesRequest.setNamespace(nameSpace);
        getAllInstancesRequest.setService(serviceName);
        Map<String, String> parameters = new HashMap<>();
        parameters.put("protocol", protocol);
        getAllInstancesRequest.setMetadata(parameters);
        InstancesResponse response = consumerAPI.getAllInstance(getAllInstancesRequest);
        this.currentData = response;
        ProviderGroup providerGroup = new ProviderGroup(currentProviders());
        listeners.stream().filter(Objects::nonNull).forEach(l -> l.updateProviders(providerGroup));
    } catch (Exception e) {
        LOGGER.error(LogCodes.getLog(LogCodes.ERROR_WATCH_HEALTH, "Polaris"), e);
    }
}
Also used : HashMap(java.util.HashMap) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 3 with GetAllInstancesRequest

use of com.tencent.polaris.api.rpc.GetAllInstancesRequest in project sofa-rpc by sofastack.

the class PolarisRegistryTest method testRegister.

@Test
public void testRegister() {
    polaris.getNamingService().addService(new ServiceKey(NAMESPACE, SERVICE));
    // register
    ProviderConfig<?> providerConfig = providerConfig("polaris-test-1", 12200, 12201, 12202);
    registry.register(providerConfig);
    // check register
    ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPI();
    GetAllInstancesRequest getAllInstancesRequest = new GetAllInstancesRequest();
    getAllInstancesRequest.setNamespace(APPNAME);
    getAllInstancesRequest.setService(SERVICE);
    InstancesResponse allInstance = consumerAPI.getAllInstance(getAllInstancesRequest);
    Assert.assertEquals(3, allInstance.getInstances().length);
    // unregister
    registry.unRegister(providerConfig);
    // check unregister ,sleep to wait remove catch
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // if no service will throw a exception
    try {
        consumerAPI.getAllInstance(getAllInstancesRequest);
    } catch (PolarisException e) {
        Assert.assertEquals(SERVER_USER_ERROR, e.getCode());
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 4 with GetAllInstancesRequest

use of com.tencent.polaris.api.rpc.GetAllInstancesRequest in project polaris-java-agent by polarismesh.

the class PolarisDiscoveryHandler method getInstances.

/**
 * Return all instances for the given service.
 *
 * @param service serviceName
 * @return 服务实例列表
 */
public InstancesResponse getInstances(String service) {
    LogUtils.logInvoke(this, "getInstances");
    PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
    String namespace = polarisAgentProperties.getNamespace();
    GetAllInstancesRequest request = new GetAllInstancesRequest();
    request.setNamespace(namespace);
    request.setService(service);
    return consumerAPI.getAllInstance(request);
}
Also used : PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest)

Example 5 with GetAllInstancesRequest

use of com.tencent.polaris.api.rpc.GetAllInstancesRequest in project spring-cloud-tencent by Tencent.

the class PolarisDiscoveryHandler method getInstances.

/**
 * Return all instances for the given service.
 * @param service serviceName
 * @return list of instances
 */
public InstancesResponse getInstances(String service) {
    String namespace = polarisDiscoveryProperties.getNamespace();
    GetAllInstancesRequest request = new GetAllInstancesRequest();
    request.setNamespace(namespace);
    request.setService(service);
    return polarisConsumer.getAllInstance(request);
}
Also used : GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest)

Aggregations

GetAllInstancesRequest (com.tencent.polaris.api.rpc.GetAllInstancesRequest)8 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)5 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)4 HashMap (java.util.HashMap)2 PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 Configuration (com.tencent.polaris.api.config.Configuration)1 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 InitResult (com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult)1 RouterAPI (com.tencent.polaris.router.api.core.RouterAPI)1 ProcessLoadBalanceRequest (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest)1 ProcessLoadBalanceResponse (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse)1 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)1 RouterNamesGroup (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup)1 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)1 InitResult (com.tencent.polaris.router.example.ExampleUtils.InitResult)1