1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package cn.com.taiji.config;
- import org.apache.http.HttpHost;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.CredentialsProvider;
- import org.apache.http.impl.client.BasicCredentialsProvider;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.util.StringUtils;
- /**
- * @ClassNmae EsClientConfiguration
- * @Description es连接类
- * @Author suhh
- * @Date2021/9/417:25
- * @Version 1.0
- **/
- @Configuration
- public class EsClientConfiguration {
- @Value("${taiji.elasticsearch.rest.uris}")
- private String[] esUris;
- @Value("${taiji.elasticsearch.rest.connection-timeout}")
- /**
- * 连接超时时间
- */
- private int connectTimeOut;
- @Value("${taiji.elasticsearch.rest.max-connection}")
- /**
- * 最大连接数
- */
- private int maxConnection;
- @Value("${taiji.elasticsearch.rest.username}")
- /**
- * 用户名
- */
- private String userName;
- @Value("${taiji.elasticsearch.rest.password}")
- /**
- * 密码
- */
- private String password;
- @Bean
- public RestHighLevelClient client() {
- HttpHost[] httpHosts = new HttpHost[esUris.length];
- //将地址转换为http主机数组,未配置端口则采用默认9200端口,配置了端口则用配置的端口
- for (int i = 0; i < httpHosts.length; i++) {
- if (!StringUtils.isEmpty(esUris[i])) {
- if (esUris[i].contains(":")) {
- String[] uris = esUris[i].split(":");
- httpHosts[i] = new HttpHost(uris[0], Integer.parseInt(uris[1]), "http");
- } else {
- httpHosts[i] = new HttpHost(esUris[i], 9200, "http");
- }
- }
- }
- //判断,如果未配置用户名,则进行无用户名密码连接,配置了用户名,则进行用户名密码连接
- if (StringUtils.isEmpty(userName)) {
- RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHosts));
- return client;
- } else {
- final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(AuthScope.ANY,
- //es账号密码
- new UsernamePasswordCredentials(userName, password));
- RestHighLevelClient client = new RestHighLevelClient(
- RestClient.builder(httpHosts)
- .setHttpClientConfigCallback((httpClientBuilder) -> {
- httpClientBuilder.setMaxConnTotal(maxConnection);
- httpClientBuilder.disableAuthCaching();
- httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
- return httpClientBuilder;
- })
- .setRequestConfigCallback(builder -> {
- builder.setConnectTimeout(connectTimeOut);
- return builder;
- })
- );
- return client;
- }
- }
- }
|