mirror of
https://gitee.com/dromara/easy-es.git
synced 2025-12-07 09:39:04 +08:00
v2.0.0-beta5
多数据源代码合并及优化
This commit is contained in:
parent
7839bed247
commit
ed4ee24fd7
@ -0,0 +1,19 @@
|
|||||||
|
package org.dromara.easyes.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lyy
|
||||||
|
*/
|
||||||
|
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EsDS {
|
||||||
|
/**
|
||||||
|
* 数据源名称
|
||||||
|
*
|
||||||
|
* @return 数据源名称
|
||||||
|
*/
|
||||||
|
String value();
|
||||||
|
}
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
package org.dromara.easyes.starter.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lyy
|
||||||
|
* @date 2023年03月14日 15:57
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "easy-es.dynamic")
|
||||||
|
public class DynamicEsProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置多动态数据源key datasource id
|
||||||
|
*/
|
||||||
|
private Map<String, EasyEsConfigProperties> datasource = new HashMap<>();
|
||||||
|
}
|
||||||
@ -10,15 +10,12 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
|
|||||||
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
|
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
|
||||||
import org.apache.http.ssl.SSLContextBuilder;
|
import org.apache.http.ssl.SSLContextBuilder;
|
||||||
import org.dromara.easyes.common.enums.SchemaEnum;
|
import org.dromara.easyes.common.enums.SchemaEnum;
|
||||||
import org.dromara.easyes.common.utils.ExceptionUtils;
|
import org.dromara.easyes.common.utils.*;
|
||||||
import org.dromara.easyes.common.utils.LogUtils;
|
|
||||||
import org.dromara.easyes.common.utils.StringUtils;
|
|
||||||
import org.elasticsearch.client.RestClient;
|
import org.elasticsearch.client.RestClient;
|
||||||
import org.elasticsearch.client.RestClientBuilder;
|
import org.elasticsearch.client.RestClientBuilder;
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
@ -26,12 +23,10 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static org.dromara.easyes.common.constants.BaseEsConstants.*;
|
import static org.dromara.easyes.common.constants.BaseEsConstants.*;
|
||||||
|
import static org.dromara.easyes.common.utils.RestHighLevelClientUtils.DEFAULT_DS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* es自动配置
|
* es自动配置
|
||||||
@ -40,12 +35,16 @@ import static org.dromara.easyes.common.constants.BaseEsConstants.*;
|
|||||||
**/
|
**/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(RestHighLevelClient.class)
|
@ConditionalOnClass(RestHighLevelClient.class)
|
||||||
@EnableConfigurationProperties(EasyEsConfigProperties.class)
|
@EnableConfigurationProperties(value = {DynamicEsProperties.class, EasyEsConfigProperties.class})
|
||||||
@ConditionalOnExpression("'${easy-es.address:x}'!='x'")
|
//@ConditionalOnExpression("'${easy-es.address:x}'!='x'")
|
||||||
@ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true)
|
@ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true)
|
||||||
public class EsAutoConfiguration {
|
public class EsAutoConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
private EasyEsConfigProperties easyEsConfigProperties;
|
private EasyEsConfigProperties easyEsConfigProperties;
|
||||||
|
@Autowired
|
||||||
|
private DynamicEsProperties dynamicEsProperties;
|
||||||
|
|
||||||
|
private final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 装配RestHighLevelClient
|
* 装配RestHighLevelClient
|
||||||
@ -55,6 +54,26 @@ public class EsAutoConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public RestHighLevelClient restHighLevelClient() {
|
public RestHighLevelClient restHighLevelClient() {
|
||||||
|
return restHighLevelClient(easyEsConfigProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestHighLevelClientUtils restHighLevelClientUtils() {
|
||||||
|
RestHighLevelClientUtils restHighLevelClientUtils = new RestHighLevelClientUtils();
|
||||||
|
Map<String, EasyEsConfigProperties> datasourceMap = dynamicEsProperties.getDatasource();
|
||||||
|
if (CollectionUtils.isEmpty(datasourceMap)) {
|
||||||
|
// 设置默认数据源,兼容不使用多数据源配置场景的老用户使用习惯
|
||||||
|
datasourceMap.put(DEFAULT_DS, easyEsConfigProperties);
|
||||||
|
}
|
||||||
|
for (String key : datasourceMap.keySet()) {
|
||||||
|
EasyEsConfigProperties easyEsConfigProperties = datasourceMap.get(key);
|
||||||
|
RestHighLevelClientUtils.registerRestHighLevelClient(key, restHighLevelClient(easyEsConfigProperties));
|
||||||
|
}
|
||||||
|
return restHighLevelClientUtils;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private RestHighLevelClient restHighLevelClient(EasyEsConfigProperties easyEsConfigProperties) {
|
||||||
// 处理地址
|
// 处理地址
|
||||||
String address = easyEsConfigProperties.getAddress();
|
String address = easyEsConfigProperties.getAddress();
|
||||||
if (StringUtils.isEmpty(address)) {
|
if (StringUtils.isEmpty(address)) {
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
package org.dromara.easyes.starter.register;
|
package org.dromara.easyes.starter.register;
|
||||||
|
|
||||||
|
import org.dromara.easyes.annotation.EsDS;
|
||||||
import org.dromara.easyes.annotation.Intercepts;
|
import org.dromara.easyes.annotation.Intercepts;
|
||||||
import org.dromara.easyes.annotation.rely.DefaultChildClass;
|
import org.dromara.easyes.annotation.rely.DefaultChildClass;
|
||||||
import org.dromara.easyes.common.enums.ProcessIndexStrategyEnum;
|
import org.dromara.easyes.common.enums.ProcessIndexStrategyEnum;
|
||||||
import org.dromara.easyes.common.utils.LogUtils;
|
import org.dromara.easyes.common.utils.LogUtils;
|
||||||
|
import org.dromara.easyes.common.utils.RestHighLevelClientUtils;
|
||||||
import org.dromara.easyes.common.utils.TypeUtils;
|
import org.dromara.easyes.common.utils.TypeUtils;
|
||||||
import org.dromara.easyes.core.biz.EntityInfo;
|
import org.dromara.easyes.core.biz.EntityInfo;
|
||||||
import org.dromara.easyes.core.cache.BaseCache;
|
import org.dromara.easyes.core.cache.BaseCache;
|
||||||
@ -26,6 +28,8 @@ import java.lang.reflect.Proxy;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.dromara.easyes.common.utils.RestHighLevelClientUtils.DEFAULT_DS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代理类
|
* 代理类
|
||||||
* <p>
|
* <p>
|
||||||
@ -35,7 +39,8 @@ public class MapperFactoryBean<T> implements FactoryBean<T> {
|
|||||||
private Class<T> mapperInterface;
|
private Class<T> mapperInterface;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestHighLevelClient client;
|
private RestHighLevelClientUtils restHighLevelClientUtils;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
@ -63,6 +68,10 @@ public class MapperFactoryBean<T> implements FactoryBean<T> {
|
|||||||
|
|
||||||
// 初始化缓存
|
// 初始化缓存
|
||||||
GlobalConfigCache.setGlobalConfig(esConfigProperties.getGlobalConfig());
|
GlobalConfigCache.setGlobalConfig(esConfigProperties.getGlobalConfig());
|
||||||
|
|
||||||
|
//获取动态数据源 若未配置多数据源,则使用默认数据源
|
||||||
|
String restHighLevelClientId = Optional.ofNullable(mapperInterface.getAnnotation(EsDS.class)).map(EsDS::value).orElse(DEFAULT_DS);
|
||||||
|
RestHighLevelClient client = restHighLevelClientUtils.getClient(restHighLevelClientId);
|
||||||
BaseCache.initCache(mapperInterface, entityClass, client);
|
BaseCache.initCache(mapperInterface, entityClass, client);
|
||||||
|
|
||||||
// 创建代理
|
// 创建代理
|
||||||
@ -83,9 +92,9 @@ public class MapperFactoryBean<T> implements FactoryBean<T> {
|
|||||||
|
|
||||||
// 将子文档索引激活为父文档索引
|
// 将子文档索引激活为父文档索引
|
||||||
if (!DefaultChildClass.class.equals(entityInfo.getChildClass())) {
|
if (!DefaultChildClass.class.equals(entityInfo.getChildClass())) {
|
||||||
Optional.ofNullable(entityInfo.getChildClass())
|
Optional.ofNullable(entityInfo.getChildClass())
|
||||||
.flatMap(childClass -> Optional.ofNullable(EntityInfoHelper.getEntityInfo(childClass)))
|
.flatMap(childClass -> Optional.ofNullable(EntityInfoHelper.getEntityInfo(childClass)))
|
||||||
.ifPresent(childEntityInfo -> childEntityInfo.setIndexName(entityInfo.getIndexName()));
|
.ifPresent(childEntityInfo -> childEntityInfo.setIndexName(entityInfo.getIndexName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -0,0 +1,44 @@
|
|||||||
|
package org.dromara.easyes.common.utils;
|
||||||
|
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lyy
|
||||||
|
* @date 2023年03月14日 16:05
|
||||||
|
*/
|
||||||
|
public class RestHighLevelClientUtils {
|
||||||
|
|
||||||
|
public static final String DEFAULT_DS = "DEFAULT_DS";
|
||||||
|
|
||||||
|
private static Map<String, RestHighLevelClient> restHighLevelClientMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public RestHighLevelClientUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RestHighLevelClient getRestHighLevelClient(String restHighLevelClientId) {
|
||||||
|
if (DEFAULT_DS.equals(restHighLevelClientId)) {
|
||||||
|
return restHighLevelClientMap.values()
|
||||||
|
.stream()
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow(() -> ExceptionUtils.eee("找不到RestHighLevelClient,restHighLevelClientId:%s", restHighLevelClientId));
|
||||||
|
}
|
||||||
|
RestHighLevelClient restHighLevelClient = restHighLevelClientMap.get(restHighLevelClientId);
|
||||||
|
if (restHighLevelClient == null) {
|
||||||
|
ExceptionUtils.eee("找不到RestHighLevelClient,restHighLevelClientId:%s", restHighLevelClientId);
|
||||||
|
|
||||||
|
}
|
||||||
|
return restHighLevelClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RestHighLevelClient getClient(String restHighLevelClientId) {
|
||||||
|
return RestHighLevelClientUtils.getRestHighLevelClient(restHighLevelClientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void registerRestHighLevelClient(String restHighLevelClientId, RestHighLevelClient restHighLevelClient) {
|
||||||
|
RestHighLevelClientUtils.restHighLevelClientMap.putIfAbsent(restHighLevelClientId, restHighLevelClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user