ElasticSearchProperties 配置类,可以配置在中心中
public class ElasticSearchProperties {
/**
* 连接超时时间(毫秒)
*/
private Integer connectTimeout = 3000;
/**
* socket 超时时间
*/
private Integer socketTimeout = 30000;
/**
* 连接请求超时时间
*/
private Integer connectionRequestTimeout = 500;
/**
* keepAlive策略时间
*/
private Integer keepAliveStrategy = 60 * 1000;
}
EsClientFactory es客户端工厂
/**
* @author yangwenxin
* @Date 2022/5/13
* @Description
*/
@Slf4j
public class EsClientFactory {
/**
* 创建es客户端
*
* @param properties 配置信息
* @param address 地址, 示例:127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
* @param userName 用户名
* @param password 密码
* @return
*/
public static RestHighLevelClient createRestHighLevelClient(ElasticSearchProperties properties, String address,
String userName, String password) {
//ElasticSearch 连接地址地址
HttpHost[] httpHosts = getElasticSearchHttpHosts(address);
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts).setRequestConfigCallback(requestConfigBuilder -> {
//设置连接超时时间
requestConfigBuilder.setConnectTimeout(properties.getConnectTimeout());
requestConfigBuilder.setSocketTimeout(properties.getSocketTimeout());
requestConfigBuilder.setConnectionRequestTimeout(properties.getConnectionRequestTimeout());
return requestConfigBuilder;
}).setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.disableAuthCaching();
httpClientBuilder.setKeepAliveStrategy((response, context) -> properties.getKeepAliveStrategy());
//设置账密
return getHttpAsyncClientBuilder(httpClientBuilder, userName, password);
});
log.info("init esClient success, address : {}", address);
return new RestHighLevelClient(restClientBuilder);
}
/**
* ElasticSearch 连接地址
* 多个逗号分隔
* 示例:127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
*/
private static HttpHost[] getElasticSearchHttpHosts(String address) {
String[] hosts = address.split(",");
HttpHost[] httpHosts = new HttpHost[hosts.length];
for (int i = 0; i < httpHosts.length; i++) {
String host = hosts[i];
host = host.replaceAll("http://", "").replaceAll("https://", "");
httpHosts[i] = new HttpHost(host.split(":")[0], Integer.parseInt(host.split(":")[1]), "http");
}
return httpHosts;
}
private static HttpAsyncClientBuilder getHttpAsyncClientBuilder(HttpAsyncClientBuilder httpClientBuilder, String username, String password) {
if (StringUtils.isBlank(username) || StringUtils.isEmpty(password)) {
return httpClientBuilder;
}
//账密设置
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//es账号密码(一般使用,用户elastic)
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpClientBuilder;
}
}
ElasticSearchClientService es客户端初始化,从数据库中读取信息初始化客户端。其中DataSourceConfig就是配置了一些es链接的信息,账号密码地址等。
/**
* @author yangwenxin
* @Date 2022/5/13
* @Description
*/
@Service
@Slf4j
public class ElasticSearchClientService {
@Autowired
private ElasticSearchProperties elasticSearchProperties;
private Map<String, EsConfig> esClientMap = new HashMap<>();
@PostConstruct
public void init() {
initEsClientMap();