mirror of
https://gitee.com/dromara/easy-es.git
synced 2025-12-06 17:18:57 +08:00
Merge remote-tracking branch 'origin/feature-dynamicDataSource'
# Conflicts: # easy-es-test/src/test/resources/application.yml
This commit is contained in:
commit
2298dac969
@ -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);
|
||||||
|
|
||||||
// 创建代理
|
// 创建代理
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package org.dromara.easyes.test.mapper;
|
package org.dromara.easyes.test.mapper;
|
||||||
|
|
||||||
|
import org.dromara.easyes.annotation.EsDS;
|
||||||
import org.dromara.easyes.core.core.BaseEsMapper;
|
import org.dromara.easyes.core.core.BaseEsMapper;
|
||||||
import org.dromara.easyes.test.entity.Document;
|
import org.dromara.easyes.test.entity.Document;
|
||||||
|
|
||||||
@ -8,5 +9,6 @@ import org.dromara.easyes.test.entity.Document;
|
|||||||
* <p>
|
* <p>
|
||||||
* Copyright © 2021 xpc1024 All Rights Reserved
|
* Copyright © 2021 xpc1024 All Rights Reserved
|
||||||
**/
|
**/
|
||||||
|
@EsDS("ds1")
|
||||||
public interface DocumentMapper extends BaseEsMapper<Document> {
|
public interface DocumentMapper extends BaseEsMapper<Document> {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
easy-es:
|
easy-es:
|
||||||
# enable: true
|
# enable: true
|
||||||
address: 10.20.64.228:9200
|
# address: 10.20.64.228:9200
|
||||||
schema: http
|
# schema: http
|
||||||
# username: elastic
|
# username: elastic
|
||||||
# password: WG7WVmuNMtM4GwNYkyWH
|
# password: WG7WVmuNMtM4GwNYkyWH
|
||||||
keep-alive-millis: 18000
|
keep-alive-millis: 18000
|
||||||
@ -17,6 +17,15 @@ easy-es:
|
|||||||
refresh-policy: immediate
|
refresh-policy: immediate
|
||||||
enable-track-total-hits: true
|
enable-track-total-hits: true
|
||||||
# index-prefix: dev_
|
# index-prefix: dev_
|
||||||
|
dynamic:
|
||||||
|
datasource:
|
||||||
|
ds1:
|
||||||
|
address: 10.20.64.228:9200 # 数据源1
|
||||||
|
ds2:
|
||||||
|
address: 49.234.28.111:9200 # 数据源2
|
||||||
|
#username: '若无可去掉此行'
|
||||||
|
#password: '若无可去掉此行'
|
||||||
|
|
||||||
#logging:
|
#logging:
|
||||||
# level:
|
# level:
|
||||||
# tracer: trace
|
# tracer: trace
|
||||||
Loading…
x
Reference in New Issue
Block a user