Search in sources :

Example 6 with HealthServicesRequest

use of com.ecwid.consul.v1.health.HealthServicesRequest in project polaris-java by polarismesh.

the class ConsulAPIConnector method syncGetServiceInstances.

@Override
public List<DefaultInstance> syncGetServiceInstances(ServiceUpdateTask serviceUpdateTask) {
    List<DefaultInstance> instanceList = new ArrayList<>();
    try {
        HealthServicesRequest request = HealthServicesRequest.newBuilder().setQueryParams(new QueryParams(ConsistencyMode.DEFAULT)).build();
        Response<List<HealthService>> response = this.consulClient.getHealthServices(serviceUpdateTask.getServiceEventKey().getService(), request);
        if (response.getValue() == null || response.getValue().isEmpty()) {
            return Collections.emptyList();
        }
        for (HealthService service : response.getValue()) {
            DefaultInstance instance = new DefaultInstance();
            instance.setId(service.getService().getId());
            instance.setService(service.getService().getService());
            instance.setHost(service.getService().getAddress());
            instance.setPort(service.getService().getPort());
            instanceList.add(instance);
        }
    } catch (ConsulException e) {
        throw ServerErrorResponseException.build(ErrorCode.SERVER_USER_ERROR.ordinal(), String.format("Get service instances of %s sync failed.", serviceUpdateTask.getServiceEventKey().getServiceKey()));
    }
    return instanceList;
}
Also used : HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) ConsulException(com.ecwid.consul.ConsulException) HealthService(com.ecwid.consul.v1.health.model.HealthService) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ArrayList(java.util.ArrayList) QueryParams(com.ecwid.consul.v1.QueryParams) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with HealthServicesRequest

use of com.ecwid.consul.v1.health.HealthServicesRequest in project sofa-rpc by sofastack.

the class ConsulRegistryTest method testRegister.

@Test
public void testRegister() {
    ProviderConfig<?> providerConfig = providerConfig("consul-test-1", 12200, 12201, 12202);
    registry.register(providerConfig);
    ConsulClient consulClient = new ConsulClient("localhost", consul.getHttpPort());
    HealthServicesRequest request = HealthServicesRequest.newBuilder().setPassing(true).build();
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(INTERFACE_ID, request);
        Assert.assertEquals(3, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
    registry.unRegister(providerConfig);
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(INTERFACE_ID, request);
        Assert.assertEquals(0, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
}
Also used : HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) ConsulClient(com.ecwid.consul.v1.ConsulClient) List(java.util.List) Test(org.junit.Test)

Example 8 with HealthServicesRequest

use of com.ecwid.consul.v1.health.HealthServicesRequest in project sofa-rpc by sofastack.

the class ConsulRegistryTest method testRegisterWithCustomName.

@Test
public void testRegisterWithCustomName() {
    ProviderConfig<?> providerConfig = providerConfig("consul-test-1", 12200, 12201, 12202);
    providerConfig.setParameter(ConsulConstants.CONSUL_SERVICE_NAME_KEY, CONSUL_SERVICE_NAME);
    registry.register(providerConfig);
    ConsulClient consulClient = new ConsulClient("localhost", consul.getHttpPort());
    HealthServicesRequest request = HealthServicesRequest.newBuilder().setPassing(true).build();
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(CONSUL_SERVICE_NAME, request);
        Assert.assertEquals(3, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
    registry.unRegister(providerConfig);
    Response<List<HealthService>> healthServicesAfterUnRegister = consulClient.getHealthServices(INTERFACE_ID, request);
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(CONSUL_SERVICE_NAME, request);
        Assert.assertEquals(0, healthServicesAfterUnRegister.getValue().size());
    }, 10, TimeUnit.SECONDS);
}
Also used : HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) ConsulClient(com.ecwid.consul.v1.ConsulClient) List(java.util.List) Test(org.junit.Test)

Example 9 with HealthServicesRequest

use of com.ecwid.consul.v1.health.HealthServicesRequest in project sofa-rpc by sofastack.

the class ConsulRegistryAclTest method testRegisterWithToken.

@Test
public void testRegisterWithToken() {
    ProviderConfig<?> providerConfig = providerConfig("consul-test-1", 12200, 12201, 12202);
    registry.register(providerConfig);
    ConsulClient consulClient = new ConsulClient("localhost", consul.getHttpPort());
    HealthServicesRequest request = HealthServicesRequest.newBuilder().setPassing(true).setToken(token).build();
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(INTERFACE_ID, request);
        Assert.assertEquals(3, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
    HealthServicesRequest requestWithoutToken = HealthServicesRequest.newBuilder().setPassing(true).build();
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(INTERFACE_ID, requestWithoutToken);
        Assert.assertEquals(0, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
    registry.unRegister(providerConfig);
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(INTERFACE_ID, request);
        Assert.assertEquals(0, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
}
Also used : HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) ConsulClient(com.ecwid.consul.v1.ConsulClient) List(java.util.List) Test(org.junit.Test)

Example 10 with HealthServicesRequest

use of com.ecwid.consul.v1.health.HealthServicesRequest in project sofa-rpc by sofastack.

the class ConsulRegistryAclTest method testRegisterWithCustomNameWithToken.

@Test
public void testRegisterWithCustomNameWithToken() {
    ProviderConfig<?> providerConfig = providerConfig("consul-test-1", 12200, 12201, 12202);
    providerConfig.setParameter(ConsulConstants.CONSUL_SERVICE_NAME_KEY, CONSUL_SERVICE_NAME);
    registry.register(providerConfig);
    ConsulClient consulClient = new ConsulClient("localhost", consul.getHttpPort());
    HealthServicesRequest request = HealthServicesRequest.newBuilder().setPassing(true).setToken(token).build();
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(CONSUL_SERVICE_NAME, request);
        Assert.assertEquals(3, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
    HealthServicesRequest requestWithoutToken = HealthServicesRequest.newBuilder().setPassing(true).build();
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(CONSUL_SERVICE_NAME, requestWithoutToken);
        Assert.assertEquals(0, healthServices.getValue().size());
    }, 10, TimeUnit.SECONDS);
    registry.unRegister(providerConfig);
    Response<List<HealthService>> healthServicesAfterUnRegister = consulClient.getHealthServices(INTERFACE_ID, request);
    assertUntil(() -> {
        Response<List<HealthService>> healthServices = consulClient.getHealthServices(CONSUL_SERVICE_NAME, request);
        Assert.assertEquals(0, healthServicesAfterUnRegister.getValue().size());
    }, 10, TimeUnit.SECONDS);
}
Also used : HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) ConsulClient(com.ecwid.consul.v1.ConsulClient) List(java.util.List) Test(org.junit.Test)

Aggregations

HealthServicesRequest (com.ecwid.consul.v1.health.HealthServicesRequest)10 List (java.util.List)8 ConsulClient (com.ecwid.consul.v1.ConsulClient)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 QueryParams (com.ecwid.consul.v1.QueryParams)3 HealthService (com.ecwid.consul.v1.health.model.HealthService)2 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ConsulException (com.ecwid.consul.ConsulException)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1