ElasticSearchClientConfig.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.example.xiaoshiweixinback.business.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("es-cn-em93o8856000ho9e7.public.elasticsearch.aliyuncs.com", 9200, "http"));
  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. //
  44. // return new ElasticsearchClient(transport);
  45. // }
  46. // @Bean
  47. // public ElasticsearchClient elasticsearchClient() throws Exception {
  48. // RestClientBuilder builder = RestClient.builder(new HttpHost("47.101.137.223", 9200, "http"));
  49. // CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  50. // credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "xiaoshi221101"));
  51. // SSLContext sslContext = SSLContextBuilder.create()
  52. // .loadTrustMaterial(new TrustSelfSignedStrategy())
  53. // .build();
  54. // builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()));
  55. // RestClient client =builder.build();
  56. // ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
  57. // return new ElasticsearchClient(transport);
  58. // }
  59. }
  60. }