Action: Consider revisiting the entries above or defining a bean of type 'org.elasticsearch.client.transport.TransportClient' in your configuration
接上一篇文章,在使用TransportClient后,直接注入TransportClient,需要使用的方法可以使用了,但是运行后报错了:
A component required a bean of type 'org.elasticsearch.client.transport.TransportClient' that could not be found.
提示:
Action: Consider revisiting the entries above or defining a bean of type 'org.elasticsearch.client.transport.TransportClient' in your configuration
那就肯定是没有向Spring中注入TransportClient的Bean了;
方法如下:
@Component public class ElasticsearchClient implements DisposableBean { private static final Logger logger = LoggerFactory.getLogger(ElasticsearchClient.class); private TransportClient transportClient; @Value("${elasticsearch.cluster.name}") private String clusterName; @Value("${elasticsearch.host}") private String host; @Value("${elasticsearch.port:9200}") private String port; @Bean public TransportClient getTransportClient() throws Exception { Settings settings = Settings.builder().put("cluster.name", clusterName) .put("client.transport.sniff", true) .build(); transportClient = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName(host), Integer.valueOf(port))); logger.info("elasticsearch transportClient 连接成功"); return transportClient; } @Override public void destroy() throws Exception { if (transportClient != null) { transportClient.close(); } } }
当然在application.yaml文件中也要配置相应的信息:
elasticsearch:
cluster:
name:
host:
port: 9200
这里cluster.name和host我都是空,可以自行设置;
另外在pom.xml文件添加了相应的依赖:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> </dependency>
这里版本就根据我的的elasticsearch版本来的,注意这里elasticsearch版本要在7.x及以下。

浙公网安备 33010602011771号