要在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连接信息,包括集群节点、端口等: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的连接池相关属性,例如最大连接数、最大路由数等: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集群的配置步骤。你可以根据实际需求进行相应的调整和优化。