From e0aaf3fc69a6aadd48be9184c463cf57c89df494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E6=98=A5=E9=98=B3?= Date: Thu, 29 Jan 2026 20:29:11 +0800 Subject: [PATCH] =?UTF-8?q?add=20spring=20actuate=20=E9=80=82=E9=85=8D?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easy-es-actuator/pom.xml | 34 ++++++++ ...yElasticsearchHealthAutoConfiguration.java | 25 ++++++ .../ElasticsearchRestHealthIndicator.java | 84 +++++++++++++++++++ .../EasyElasticsearchHealthProperties.java | 21 +++++ .../main/resources/META-INF/spring.factories | 1 + ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../dromara/easyes/core/cache/BaseCache.java | 8 ++ .../easyes/core/kernel/BaseEsMapperImpl.java | 8 ++ easy-es-parent/pom.xml | 6 ++ pom.xml | 7 +- 10 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 easy-es-actuator/pom.xml create mode 100644 easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/EasyElasticsearchHealthAutoConfiguration.java create mode 100644 easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/indicator/ElasticsearchRestHealthIndicator.java create mode 100644 easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/properties/EasyElasticsearchHealthProperties.java create mode 100644 easy-es-actuator/src/main/resources/META-INF/spring.factories create mode 100644 easy-es-actuator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/easy-es-actuator/pom.xml b/easy-es-actuator/pom.xml new file mode 100644 index 0000000..b77c08b --- /dev/null +++ b/easy-es-actuator/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + org.dromara.easy-es + easy-es-parent + 3.0.0 + ../easy-es-parent + + + easy-es-actuator + + + 8 + 8 + UTF-8 + + + + org.springframework.boot + spring-boot-actuator-autoconfigure + + + org.springframework.boot + spring-boot-configuration-processor + + + org.dromara.easy-es + easy-es-core + + + \ No newline at end of file diff --git a/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/EasyElasticsearchHealthAutoConfiguration.java b/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/EasyElasticsearchHealthAutoConfiguration.java new file mode 100644 index 0000000..9839d91 --- /dev/null +++ b/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/EasyElasticsearchHealthAutoConfiguration.java @@ -0,0 +1,25 @@ +package org.dromara.easyes.starter.health; + +import org.dromara.easyes.starter.health.indicator.ElasticsearchRestHealthIndicator; +import org.dromara.easyes.starter.health.properties.EasyElasticsearchHealthProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("org.dromara.easyes.starter.health") +@EnableConfigurationProperties(EasyElasticsearchHealthProperties.class) +public class EasyElasticsearchHealthAutoConfiguration { + + /** + * 心跳检查自动装配 + * @return 心跳检查自动装配 + */ + @Bean("elasticsearchHealthIndicator") + @ConditionalOnProperty(value = "easy-es.health.elasticsearch.enabled",havingValue = "true",matchIfMissing = true) + public ElasticsearchRestHealthIndicator elasticsearchHealthIndicator() { + return new ElasticsearchRestHealthIndicator(); + } +} diff --git a/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/indicator/ElasticsearchRestHealthIndicator.java b/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/indicator/ElasticsearchRestHealthIndicator.java new file mode 100644 index 0000000..015476b --- /dev/null +++ b/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/indicator/ElasticsearchRestHealthIndicator.java @@ -0,0 +1,84 @@ +package org.dromara.easyes.starter.health.indicator; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.elasticsearch._types.HealthStatus; +import co.elastic.clients.elasticsearch.cluster.ElasticsearchClusterClient; +import co.elastic.clients.elasticsearch.cluster.HealthResponse; +import co.elastic.clients.transport.ElasticsearchTransport; +import co.elastic.clients.transport.rest_client.RestClientTransport; +import org.apache.http.HttpHost; +import org.dromara.easyes.core.cache.BaseCache; +import org.dromara.easyes.core.kernel.BaseEsMapper; +import org.dromara.easyes.core.kernel.BaseEsMapperImpl; +import org.elasticsearch.client.Node; +import org.elasticsearch.client.RestClient; +import org.springframework.beans.BeansException; +import org.springframework.boot.actuate.health.AbstractHealthIndicator; +import org.springframework.boot.actuate.health.Health; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.util.CollectionUtils; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * Elasticsearch健康检查指示器 + *

+ */ +public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator implements ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + @Override + protected void doHealthCheck(Health.Builder builder) throws Exception { + Map details = new HashMap<>(); + Map beansOfType = applicationContext.getBeansOfType(BaseEsMapper.class); + if (CollectionUtils.isEmpty(beansOfType)) { + builder.up(); + return; + } + Collection> allImpl = BaseCache.getAllImpl(); + if (CollectionUtils.isEmpty(allImpl)) { + builder.up(); + return; + } + Collection elasticsearchClients = allImpl + .stream() + .map(BaseEsMapperImpl::getClient) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + for (ElasticsearchClient elasticsearchClient : elasticsearchClients) { + ElasticsearchClusterClient cluster = elasticsearchClient.cluster(); + HealthResponse health = cluster.health(); + HealthStatus status = health.status(); + if (status.equals(HealthStatus.Red)) { + ElasticsearchTransport transport = cluster._transport(); + if (transport instanceof RestClientTransport ) { + RestClientTransport restTransport = (RestClientTransport)transport; + RestClient restClient = restTransport.restClient(); + List nodes = restClient.getNodes(); + String clusterName = health.clusterName(); + List message = new ArrayList<>(); + + for (Node node : nodes) { + HttpHost host = node.getHost(); + message.add("node:" + host.getHostName() + ":" + host.getPort()); + } + details.put(clusterName,message); + } + } + } + if (CollectionUtils.isEmpty(details)) { + builder.up(); + return; + } + builder.withDetails(details); + builder.outOfService(); + } +} diff --git a/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/properties/EasyElasticsearchHealthProperties.java b/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/properties/EasyElasticsearchHealthProperties.java new file mode 100644 index 0000000..c146259 --- /dev/null +++ b/easy-es-actuator/src/main/java/org/dromara/easyes/starter/health/properties/EasyElasticsearchHealthProperties.java @@ -0,0 +1,21 @@ +package org.dromara.easyes.starter.health.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "easy-es.health.elasticsearch") +public class EasyElasticsearchHealthProperties { + /** + * 心跳检查是否开启,默认:开启 + */ + private boolean enabled = true; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +} diff --git a/easy-es-actuator/src/main/resources/META-INF/spring.factories b/easy-es-actuator/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..2eea822 --- /dev/null +++ b/easy-es-actuator/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.dromara.easyes.starter.health.EasyElasticsearchHealthAutoConfiguration diff --git a/easy-es-actuator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/easy-es-actuator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..1a31466 --- /dev/null +++ b/easy-es-actuator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +org.dromara.easyes.starter.health.EasyElasticsearchHealthAutoConfiguration \ No newline at end of file diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/cache/BaseCache.java b/easy-es-core/src/main/java/org/dromara/easyes/core/cache/BaseCache.java index c5f5118..87c68b8 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/cache/BaseCache.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/cache/BaseCache.java @@ -176,4 +176,12 @@ public static Object getId(Class entityClass, Object obj) { String idFieldName = EntityInfoHelper.getEntityInfo(entityClass).getKeyProperty(); return getterInvoke(entityClass, idFieldName, obj); } + + /** + * 获取所有BaseEsMapperImpl实例 + * @return 实例集合 + */ + public static Collection> getAllImpl() { + return baseEsMapperInstanceMap.values(); + } } diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/kernel/BaseEsMapperImpl.java b/easy-es-core/src/main/java/org/dromara/easyes/core/kernel/BaseEsMapperImpl.java index 86896c5..31f4200 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/kernel/BaseEsMapperImpl.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/kernel/BaseEsMapperImpl.java @@ -714,6 +714,14 @@ public Integer delete(Wrapper wrapper) { } } + /** + * 获取客户端 + * @return es 客户端 + */ + public ElasticsearchClient getClient() { + return client; + } + /** * 执行批量插入数据 * diff --git a/easy-es-parent/pom.xml b/easy-es-parent/pom.xml index d1ede77..bd8705a 100644 --- a/easy-es-parent/pom.xml +++ b/easy-es-parent/pom.xml @@ -27,6 +27,7 @@ ../easy-es-common ../easy-es-solon-plugin ../easy-es-spring + ../easy-es-actuator @@ -137,6 +138,11 @@ spring-aop ${spring.version} + + org.springframework.boot + spring-boot-actuator-autoconfigure + ${spring-boot.version} + diff --git a/pom.xml b/pom.xml index be95b5d..5f57532 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ easy-es-spring-test easy-es-springboot-sample easy-es-springboot-test - + easy-es-actuator @@ -80,6 +80,11 @@ solon-logging-logback ${solon.version} + + org.springframework.boot + spring-boot-actuator-autoconfigure + ${spring-boot.version} +