From edac6074fb7240903fe953ef0da4008c37744887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Sun, 6 Jul 2025 15:16:46 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20easy-es=20=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/config/EasyEsConfiguration.java | 102 ++++++++++++++++++ .../config/GeneratorConfiguration.java | 27 +++++ 2 files changed, 129 insertions(+) create mode 100644 ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/spring/config/EasyEsConfiguration.java create mode 100644 ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/starter/config/GeneratorConfiguration.java diff --git a/ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/spring/config/EasyEsConfiguration.java b/ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/spring/config/EasyEsConfiguration.java new file mode 100644 index 00000000..4a2f89ac --- /dev/null +++ b/ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/spring/config/EasyEsConfiguration.java @@ -0,0 +1,102 @@ +package org.dromara.easyes.spring.config; + +import lombok.NonNull; +import lombok.Setter; +import org.dromara.easyes.common.constants.BaseEsConstants; +import org.dromara.easyes.common.property.EasyEsDynamicProperties; +import org.dromara.easyes.common.property.EasyEsProperties; +import org.dromara.easyes.common.strategy.AutoProcessIndexStrategy; +import org.dromara.easyes.common.utils.EsClientUtils; +import org.dromara.easyes.core.index.AutoProcessIndexNotSmoothlyStrategy; +import org.dromara.easyes.core.index.AutoProcessIndexSmoothlyStrategy; +import org.dromara.easyes.spring.factory.IndexStrategyFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.util.Assert; + +import java.util.Map; + +/** + * Easy-Es Spring配置类 + * @author MoJie + * @since 2.0 + */ +@ConditionalOnProperty(value = "easy-es.enable", havingValue = "true") +@Configuration +public class EasyEsConfiguration implements InitializingBean, EnvironmentAware { + + private Environment environment; + + @Setter + @Autowired(required = false) + private EasyEsProperties easyEsProperties; + + @Setter + @Autowired(required = false) + private EasyEsDynamicProperties easyEsDynamicProperties; + + @Override + public void setEnvironment(@NonNull Environment environment) { + this.environment = environment; + } + + /** + * 当当前配置类注册为bean完成后触发,校验easy-es配置是否存在, + * 如果easy-es.enable: false, 那么不进行校验和抛出异常 + * 默认情况下引入了easy-es是需要配置的,即easy-es.enable:true + * 如果不需要easy-es,那么自行配置为false + * @author MoJie + */ + @Override + public void afterPropertiesSet() { + Boolean enable = environment.getProperty(BaseEsConstants.ENABLE_PREFIX, Boolean.class, Boolean.TRUE); + if (enable) { + Assert.notNull(this.easyEsProperties, "easyEsProperties must is A bean. easy-es配置类必须给配置一个bean"); + } + } + + @Bean + public IndexStrategyFactory indexStrategyFactory() { + return new IndexStrategyFactory(); + } + + @Bean + public EsClientUtils esClientUtils() { + EsClientUtils esClientUtils = new EsClientUtils(); + if (this.easyEsDynamicProperties == null) { + this.easyEsDynamicProperties = new EasyEsDynamicProperties(); + } + Map datasourceMap = this.easyEsDynamicProperties.getDatasource(); + if (datasourceMap.isEmpty()) { + // 设置默认数据源,兼容不使用多数据源配置场景的老用户使用习惯 + datasourceMap.put(EsClientUtils.DEFAULT_DS, this.easyEsProperties); + } + for (String key : datasourceMap.keySet()) { + EasyEsProperties easyEsConfigProperties = datasourceMap.get(key); + EsClientUtils.registerClient(key, () -> EsClientUtils.buildClient(easyEsConfigProperties)); + } + return esClientUtils; + } + + /** + * 索引策略注册 + * + * @return {@link AutoProcessIndexStrategy} + * @author MoJie + */ + @Bean + public AutoProcessIndexStrategy autoProcessIndexSmoothlyStrategy() { + return new AutoProcessIndexSmoothlyStrategy(); + } + + @Bean + public AutoProcessIndexStrategy autoProcessIndexNotSmoothlyStrategy() { + return new AutoProcessIndexNotSmoothlyStrategy(); + } + +} diff --git a/ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/starter/config/GeneratorConfiguration.java b/ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/starter/config/GeneratorConfiguration.java new file mode 100644 index 00000000..3ab45eab --- /dev/null +++ b/ruoyi-common/ruoyi-common-elasticsearch/src/main/java/org/dromara/easyes/starter/config/GeneratorConfiguration.java @@ -0,0 +1,27 @@ +package org.dromara.easyes.starter.config; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import org.dromara.easyes.core.config.GeneratorConfig; +import org.dromara.easyes.core.toolkit.Generator; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +/** + * 代码生成注册 + * @author MoJie + * @since 2.0 + */ +@ConditionalOnProperty(value = "easy-es.enable", havingValue = "true") +@Component +public class GeneratorConfiguration extends Generator { + + @Autowired + private ElasticsearchClient client; + + @Override + public Boolean generate(GeneratorConfig config) { + super.generateEntity(config, this.client); + return Boolean.TRUE; + } +}