private List<ServiceHealth> getHealthyServices(final String serviceName) { Consul consul = consul(); String tag = tags.length > 1 ? tags[0] : null; final ConsulResponse<List<ServiceHealth>> consulResponse = consul.health() .getHealthyServices(serviceName, datacenter, tag, buildRequestOptions()); this.lastIndex.set(consulResponse.getIndex()); //noinspection UnnecessaryLocalVariable final List<ServiceHealth> healthyServices = consulResponse.getResponse(); return healthyServices; }
private void assertHealth(String serviceId, boolean found, ConsulResponse<List<ServiceHealth>> response) { List<ServiceHealth> nodes = response.getResponse(); assertEquals(1, nodes.size()); for (ServiceHealth health : nodes) { if (health.getService().getId().equals(serviceId)) { found = true; } } assertTrue(found); }
@Test public void getServices() throws Exception { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); ConsulResponse<Map<String, List<String>>> services = catalogClient.getServices(); assertTrue(services.getResponse().containsKey("consul")); }
@Test public void getSingleService() throws Exception { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); ConsulResponse<List<CatalogService>> services = catalogClient.getService("consul"); assertEquals("consul", services.getResponse().iterator().next().getServiceName()); }
@Test public void nodesByDataCenter() throws UnknownHostException { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); assertFalse(catalogClient.getNodes("dc1", null).getResponse().isEmpty()); }
@Test public void nodes() throws UnknownHostException { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); assertFalse(catalogClient.getNodes().getResponse().isEmpty()); }
@Test public void register() throws UnknownHostException { Consul client = Consul.consul(); String serviceName = UUID.randomUUID().toString(); String serviceId = UUID.randomUUID().toString(); client.agent().registerService("localhost", 80, 20000L, serviceName, serviceId); boolean found = false; for (ServiceHealth health : client.health().getAllNodes(serviceName).getResponse()) { if (health.getService().getId().equals(serviceId)) { found = true; } } assertTrue(found); } }
@Test public void getSingleNode() throws Exception { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); ConsulResponse<CatalogNode> node = catalogClient.getNode(catalogClient.getNodes() .getResponse().iterator().next().getNode()); assertNotNull(node); }
@Test public void deregister() throws Exception { Consul client = Consul.consul(); String serviceName = UUID.randomUUID().toString(); String serviceId = UUID.randomUUID().toString(); client.agent().registerService("localhost", 8080, 10000L, serviceName, serviceId); client.agent().deregister(serviceId); Thread.sleep(1000L); boolean found = false; for (ServiceHealth health : client.health().getAllNodes(serviceName).getResponse()) { if (health.getService().getId().equals(serviceId)) { found = true; } } assertFalse(found); }
@Test public void blockingNodesByDataCenter() throws UnknownHostException { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); long start = System.currentTimeMillis(); ConsulResponse<List<Node>> response = catalogClient.getNodes("dc1", null, requestOptionsBuilder().blockSeconds(2, Integer.MAX_VALUE).build()); long time = System.currentTimeMillis() - start; assertTrue(time >= 2000); assertFalse(response.getResponse().isEmpty()); }
private void verifyState(String state, Consul client, String serviceId, String serviceName) throws Exception { List<ServiceHealth> nodes = client.health().getAllNodes(serviceName).getResponse(); boolean found = false; for (ServiceHealth health : nodes) { if (health.getService().getId().equals(serviceId)) { List<HealthCheck> checks = health.getChecks(); found = true; assertEquals(serviceId, health.getService().getId()); assertNotNull(checks); checks = checks.stream().filter(healthCheck -> healthCheck.getStatus().equals(state)).collect(Collectors.toList()); assertEquals(state, checks.get(0).getStatus()); } } assertTrue(found); }
@Test public void fetchNodeByState() throws UnknownHostException, NotRegisteredException { Consul client = Consul.consul(); String serviceName = UUID.randomUUID().toString(); String serviceId = UUID.randomUUID().toString(); client.agent().registerService(8080, 20L, serviceName, serviceId); client.agent().warn(serviceId); Sys.sleep(1000); boolean found = false; ConsulResponse<List<HealthCheck>> response = client.health().getChecksByState(Status.WARN); for (HealthCheck healthCheck : response.getResponse()) { if (healthCheck.getServiceId().equals(serviceId)) { found = true; } } assertTrue(found); }
private List<ServiceHealth> getHealthyServices(final String serviceName) { Consul consul = consul(); String tag = tags.length > 1 ? tags[0] : null; final ConsulResponse<List<ServiceHealth>> consulResponse = consul.health() .getHealthyServices(serviceName, datacenter, tag, buildRequestOptions()); this.lastIndex.set(consulResponse.getIndex()); //noinspection UnnecessaryLocalVariable final List<ServiceHealth> healthyServices = consulResponse.getResponse(); return healthyServices; }