聊聊springboot elasticsearch healthIndicator

本文主要研究一下springboot elasticsearch healthIndicatorhtml

ElasticsearchHealthIndicatorProperties

spring-boot-actuator-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchHealthIndicatorProperties.javajava

@ConfigurationProperties(prefix = "management.health.elasticsearch",
		ignoreUnknownFields = false)
public class ElasticsearchHealthIndicatorProperties {

	/**
	 * Comma-separated index names.
	 */
	private List<String> indices = new ArrayList<>();

	/**
	 * Time to wait for a response from the cluster.
	 */
	private Duration responseTimeout = Duration.ofMillis(100);

	public List<String> getIndices() {
		return this.indices;
	}

	public void setIndices(List<String> indices) {
		this.indices = indices;
	}

	public Duration getResponseTimeout() {
		return this.responseTimeout;
	}

	public void setResponseTimeout(Duration responseTimeout) {
		this.responseTimeout = responseTimeout;
	}

}
複製代碼
  • ElasticsearchHealthIndicatorProperties提供了indices,responseTimeout兩個配置項

ElasticSearchClientHealthIndicatorAutoConfiguration

spring-boot-actuator-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchClientHealthIndicatorAutoConfiguration.javanode

@Configuration
@ConditionalOnClass(Client.class)
@ConditionalOnBean(Client.class)
@ConditionalOnEnabledHealthIndicator("elasticsearch")
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
@AutoConfigureAfter(ElasticsearchAutoConfiguration.class)
@EnableConfigurationProperties(ElasticsearchHealthIndicatorProperties.class)
public class ElasticSearchClientHealthIndicatorAutoConfiguration extends
		CompositeHealthIndicatorConfiguration<ElasticsearchHealthIndicator, Client> {

	private final Map<String, Client> clients;

	private final ElasticsearchHealthIndicatorProperties properties;

	public ElasticSearchClientHealthIndicatorAutoConfiguration(
			Map<String, Client> clients,
			ElasticsearchHealthIndicatorProperties properties) {
		this.clients = clients;
		this.properties = properties;
	}

	@Bean
	@ConditionalOnMissingBean(name = "elasticsearchHealthIndicator")
	public HealthIndicator elasticsearchHealthIndicator() {
		return createHealthIndicator(this.clients);
	}

	@Override
	protected ElasticsearchHealthIndicator createHealthIndicator(Client client) {
		Duration responseTimeout = this.properties.getResponseTimeout();
		return new ElasticsearchHealthIndicator(client,
				(responseTimeout != null) ? responseTimeout.toMillis() : 100,
				this.properties.getIndices());
	}

}
複製代碼
  • ElasticSearchClientHealthIndicatorAutoConfiguration建立的是ElasticsearchHealthIndicator(elasticsearch),它是經過org.elasticsearch.client.Client去檢測的

ElasticsearchHealthIndicator

spring-boot-actuator-2.1.4.RELEASE-sources.jar!/org/springframework/boot/actuate/elasticsearch/ElasticsearchHealthIndicator.javaspring

public class ElasticsearchHealthIndicator extends AbstractHealthIndicator {

	private static final String[] ALL_INDICES = { "_all" };

	private final Client client;

	private final String[] indices;

	private final long responseTimeout;

	/**
	 * Create a new {@link ElasticsearchHealthIndicator} instance.
	 * @param client the Elasticsearch client
	 * @param responseTimeout the request timeout in milliseconds
	 * @param indices the indices to check
	 */
	public ElasticsearchHealthIndicator(Client client, long responseTimeout,
			List<String> indices) {
		this(client, responseTimeout,
				(indices != null) ? StringUtils.toStringArray(indices) : null);
	}

	/**
	 * Create a new {@link ElasticsearchHealthIndicator} instance.
	 * @param client the Elasticsearch client
	 * @param responseTimeout the request timeout in milliseconds
	 * @param indices the indices to check
	 */
	public ElasticsearchHealthIndicator(Client client, long responseTimeout,
			String... indices) {
		super("Elasticsearch health check failed");
		this.client = client;
		this.responseTimeout = responseTimeout;
		this.indices = indices;
	}

	@Override
	protected void doHealthCheck(Health.Builder builder) throws Exception {
		ClusterHealthRequest request = Requests.clusterHealthRequest(
				ObjectUtils.isEmpty(this.indices) ? ALL_INDICES : this.indices);
		ClusterHealthResponse response = this.client.admin().cluster().health(request)
				.actionGet(this.responseTimeout);
		switch (response.getStatus()) {
		case GREEN:
		case YELLOW:
			builder.up();
			break;
		case RED:
		default:
			builder.down();
			break;
		}
		builder.withDetail("clusterName", response.getClusterName());
		builder.withDetail("numberOfNodes", response.getNumberOfNodes());
		builder.withDetail("numberOfDataNodes", response.getNumberOfDataNodes());
		builder.withDetail("activePrimaryShards", response.getActivePrimaryShards());
		builder.withDetail("activeShards", response.getActiveShards());
		builder.withDetail("relocatingShards", response.getRelocatingShards());
		builder.withDetail("initializingShards", response.getInitializingShards());
		builder.withDetail("unassignedShards", response.getUnassignedShards());
	}

}
複製代碼
  • ElasticsearchHealthIndicator繼承了AbstractHealthIndicator,這裏若是不指定indices的話,默認是ALL_INDICES(_all);doHealthCheck方法使用client.admin().cluster().health(request)來進行請求,以後根據ClusterHealthResponse的狀態來決定是up仍是down,若是是GREEN或YELLOW則返回Status.UP,若是是RED則返回Status.DOWN

ElasticSearchRestHealthIndicatorAutoConfiguration

spring-boot-actuator-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthIndicatorAutoConfiguration.javadocker

@Configuration
@ConditionalOnClass(RestClient.class)
@ConditionalOnBean(RestClient.class)
@ConditionalOnEnabledHealthIndicator("elasticsearch")
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
@AutoConfigureAfter({ RestClientAutoConfiguration.class,
		ElasticSearchClientHealthIndicatorAutoConfiguration.class })
public class ElasticSearchRestHealthIndicatorAutoConfiguration extends
		CompositeHealthIndicatorConfiguration<ElasticsearchRestHealthIndicator, RestClient> {

	private final Map<String, RestClient> clients;

	public ElasticSearchRestHealthIndicatorAutoConfiguration(
			Map<String, RestClient> clients) {
		this.clients = clients;
	}

	@Bean
	@ConditionalOnMissingBean(name = "elasticsearchRestHealthIndicator")
	public HealthIndicator elasticsearchRestHealthIndicator() {
		return createHealthIndicator(this.clients);
	}

	@Override
	protected ElasticsearchRestHealthIndicator createHealthIndicator(RestClient client) {
		return new ElasticsearchRestHealthIndicator(client);
	}

}
複製代碼
  • ElasticSearchRestHealthIndicatorAutoConfiguration建立的是ElasticsearchRestHealthIndicator(elasticsearchRest),它是經過org.elasticsearch.client.RestClient去檢測的

ElasticsearchRestHealthIndicator

spring-boot-actuator-2.1.4.RELEASE-sources.jar!/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.javajson

public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {

	private static final String RED_STATUS = "red";

	private final RestClient client;

	private final JsonParser jsonParser;

	public ElasticsearchRestHealthIndicator(RestClient client) {
		super("Elasticsearch health check failed");
		this.client = client;
		this.jsonParser = JsonParserFactory.getJsonParser();
	}

	@Override
	protected void doHealthCheck(Health.Builder builder) throws Exception {
		Response response = this.client
				.performRequest(new Request("GET", "/_cluster/health/"));
		StatusLine statusLine = response.getStatusLine();
		if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
			builder.down();
			builder.withDetail("statusCode", statusLine.getStatusCode());
			builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
			return;
		}
		try (InputStream inputStream = response.getEntity().getContent()) {
			doHealthCheck(builder,
					StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
		}
	}

	private void doHealthCheck(Health.Builder builder, String json) {
		Map<String, Object> response = this.jsonParser.parseMap(json);
		String status = (String) response.get("status");
		if (RED_STATUS.equals(status)) {
			builder.outOfService();
		}
		else {
			builder.up();
		}
		builder.withDetails(response);
	}

}
複製代碼
  • ElasticsearchRestHealthIndicator繼承了AbstractHealthIndicator,構造器經過JsonParserFactory.getJsonParser()建立了JsonParser
  • doHealthCheck方法經過RestClient.performRequest(new Request("GET", "/_cluster/health/"))進行請求,若是http response status code不是HttpStatus.SC_OK,直接返回Status.DOWN;若是是HttpStatus.SC_OK再進一步解析json判斷
  • 私有的doHealthCheck方法經過jsonParser.parseMap(json)解析返回json爲Map,而後取status字段,若是是red則返回Status.OUT_OF_SERVICE,不然返回Status.UP

GET /_cluster/health/在http response status code爲200的狀況下返回的結構實例以下:springboot

{
  "cluster_name" : "docker-cluster",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 8,
  "active_shards" : 8,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 6,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 57.14285714285714
}
複製代碼

ElasticSearchJestHealthIndicatorAutoConfiguration

spring-boot-actuator-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticSearchJestHealthIndicatorAutoConfiguration.javabash

@Configuration
@ConditionalOnClass(JestClient.class)
@ConditionalOnBean(JestClient.class)
@ConditionalOnEnabledHealthIndicator("elasticsearch")
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
@AutoConfigureAfter({ JestAutoConfiguration.class,
		ElasticSearchClientHealthIndicatorAutoConfiguration.class })
public class ElasticSearchJestHealthIndicatorAutoConfiguration extends
		CompositeHealthIndicatorConfiguration<ElasticsearchJestHealthIndicator, JestClient> {

	private final Map<String, JestClient> clients;

	public ElasticSearchJestHealthIndicatorAutoConfiguration(
			Map<String, JestClient> clients) {
		this.clients = clients;
	}

	@Bean
	@ConditionalOnMissingBean(name = "elasticsearchHealthIndicator")
	public HealthIndicator elasticsearchHealthIndicator() {
		return createHealthIndicator(this.clients);
	}

	@Override
	protected ElasticsearchJestHealthIndicator createHealthIndicator(JestClient client) {
		return new ElasticsearchJestHealthIndicator(client);
	}

}
複製代碼
  • ElasticSearchJestHealthIndicatorAutoConfiguration建立的是ElasticsearchJestHealthIndicator(elasticsearch),它是經過io.searchbox.client.JestClient去檢測的

ElasticsearchJestHealthIndicator

spring-boot-actuator-2.1.4.RELEASE-sources.jar!/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicator.javaelasticsearch

public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {

	private final JestClient jestClient;

	private final JsonParser jsonParser = JsonParserFactory.getJsonParser();

	public ElasticsearchJestHealthIndicator(JestClient jestClient) {
		super("Elasticsearch health check failed");
		this.jestClient = jestClient;
	}

	@Override
	protected void doHealthCheck(Health.Builder builder) throws Exception {
		JestResult healthResult = this.jestClient
				.execute(new io.searchbox.cluster.Health.Builder().build());
		if (healthResult.getResponseCode() != 200 || !healthResult.isSucceeded()) {
			builder.down();
			builder.withDetail("statusCode", healthResult.getResponseCode());
		}
		else {
			Map<String, Object> response = this.jsonParser
					.parseMap(healthResult.getJsonString());
			String status = (String) response.get("status");
			if (status.equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
				builder.outOfService();
			}
			else {
				builder.up();
			}
			builder.withDetails(response);
		}
	}

}
複製代碼
  • ElasticsearchJestHealthIndicator繼承了AbstractHealthIndicator,構造器經過接收JestClient
  • doHealthCheck方法經過jestClient.execute(new io.searchbox.cluster.Health.Builder().build())進行請求,若是http response status code不是200,或者healthResult.isSucceeded()不是true則直接返回Status.DOWN
  • 若是http response status code是200且healthResult.isSucceeded()爲true則再進一步經過jsonParser.parseMap(json)解析返回json爲Map,而後取status字段,若是是io.searchbox.cluster.Health.Status.RED.getKey()則返回Status.OUT_OF_SERVICE,不然返回Status.UP

小結

  • springboot提供了三個elasticsearch的healthIndicator配置,分別是ElasticSearchClientHealthIndicatorAutoConfiguration、ElasticSearchRestHealthIndicatorAutoConfiguration、ElasticSearchJestHealthIndicatorAutoConfiguration
  • ElasticSearchClientHealthIndicatorAutoConfiguration建立的是ElasticsearchHealthIndicator(elasticsearch),它是經過org.elasticsearch.client.Client去檢測的
  • ElasticSearchRestHealthIndicatorAutoConfiguration建立的是ElasticsearchRestHealthIndicator(elasticsearchRest),它是經過org.elasticsearch.client.RestClient去檢測的
  • ElasticSearchJestHealthIndicatorAutoConfiguration建立的是ElasticsearchJestHealthIndicator(elasticsearch),它是經過io.searchbox.client.JestClient去檢測的
  • ElasticsearchHealthIndicatorProperties提供了indices,responseTimeout兩個配置項,對於使用org.elasticsearch.client.Client的若是沒有配置應用使用的indices,則使用ALL_INDICES(_all)去請求;而使用org.elasticsearch.client.RestClient或io.searchbox.client.JestClient的它們請求的是/_cluster/health/這個接口

doc

相關文章
相關標籤/搜索