标题1.报错问题
标题2.新建一个配置类
package cn.itcast.hotel.config;import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ElasticSearchClientConfig {@Bean@Qualifier("highLevelClient")public RestHighLevelClient restHighLevelClient() {// RestHighLevelClient highLevelClient = new RestHighLevelClient(// RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));RestHighLevelClient highLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {// 该方法接收一个RequestConfig.Builder对象,对该对象进行修改后然后返回。@Overridepublic RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {return requestConfigBuilder.setConnectTimeout(5000 * 1000) // 连接超时(默认为1秒).setSocketTimeout(6000 * 1000);// 套接字超时(默认为30秒)//更改客户端的超时限制默认30秒现在改为100*1000分钟}}));// 调整最大重试超时时间(默认为30秒).setMaxRetryTimeoutMillis(60000);return highLevelClient;}}
3.注意问题
配置类里面实例的对象会@BeforeEach对象发生冲突,如果存在该对象则需要吧beforeEach代码给注解
/*** 一开始完成该对象的初始化 用完了之后需要销毁 使用AfterEach* 先运行before再运行after*/@BeforeEachvoid setUp() {client = new RestHighLevelClient(RestClient.builder(//如果是多个 则是集群HttpHost.create("http://127.0.0.1:9200")));}@AfterEachvoid tearDown() throws IOException {client.close();}