在使用SpringBoot整合Elasticsearch7.x的时候,创建RestHighLevelClient的时候出现了一下问题
具体的配置文件如下
package priv.cwr.es.config;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
/**
* @Description Elasticsearch config
* @Author CWR
* @Date 2021/10/29 15:24
*/
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
/**
* es host
*/
@Value("${elasticsearch.host}")
private String esHost;
/**
* es port
*/
@Value("${elasticsearch.port}")
private String esPort;
/**
* 创建Bean
*
* @return
*/
@Bean
@Override
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(esHost + ":" + esPort)
.build();
return RestClients.create(clientConfiguration).rest();
}
}
?最后排查后发现elasticsearchClient()创建的beanName为elasticsearchClient,但是实际注入的beanName为restHighLevelClient,因为elasticsearchClient()是覆写的抽象方法,所以只需要把引入的restHighLevelClient改为elasticsearchClient即可
?
|