Merge branch '2.2.x'
This commit is contained in:
@@ -73,6 +73,7 @@ public class ConsulAutoConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(Endpoint.class)
|
||||
@EnableConfigurationProperties(ConsulHealthIndicatorProperties.class)
|
||||
protected static class ConsulHealthConfig {
|
||||
|
||||
@Bean
|
||||
@@ -85,8 +86,9 @@ public class ConsulAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledHealthIndicator("consul")
|
||||
public ConsulHealthIndicator consulHealthIndicator(ConsulClient consulClient) {
|
||||
return new ConsulHealthIndicator(consulClient);
|
||||
public ConsulHealthIndicator consulHealthIndicator(ConsulClient consulClient,
|
||||
ConsulHealthIndicatorProperties properties) {
|
||||
return new ConsulHealthIndicator(consulClient, properties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,16 +34,24 @@ public class ConsulHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private ConsulClient consul;
|
||||
|
||||
public ConsulHealthIndicator(ConsulClient consul) {
|
||||
private ConsulHealthIndicatorProperties properties;
|
||||
|
||||
public ConsulHealthIndicator(ConsulClient consul,
|
||||
ConsulHealthIndicatorProperties properties) {
|
||||
this.consul = consul;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
protected void doHealthCheck(Health.Builder builder) {
|
||||
final Response<String> leaderStatus = this.consul.getStatusLeader();
|
||||
final Response<Map<String, List<String>>> services = this.consul
|
||||
.getCatalogServices(CatalogServicesRequest.newBuilder().setQueryParams(QueryParams.DEFAULT).build());
|
||||
builder.up().withDetail("leader", leaderStatus.getValue()).withDetail("services", services.getValue());
|
||||
builder.up().withDetail("leader", leaderStatus.getValue());
|
||||
if (properties.isIncludeServicesQuery()) {
|
||||
final Response<Map<String, List<String>>> services = this.consul
|
||||
.getCatalogServices(CatalogServicesRequest.newBuilder()
|
||||
.setQueryParams(QueryParams.DEFAULT).build());
|
||||
builder.withDetail("services", services.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2013-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.consul;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
/**
|
||||
* Configuration properties for {@link ConsulHealthIndicator}.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.consul.health-indicator")
|
||||
public class ConsulHealthIndicatorProperties {
|
||||
|
||||
/**
|
||||
* Whether or not the indicator should include a query for all registered services
|
||||
* during its execution. When set to {@code false} the indicator only uses the lighter
|
||||
* {@link ConsulClient#getStatusLeader()}. This can be helpful in large deployments
|
||||
* where the number of services returned makes the operation unnecessarily heavy.
|
||||
*/
|
||||
private boolean includeServicesQuery = true;
|
||||
|
||||
boolean isIncludeServicesQuery() {
|
||||
return includeServicesQuery;
|
||||
}
|
||||
|
||||
void setIncludeServicesQuery(boolean includeServicesQuery) {
|
||||
this.includeServicesQuery = includeServicesQuery;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this)
|
||||
.append("includeServicesQuery", this.includeServicesQuery).toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user