要连接ES集群,你可以按照以下步骤进行配置:
pom.xml
文件中添加ES的依赖:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
application.properties
文件中配置ES集群的连接信息,例如:spring.data.elasticsearch.cluster-nodes=host1:port1,host2:port2,host3:port3
ElasticsearchConfig.java
,用于配置ES的连接信息和其他相关配置:import org.elasticsearch.client.RestHighLevelClient;
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;
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("host1:port1", "host2:port2", "host3:port3")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
ElasticsearchTemplate
或ElasticsearchRepository
进行ES操作。现在你的Spring Boot应用程序就可以连接到ES集群了。你可以使用ElasticsearchTemplate
或ElasticsearchRepository
来进行索引、搜索和其他ES操作。