private EndpointDefinition convertToServiceDefinition(final ServiceHealth serviceHealth) { final String host = serviceHealth.getNode().getAddress(); final int port = serviceHealth.getService().getPort(); final String id = serviceHealth.getService().getId(); final String name = serviceHealth.getService().getService(); final List<String> tags = serviceHealth.getService().getTags(); final EndpointDefinition endpointDefinition = new EndpointDefinition(HealthStatus.PASS, id, name, host, port, tags); if (debug) logger.debug(sputs("convertToServiceDefinition \nserviceHealth", serviceHealth, "\nserviceDefinition", endpointDefinition)); return endpointDefinition; }
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); }
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 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 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); }
private EndpointDefinition convertToServiceDefinition(final ServiceHealth serviceHealth) { final String host = serviceHealth.getNode().getAddress(); final int port = serviceHealth.getService().getPort(); final String id = serviceHealth.getService().getId(); final String name = serviceHealth.getService().getService(); final EndpointDefinition endpointDefinition = new EndpointDefinition(HealthStatus.PASS, id, name, host, port); if (debug) logger.debug(sputs("convertToServiceDefinition \nserviceHealth", serviceHealth, "\nserviceDefinition", endpointDefinition)); return endpointDefinition; }