123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package cn.cslg.pas.common.config;
- import co.elastic.clients.elasticsearch.ElasticsearchClient;
- import co.elastic.clients.json.jackson.JacksonJsonpMapper;
- import co.elastic.clients.transport.ElasticsearchTransport;
- import co.elastic.clients.transport.rest_client.RestClientTransport;
- 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.conn.ssl.NoopHostnameVerifier;
- import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
- import org.apache.http.impl.client.BasicCredentialsProvider;
- import org.apache.http.ssl.SSLContextBuilder;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestClientBuilder;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import javax.net.ssl.SSLContext;
- @Configuration
- public class ElasticSearchClientConfig {
- //配置RestHighLevelClient依赖到spring容器中待用
- @Value("${ES.config}")
- private String config;
- @Configuration
- public class ElasticSearchConfig {
- //注入IOC容器
- // @Bean
- // public ElasticsearchClient elasticsearchClient() throws Exception {
- // RestClientBuilder builder = RestClient.builder(new HttpHost(config, 9200, "http"));
- // CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- // credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "Xiaoshi221101"));
- // SSLContext sslContext = SSLContextBuilder.create()
- // .loadTrustMaterial(new TrustSelfSignedStrategy())
- // .build();
- // builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()));
- // RestClient client =builder.build();
- // ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
- // return new ElasticsearchClient(transport);
- // }
- // @Bean
- // public ElasticsearchClient elasticsearchClient() throws Exception {
- // RestClientBuilder builder = RestClient.builder(new HttpHost("47.101.137.223", 9200, "http"));
- // RestClient client = builder.build();
- // ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
- //
- // return new ElasticsearchClient(transport);
- // }
- @Bean
- public ElasticsearchClient elasticsearchClient() throws Exception {
- RestClientBuilder builder = RestClient.builder(new HttpHost(config, 9200, "http"));
- CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "xiaoshi221101"));
- SSLContext sslContext = SSLContextBuilder.create()
- .loadTrustMaterial(new TrustSelfSignedStrategy())
- .build();
- builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()));
- RestClient client =builder.build();
- ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
- return new ElasticsearchClient(transport);
- }
- }
- }
|