关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

如何使用Spring Cloud搭建高可用的Elasticsearch集群?详解Elasticsearch的安装与配置及Spring Boot集成的实现(下)

发布时间:2023-06-28 10:00:22

4.使用 Spring Cloud 搭建 Elasticsearch

在 Spring Cloud 微服务中使用 Elasticsearch,需要先搭建一个基于 Spring Cloud 的微服务架构。本文将以 Spring Cloud Eureka 作为服务注册中心,Spring Cloud Config 作为配置中心,Spring Cloud Gateway 作为网关,Spring Cloud Feign 作为服务调用客户端,演示如何搭建一个微服务架构,并在其中使用 Elasticsearch 进行数据存储和检索。

4.1 搭建微服务架构

首先需要创建一个 Spring Boot 项目,作为微服务架构的父项目,命名为 spring-cloud-demo。在该项目的 pom.xml 文件中添加以下依赖:

 org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-openfeign

   

这些依赖分别是 Spring Cloud Eureka、Spring Cloud Config、Spring Cloud Gateway 和 Spring Cloud Feign。

在项目的 application.yml 文件中配置 Eureka、Config 和 Gateway 的相关信息,如下所示:

spring:  application:  name: spring-cloud-demo eureka:  client:  service-url:  defaultZone: http://localhost:8761/eureka/  instance:  instance-id: ${spring.application.name}:${random.value}  prefer-ip-address: true server:  port: 8000 --- spring:  profiles

   

4.2 集成 Elasticsearch

在 Spring Cloud 微服务架构中集成 Elasticsearch,需要分别在每个微服务中添加 Elasticsearch 的相关依赖和配置。

4.2.1 添加 Elasticsearch 依赖

在微服务的 pom.xml 文件中添加 Elasticsearch 的相关依赖,如下所示:

 org.springframework.boot spring-boot-starter-data-elasticsearch org.elasticsearch.client elasticsearch-rest-high-level-client 7.7.0

   

其中,spring-boot-starter-data-elasticsearch 依赖用于集成 Spring Data Elasticsearch,elasticsearch-rest-high-level-client 依赖用于连接 Elasticsearch 服务器。

4.2.2 添加 Elasticsearch 配置

在微服务的 application.yml 文件中添加 Elasticsearch 的连接配置,如下所示:

spring:  data:  elasticsearch:  cluster-name: my-application  cluster-nodes: localhost:9300

   

其中,cluster-name 和 Elasticsearch 中配置的 cluster.name 相对应,cluster-nodes 表示 Elasticsearch 的节点地址和端口号。

4.2.3 使用 Elasticsearch

使用 Spring Data Elasticsearch 进行数据的增删改查操作和使用普通的 Spring Data JPA 操作类似,只需要定义一个实体类,并继承 ElasticsearchRepository 接口即可。例如,在一个微服务中定义一个 Book 实体类和对应的 Repository 接口,如下所示:

@Document(indexName = "book") public class Book {  @Id  private String id;  private String title;  private String author;  // getter 和 setter 方法省略 }  public interface BookRepository extends ElasticsearchRepository{  ListfindByTitle(String title);  ListfindByAuthor(String author); }

   

其中,@Document 注解用于指定 Elasticsearch 中的索引名称,@Id 注解用于指定实体类中的 ID 属性。

定义完实体类和 Repository 接口后,就可以在服务中使用 BookRepository 中的方法进行数据的增删改查操作了。


补充:

为了更好地使用Elasticsearch,我们也可以使用官方提供的高级客户端工具包。

准备工作

在开始使用Elasticsearch高级客户端工具包之前,我们需要安装并启动Elasticsearch服务器。官方网站提供了安装包和文档,你可以按照文档的步骤进行安装和配置。

添加依赖

为了在Spring Boot和Spring Cloud项目中使用Elasticsearch高级客户端工具包,我们需要在项目中添加相关依赖。在Maven项目中,我们可以添加以下依赖:

 org.elasticsearch.client elasticsearch-rest-high-level-client 7.15.2

   

在Gradle项目中,我们可以添加以下依赖:

implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.15.2'

   

配置Elasticsearch客户端

接下来,我们需要在Spring Boot和Spring Cloud项目中配置Elasticsearch客户端。我们可以通过以下方式来创建客户端:

@Bean public RestHighLevelClient restHighLevelClient() {  RestClientBuilder builder = RestClient.builder(  new HttpHost("localhost", 9200, "http"),  new HttpHost("localhost", 9201, "http"));  return new RestHighLevelClient(builder); }

   

在这个例子中,我们创建了一个RestHighLevelClient实例,并使用了两个HttpHost实例来指定Elasticsearch服务器的地址和端口。如果有多个服务器,我们可以通过添加多个HttpHost实例来指定它们的地址和端口。

使用Elasticsearch客户端

现在我们已经成功地配置了Elasticsearch客户端,我们可以使用它来进行数据操作。以下是一个简单的例子:

@Autowired private RestHighLevelClient restHighLevelClient;  public void saveDocument() throws IOException {  IndexRequest indexRequest = new IndexRequest("posts");  indexRequest.id("1");  indexRequest.source("title", "Spring Boot",  "body", "Spring Boot is awesome!");   IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);   System.out.println("Index Response: " + indexResponse); }

   

在这个例子中,我们创建了一个IndexRequest实例,并指定了文档的id、索引名称和字段值。然后我们使用RestHighLevelClient实例来执行索引操作,并得到IndexResponse实例作为结果。


总结

本文介绍了如何使用 Spring Cloud 搭建一个微服务架构,并在其中使用 Elasticsearch 进行数据存储和检索。具体来说,主要分为以下几个步骤:

  1. 在 Elasticsearch 中创建索引和文档类型;
  2. 在 Spring Boot 项目中添加 Elasticsearch 的相关依赖,并配置连接信息;
  3. 使用 Spring Data Elasticsearch 进行数据的增删改查操作;
  4. 在 Spring Cloud 微服务架构中添加 Elasticsearch 的相关依赖和配置;
  5. 在微服务中使用 Spring Data Elasticsearch 进行数据的增删改查操作。

通过本文的介绍,相信读者已经掌握了如何在 Spring Boot 和 Spring Cloud 微服务架构中使用 Elasticsearch 进行数据存储和检索的方法。在实际开发中,还需要根据具体的需求和业务场景进行相应的调整和优化,以实现更好的效果


/template/Home/leiyu/PC/Static