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