EsClientConfiguration.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package cn.com.taiji.config;
  2. import org.apache.http.HttpHost;
  3. import org.apache.http.auth.AuthScope;
  4. import org.apache.http.auth.UsernamePasswordCredentials;
  5. import org.apache.http.client.CredentialsProvider;
  6. import org.apache.http.impl.client.BasicCredentialsProvider;
  7. import org.elasticsearch.client.RestClient;
  8. import org.elasticsearch.client.RestHighLevelClient;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.util.StringUtils;
  13. /**
  14. * @ClassNmae EsClientConfiguration
  15. * @Description es连接类
  16. * @Author suhh
  17. * @Date2021/9/417:25
  18. * @Version 1.0
  19. **/
  20. @Configuration
  21. public class EsClientConfiguration {
  22. @Value("${taiji.elasticsearch.rest.uris}")
  23. private String[] esUris;
  24. @Value("${taiji.elasticsearch.rest.connection-timeout}")
  25. /**
  26. * 连接超时时间
  27. */
  28. private int connectTimeOut;
  29. @Value("${taiji.elasticsearch.rest.max-connection}")
  30. /**
  31. * 最大连接数
  32. */
  33. private int maxConnection;
  34. @Value("${taiji.elasticsearch.rest.username}")
  35. /**
  36. * 用户名
  37. */
  38. private String userName;
  39. @Value("${taiji.elasticsearch.rest.password}")
  40. /**
  41. * 密码
  42. */
  43. private String password;
  44. @Bean
  45. public RestHighLevelClient client() {
  46. HttpHost[] httpHosts = new HttpHost[esUris.length];
  47. //将地址转换为http主机数组,未配置端口则采用默认9200端口,配置了端口则用配置的端口
  48. for (int i = 0; i < httpHosts.length; i++) {
  49. if (!StringUtils.isEmpty(esUris[i])) {
  50. if (esUris[i].contains(":")) {
  51. String[] uris = esUris[i].split(":");
  52. httpHosts[i] = new HttpHost(uris[0], Integer.parseInt(uris[1]), "http");
  53. } else {
  54. httpHosts[i] = new HttpHost(esUris[i], 9200, "http");
  55. }
  56. }
  57. }
  58. //判断,如果未配置用户名,则进行无用户名密码连接,配置了用户名,则进行用户名密码连接
  59. if (StringUtils.isEmpty(userName)) {
  60. RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHosts));
  61. return client;
  62. } else {
  63. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  64. credentialsProvider.setCredentials(AuthScope.ANY,
  65. //es账号密码
  66. new UsernamePasswordCredentials(userName, password));
  67. RestHighLevelClient client = new RestHighLevelClient(
  68. RestClient.builder(httpHosts)
  69. .setHttpClientConfigCallback((httpClientBuilder) -> {
  70. httpClientBuilder.setMaxConnTotal(maxConnection);
  71. httpClientBuilder.disableAuthCaching();
  72. httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
  73. return httpClientBuilder;
  74. })
  75. .setRequestConfigCallback(builder -> {
  76. builder.setConnectTimeout(connectTimeOut);
  77. return builder;
  78. })
  79. );
  80. return client;
  81. }
  82. }
  83. }