ElasticSearchClientConfig.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package cn.cslg.pas.common.config;
  2. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  3. import co.elastic.clients.json.jackson.JacksonJsonpMapper;
  4. import co.elastic.clients.transport.ElasticsearchTransport;
  5. import co.elastic.clients.transport.rest_client.RestClientTransport;
  6. import org.apache.http.HttpHost;
  7. import org.apache.http.auth.AuthScope;
  8. import org.apache.http.auth.UsernamePasswordCredentials;
  9. import org.apache.http.client.CredentialsProvider;
  10. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  11. import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
  12. import org.apache.http.impl.client.BasicCredentialsProvider;
  13. import org.apache.http.ssl.SSLContextBuilder;
  14. import org.elasticsearch.client.RestClient;
  15. import org.elasticsearch.client.RestClientBuilder;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.context.annotation.Bean;
  18. import org.springframework.context.annotation.Configuration;
  19. import javax.net.ssl.SSLContext;
  20. @Configuration
  21. public class ElasticSearchClientConfig {
  22. //配置RestHighLevelClient依赖到spring容器中待用
  23. @Value("${ES.config}")
  24. private String config;
  25. @Configuration
  26. public class ElasticSearchConfig {
  27. //注入IOC容器
  28. // @Bean
  29. // public ElasticsearchClient elasticsearchClient() throws Exception {
  30. // RestClientBuilder builder = RestClient.builder(new HttpHost(config, 9200, "http"));
  31. // CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  32. // credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "Xiaoshi221101"));
  33. // SSLContext sslContext = SSLContextBuilder.create()
  34. // .loadTrustMaterial(new TrustSelfSignedStrategy())
  35. // .build();
  36. // builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()));
  37. // RestClient client =builder.build();
  38. // ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
  39. // return new ElasticsearchClient(transport);
  40. // }
  41. // @Bean
  42. // public ElasticsearchClient elasticsearchClient() throws Exception {
  43. // RestClientBuilder builder = RestClient.builder(new HttpHost("47.101.137.223", 9200, "http"));
  44. // RestClient client = builder.build();
  45. // ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
  46. //
  47. // return new ElasticsearchClient(transport);
  48. // }
  49. @Bean
  50. public ElasticsearchClient elasticsearchClient() throws Exception {
  51. RestClientBuilder builder = RestClient.builder(new HttpHost(config, 9200, "http"));
  52. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  53. credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "Xiaoshi221101"));
  54. SSLContext sslContext = SSLContextBuilder.create()
  55. .loadTrustMaterial(new TrustSelfSignedStrategy())
  56. .build();
  57. builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()));
  58. RestClient client =builder.build();
  59. ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
  60. return new ElasticsearchClient(transport);
  61. }
  62. }
  63. }