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; }
@SuppressWarnings("SimplifiableIfStatement") @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof ServiceHealth)) return false; ServiceHealth that = (ServiceHealth) o; if (checks != null ? !checks.equals(that.checks) : that.checks != null) return false; return !(node != null ? !node.equals(that.node) : that.node != null) && !(service != null ? !service.equals(that.service) : that.service != null); }
@Override public int hashCode() { int result = node != null ? node.hashCode() : 0; result = 31 * result + (service != null ? service.hashCode() : 0); result = 31 * result + (checks != null ? checks.hashCode() : 0); return result; }
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; }
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 services() { Consul client = Consul.consul(); String id = UUID.randomUUID().toString(); client.agent().registerService("localhost", 8080, 20L, UUID.randomUUID().toString(), id); boolean found = false; for (Map.Entry<String, Service> service : client.agent().getServices().entrySet()) { if (service.getValue().getId().equals(id)) { 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); }
@SuppressWarnings("SimplifiableIfStatement") @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof ServiceHealth)) return false; ServiceHealth that = (ServiceHealth) o; if (checks != null ? !checks.equals(that.checks) : that.checks != null) return false; return !(node != null ? !node.equals(that.node) : that.node != null) && !(service != null ? !service.equals(that.service) : that.service != null); }
@Override public int hashCode() { int result = node != null ? node.hashCode() : 0; result = 31 * result + (service != null ? service.hashCode() : 0); result = 31 * result + (checks != null ? checks.hashCode() : 0); return result; }
@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); }