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); }
public static <T> ConsulResponse<List<T>> consulResponseList(final Class<T> responseType, final HTTP.Response response) { List<T> responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJsonArray(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<List<T>> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
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; }
@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()); }
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; }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HTTP.Response response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@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")); }
public static <T> ConsulResponse<List<T>> consulResponseList(final Class<T> responseType, final HttpTextResponse response) { List<T> responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJsonArray(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<List<T>> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@Test public void nodesByDataCenter() throws UnknownHostException { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); assertFalse(catalogClient.getNodes("dc1", null).getResponse().isEmpty()); }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HttpTextResponse response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@Test public void nodes() throws UnknownHostException { Consul client = Consul.consul(); CatalogEndpoint catalogClient = client.catalog(); assertFalse(catalogClient.getNodes().getResponse().isEmpty()); }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HTTP.Response response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@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); } }
public static <T> ConsulResponse<List<T>> consulResponseList(final Class<T> responseType, final HTTP.Response response) { List<T> responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJsonArray(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<List<T>> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@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); }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HttpTextResponse response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@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); }
public static <T> ConsulResponse<List<T>> consulResponseList(final Class<T> responseType, final HttpTextResponse response) { List<T> responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJsonArray(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<List<T>> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@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); }