ElasticSearchClientConfig.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.context.annotation.Bean;
  17. import org.springframework.context.annotation.Configuration;
  18. import javax.net.ssl.SSLContext;
  19. @Configuration
  20. public class ElasticSearchClientConfig {
  21. //配置RestHighLevelClient依赖到spring容器中待用
  22. @Configuration
  23. public class ElasticSearchConfig {
  24. //注入IOC容器
  25. // @Bean
  26. // public ElasticsearchClient elasticsearchClient() throws Exception {
  27. // RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.1.24", 9200, "https"));
  28. // CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  29. // credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "xiaoshi221101"));
  30. // SSLContext sslContext = SSLContextBuilder.create()
  31. // .loadTrustMaterial(new TrustSelfSignedStrategy())
  32. // .build();
  33. // builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()));
  34. // RestClient client =builder.build();
  35. // ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
  36. // return new ElasticsearchClient(transport);
  37. // }
  38. @Bean
  39. public ElasticsearchClient elasticsearchClient() throws Exception {
  40. RestClientBuilder builder = RestClient.builder(new HttpHost("47.101.137.223", 9200, "http"));
  41. RestClient client =builder.build();
  42. ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
  43. return new ElasticsearchClient(transport);
  44. }
  45. }
  46. }