feat:连接服务器es

This commit is contained in:
LiWeiJie 2024-08-27 10:57:08 +08:00
parent 619f992c93
commit 85592627d6

View File

@ -4,8 +4,17 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport; import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport; import co.elastic.clients.transport.rest_client.RestClientTransport;
import java.util.Base64;
import org.apache.http.Header;
import org.apache.http.HttpHost; 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.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -13,8 +22,9 @@ import org.springframework.context.annotation.Configuration;
public class ElasticSearchConfig { public class ElasticSearchConfig {
@Bean @Bean
public ElasticsearchClient elasticsearchClient(){ public ElasticsearchClient elasticsearchClient(){
RestClient client = RestClient.builder(new HttpHost("localhost", 9200,"http")).build(); RestClient client = RestClient.builder(new HttpHost("117.73.12.23", 49200, "http"))
ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper()); .setDefaultHeaders(new Header[]{new BasicHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString("elastic:Y123456a".getBytes()))})
return new ElasticsearchClient(transport); .build();
return new ElasticsearchClient(new RestClientTransport(client,new JacksonJsonpMapper()));
} }
} }