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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 如何使用Spring Cloud搭建高可用的Elasticsearch集群?详解Elasticsearch的安装与配置及Spring Boot集成的实现(下)

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 文件中添加以下依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

这些依赖分别是 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 的相关依赖,如下所示:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.7.0</version>
</dependency>

其中,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<Book, String> {
    List<Book> findByTitle(String title);
    List<Book> findByAuthor(String author);
}

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

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


补充:

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

准备工作

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

添加依赖

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

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.15.2</version>
</dependency>

在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 进行数据存储和检索的方法。在实际开发中,还需要根据具体的需求和业务场景进行相应的调整和优化,以实现更好的效果

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
5天前
|
存储 Java 数据安全/隐私保护
|
2天前
|
Java 开发者 Spring
Spring Boot中的资源文件属性配置
【4月更文挑战第28天】在Spring Boot应用程序中,配置文件是管理应用程序行为的重要组成部分。资源文件属性配置允许开发者在不重新编译代码的情况下,对应用程序进行灵活地配置和调整。本篇博客将介绍Spring Boot中资源文件属性配置的基本概念,并通过实际示例展示如何利用这一功能。
11 1
|
11天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
18 0
|
11天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
36 0
|
12天前
|
XML Java 数据库
在Spring框架中,XML配置事务
在Spring框架中,XML配置事务
|
12天前
|
Java Windows Spring
Spring Boot 3.x 全新的热部署配置方式(IntelliJ IDEA 2023.1)
Spring Boot 3.x 全新的热部署配置方式(IntelliJ IDEA 2023.1)
16 1
|
12天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
30 0
|
12天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
26 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
12天前
|
机器学习/深度学习 运维 Java
江帅帅:Spring Boot 底层级探索系列 02 - 自动配置的底层逻辑
江帅帅:Spring Boot 底层级探索系列 02 - 自动配置的底层逻辑
20 0
|
17天前
|
XML Java 数据格式
Spring IOC—基于XML配置和管理Bean 万字详解(通俗易懂)
Spring 第二节 IOC—基于XML配置和管理Bean 万字详解!。
94 5

热门文章

最新文章