溫馨提示×

springboot如何連接es集群

小億
224
2023-10-13 08:21:31
欄目: 編程語言

要連接一個(gè)ES(Elasticsearch)集群,你可以使用Spring Boot的Elasticsearch集成。首先,你需要添加Elasticsearch的Spring Boot Starter依賴到你的項(xiàng)目中。

你可以在你的pom.xml文件中添加以下依賴:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

接下來,你需要在你的Spring Boot應(yīng)用程序的配置文件中添加ES的連接信息。在application.propertiesapplication.yml中,添加以下配置:

spring.data.elasticsearch.cluster-nodes=host1:port1,host2:port2,...,hostN:portN

或者使用YAML配置:

spring:
data:
elasticsearch:
cluster-nodes: host1:port1,host2:port2,...,hostN:portN

其中,host1:port1,host2:port2,...,hostN:portN是ES集群的節(jié)點(diǎn)列表,用逗號分隔。

最后,在你的Spring Boot應(yīng)用程序中,你可以使用ElasticsearchTemplate或者通過繼承ElasticsearchRepository來操作ES集群。

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

或者:

public interface MyDocumentRepository extends ElasticsearchRepository<MyDocument, String> {
// 自定義查詢方法
}

這樣就可以連接并操作ES集群了。請確保你的應(yīng)用程序能夠訪問到ES集群的節(jié)點(diǎn)。

0