要在Spring Boot項目中集成Elasticsearch集群,你需要進行以下配置:
pom.xml
文件中添加Elasticsearch的依賴:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
application.properties
或application.yml
文件中配置Elasticsearch連接信息,包括集群節(jié)點、端口等:spring.elasticsearch.rest.uris=http://localhost:9200,http://localhost:9201,http://localhost:9202
或者使用YAML格式的配置:
spring:
elasticsearch:
rest:
uris: http://localhost:9200,http://localhost:9201,http://localhost:9202
application.properties
或application.yml
文件中配置Elasticsearch的連接池相關屬性,例如最大連接數(shù)、最大路由數(shù)等:spring.elasticsearch.rest.max-connections=10
spring.elasticsearch.rest.max-per-route-connections=5
或者使用YAML格式的配置:
spring:
elasticsearch:
rest:
max-connections: 10
max-per-route-connections: 5
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticsearchConfig {
@Bean
public RestHighLevelClient elasticsearchClient() {
return new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
)
);
}
}
將上述代碼放置在一個@Configuration注解的類中,Spring Boot會自動將該類中的@Bean注解的方法注冊為bean。
以上就是在Spring Boot項目中集成Elasticsearch集群的配置步驟。你可以根據(jù)實際需求進行相應的調(diào)整和優(yōu)化。