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

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

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
存储 JSON Java
643 0
|
6月前
|
IDE Ubuntu Java
在Ubuntu18.04安装兼容JDK 8的Eclipse集成开发环境的指南。
完成以上步骤后,您将在Ubuntu 18.04系统上成功安装并配置了Eclipse IDE,它将与JDK 8兼容,可以开始进行Java开发工作。如果遇到任何问题,请确保每一步骤都正确执行,并检查是否所有路径都与您的具体情况相匹配。
277 11
|
6月前
|
物联网 Linux 开发者
快速部署自己私有MQTT-Broker-下载安装到运行不到一分钟,快速简单且易于集成到自己项目中
本文给物联网开发的朋友推荐的是GMQT,让物联网开发者快速拥有合适自己的MQTT-Broker,本文从下载程序到安装部署手把手教大家安装用上私有化MQTT服务器。
1739 5
|
10月前
|
消息中间件 存储 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
本教程介绍ActiveMQ的安装与基本使用。首先从官网下载apache-activemq-5.15.3版本,解压后即可完成安装,非常便捷。启动时进入解压目录下的bin文件夹,根据系统选择win32或win64,运行activemq.bat启动服务。通过浏览器访问`http://127.0.0.1:8161/admin/`可进入管理界面,默认用户名密码为admin/admin。ActiveMQ支持两种消息模式:点对点(Queue)和发布/订阅(Topic)。前者确保每条消息仅被一个消费者消费,后者允许多个消费者同时接收相同消息。
380 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
|
10月前
|
NoSQL Java Redis
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 安装
本教程介绍在 VMware 虚拟机(CentOS 7)或阿里云服务器中安装 Redis 的过程,包括安装 gcc 编译环境、下载 Redis(官网或 wget)、解压安装、修改配置文件(如 bind、daemonize、requirepass 等设置)、启动 Redis 服务及测试客户端连接。通过 set 和 get 命令验证安装是否成功。适用于初学者快速上手 Redis 部署。
320 0
【Azure Function】Function App和Powershell 集成问题, 如何安装PowerShell的依赖模块
【Azure Function】Function App和Powershell 集成问题, 如何安装PowerShell的依赖模块
159 0
|
存储 JavaScript 前端开发
Vue中通过集成Quill富文本编辑器实现公告的发布。Vue项目中vue-quill-editor的安装与使用【实战开发应用】
文章展示了在Vue项目中通过集成Quill富文本编辑器实现公告功能的完整开发过程,包括前端的公告发布、修改、删除操作以及后端的数据存储和处理逻辑。
Vue中通过集成Quill富文本编辑器实现公告的发布。Vue项目中vue-quill-editor的安装与使用【实战开发应用】
|
前端开发 Java Maven
【前端学java】全网最详细的maven安装与IDEA集成教程!
【8月更文挑战第12天】全网最详细的maven安装与IDEA集成教程!
484 2
【前端学java】全网最详细的maven安装与IDEA集成教程!
|
缓存 NoSQL Java
Redis Spring配置集群
【7月更文挑战第5天】
1098 10
|
前端开发 JavaScript
vue3【实战】创建项目、创建并提交代码到远程仓库,安装 SASS, 清除浏览器默认样式 reset-css, 清除模板代码,提升开发效率的必要集成
vue3【实战】创建项目、创建并提交代码到远程仓库,安装 SASS, 清除浏览器默认样式 reset-css, 清除模板代码,提升开发效率的必要集成
262 0

热门文章

最新文章