com.ecwid.consul.v1.health.model.Check Java Examples

The following examples show how to use com.ecwid.consul.v1.health.model.Check. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsulServiceRegistry.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Override
public Object getStatus(ConsulRegistration registration) {
	String serviceId = registration.getServiceId();
	Response<List<Check>> response = this.client.getHealthChecksForService(serviceId,
			HealthChecksForServiceRequest.newBuilder()
					.setQueryParams(QueryParams.DEFAULT).build());
	List<Check> checks = response.getValue();

	for (Check check : checks) {
		if (check.getServiceId().equals(registration.getInstanceId())) {
			if (check.getName().equalsIgnoreCase("Service Maintenance Mode")) {
				return OUT_OF_SERVICE.getCode();
			}
		}
	}

	return UP.getCode();
}
 
Example #2
Source File: ConsulRegistrationTest.java    From grpc-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() throws ExecutionException, InterruptedException {
    final String serviceId = "grpc-grpc-demo";
    final ConsulClient consulClient = new ConsulClient("localhost", Integer.parseInt(System.getProperty("spring.cloud.consul.port")));


    List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
    assertFalse(instances.isEmpty());

    ServiceInstance serviceInstance = instances.get(0);
    ManagedChannel channel = ManagedChannelBuilder.forAddress(serviceInstance.getHost(), serviceInstance.getPort())
            .usePlaintext()
            .build();

    final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(channel);
    final GreeterOuterClass.HelloRequest helloRequest =GreeterOuterClass.HelloRequest.newBuilder().setName("Bob").build();
    final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
    assertNotNull("Replay should not be null",reply);

    boolean isHealthy = false;
    for(int i=0;i<5; ++i){
        final List<HealthService> healthServices = consulClient.getHealthServices(serviceId, true, QueryParams.DEFAULT).getValue();
        isHealthy =healthServices
                .stream()
                .flatMap(h->h.getChecks().stream())
                .anyMatch(c-> Check.CheckStatus.PASSING.equals(c.getStatus())&& c.getCheckId().contains(serviceId));
        if(isHealthy){
            break;
        }else{
            Thread.sleep(Duration.ofSeconds(10).toMillis());
        }
    }
    assertTrue(isHealthy);
    applicationContext.stop();
}
 
Example #3
Source File: TtlSchedulerRemoveTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
private Check getCheckForService(String serviceId) {
	Response<List<Check>> checkResponse = this.consul
			.getHealthChecksForService(serviceId, HealthChecksForServiceRequest
					.newBuilder().setQueryParams(QueryParams.DEFAULT).build());
	if (checkResponse.getValue().size() > 0) {
		return checkResponse.getValue().get(0);
	}
	return null;
}
 
Example #4
Source File: TtlSchedulerRemoveTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore // FIXME: 3.0.0
public void should_not_send_check_if_service_removed() throws InterruptedException {
	Thread.sleep(1000); // wait for Ttlscheduler to send a check to consul.
	Check serviceCheck = getCheckForService("ttlSchedulerRemove");
	assertThat(serviceCheck.getStatus()).as("Service check is in wrong state")
			.isEqualTo(PASSING);

	// Remove service from TtlScheduler and wait for TTL to expired.
	this.ttlScheduler.remove("ttlSchedulerRemove-id");
	Thread.sleep(2100);
	serviceCheck = getCheckForService("ttlSchedulerRemove");
	assertThat(serviceCheck.getStatus()).as("Service check is in wrong state")
			.isEqualTo(CRITICAL);
}
 
Example #5
Source File: TtlSchedulerTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
private Check getCheckForService(String serviceId) {
	Response<List<Check>> checkResponse = this.consul
			.getHealthChecksForService(serviceId, HealthChecksForServiceRequest
					.newBuilder().setQueryParams(QueryParams.DEFAULT).build());
	if (checkResponse.getValue().size() > 0) {
		return checkResponse.getValue().get(0);
	}
	return null;
}
 
Example #6
Source File: TtlSchedulerTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void should_send_a_check_before_ttl_for_all_services()
		throws InterruptedException {
	Thread.sleep(2100); // Wait for TTL to expired (TTL is set to 2 seconds)

	Check serviceCheck = getCheckForService("ttlScheduler");
	assertThat(serviceCheck).isNotNull();
	assertThat(serviceCheck.getStatus()).isEqualTo(PASSING)
			.as("Service check is in wrong state");
	Check serviceManagementCheck = getCheckForService("ttlScheduler-management");
	assertThat(serviceManagementCheck).isNotNull();
	assertThat(serviceManagementCheck.getStatus()).isEqualTo(PASSING)
			.as("Service management check is in wrong state");
}
 
Example #7
Source File: ConsulAutoServiceRegistrationCustomizedPropsTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Test
public void propertiesAreCorrect() {
	Response<Map<String, Service>> response = this.consul.getAgentServices();
	Map<String, Service> services = response.getValue();
	Service service = services.get("myTestService1-B");
	assertThat(service).as("service was null").isNotNull();
	assertThat(service.getPort()).as("service port is discovery port")
			.isEqualTo(4452);
	assertThat("myTestService1-B").as("service id was wrong")
			.isEqualTo(service.getId());
	assertThat("myTestService-B").as("service name was wrong")
			.isEqualTo(service.getService());
	assertThat("myhost").as("property hostname was wrong")
			.isEqualTo(this.properties.getHostname());
	assertThat("10.0.0.1").as("property ipAddress was wrong")
			.isEqualTo(this.properties.getIpAddress());
	assertThat("myhost").as("service address was wrong")
			.isEqualTo(service.getAddress());
	assertThat(service.getEnableTagOverride())
			.as("property enableTagOverride was wrong").isTrue();
	assertThat(service.getTags()).as("property tags contains the wrong values")
			.containsExactly("mytag");
	HashMap<String, String> entries = new HashMap<>();
	entries.put("key1", "value1");
	entries.put("key2", "value2");
	entries.put("mydefaultzonemetadataname", "myzone");
	entries.put("group", "mygroup");
	entries.put("secure", "false");
	assertThat(service.getMeta()).as("property metadata contains the wrong entries")
			.containsExactlyInAnyOrderEntriesOf(entries);

	Response<List<Check>> checkResponse = this.consul.getHealthChecksForService(
			"myTestService-B", HealthChecksForServiceRequest.newBuilder()
					.setQueryParams(QueryParams.DEFAULT).build());
	List<Check> checks = checkResponse.getValue();
	assertThat(checks).as("checks was wrong size").hasSize(0);
}
 
Example #8
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksState(Check.CheckStatus checkStatus, QueryParams queryParams) {
	String status = checkStatus == null ? "any" : checkStatus.name().toLowerCase();
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/state/" + status, queryParams);

	if (httpResponse.getStatusCode() == 200) {
		List<Check> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Check>>() {
		}.getType());
		return new Response<List<Check>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #9
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksForService(String serviceName, HealthChecksForServiceRequest healthChecksForServiceRequest) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/checks/" + serviceName, healthChecksForServiceRequest.asUrlParameters());

	if (httpResponse.getStatusCode() == 200) {
		List<Check> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Check>>() {
		}.getType());
		return new Response<List<Check>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #10
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksForService(String serviceName, QueryParams queryParams) {
	HealthChecksForServiceRequest request = HealthChecksForServiceRequest.newBuilder()
			.setQueryParams(queryParams)
			.build();

	return getHealthChecksForService(serviceName, request);
}
 
Example #11
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksForNode(String nodeName, QueryParams queryParams) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/node/" + nodeName, queryParams);

	if (httpResponse.getStatusCode() == 200) {
		List<Check> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Check>>() {
		}.getType());
		return new Response<List<Check>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #12
Source File: ConsulServerContext.java    From pampas with Apache License 2.0 5 votes vote down vote up
public boolean isPassingChecks(HealthService service) {
    for (Check check : service.getChecks()) {
        if (check.getStatus() != Check.CheckStatus.PASSING) {
            return false;
        }
    }
    return true;
}
 
Example #13
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksState(QueryParams queryParams) {
	return getHealthChecksState(null, queryParams);
}
 
Example #14
Source File: HealthClient.java    From consul-api with Apache License 2.0 2 votes vote down vote up
/**
 * @deprecated This method will be removed in consul-api 2.0. Use {@link #getHealthChecksForService(String serviceName, HealthChecksForServiceRequest healthChecksForServiceRequest)}
 */
@Deprecated
public Response<List<Check>> getHealthChecksForService(String serviceName, QueryParams queryParams);
 
Example #15
Source File: HealthClient.java    From consul-api with Apache License 2.0 votes vote down vote up
public Response<List<Check>> getHealthChecksForNode(String nodeName, QueryParams queryParams); 
Example #16
Source File: HealthClient.java    From consul-api with Apache License 2.0 votes vote down vote up
public Response<List<Check>> getHealthChecksForService(String serviceName, HealthChecksForServiceRequest healthChecksForServiceRequest); 
Example #17
Source File: HealthClient.java    From consul-api with Apache License 2.0 votes vote down vote up
public Response<List<Check>> getHealthChecksState(QueryParams queryParams); 
Example #18
Source File: HealthClient.java    From consul-api with Apache License 2.0 votes vote down vote up
public Response<List<Check>> getHealthChecksState(Check.CheckStatus checkStatus, QueryParams queryParams);