SpringBoot Redis 消息传输

本指南详细介绍了如何使用Spring Data Redis构建一个发布和订阅Redis消息的应用。首先,你需要一个Redis服务器,然后从Spring Initializr开始创建项目,添加Spring for Redis依赖。接着创建一个Redis消息接收者,实现消息监听器,注册到监听器容器中。通过RedisTemplate发送消息,应用启动后即可进行消息传输。
摘要由CSDN通过智能技术生成

开篇词

该指南将引导你完成使用 Spring Data Redis 发布和订阅 Redis 所发的消息。
 

你将创建的应用

我们将构建一个应用,该应用借助 SpringRedisTemplate 发布字符串消息,并通过 MessageListenerAdapter 使 POJO 订阅该消息。
 

你将需要的工具

如何完成这个指南

像大多数的 Spring 入门指南一样,你可以从头开始并完成每个步骤,也可以绕过你已经熟悉的基本设置步骤。如论哪种方式,你最终都有可以工作的代码。

  • 要从头开始,移步至启动一个 Redis 服务器
  • 要跳过基础,执行以下操作:
    • 下载并解压缩该指南将用到的源代码,或借助 Git 来对其进行克隆操作:git clone https://github.com/spring-guides/gs-messaging-redis.git
    • 切换至 gs-messaging-redis/initial 目录;
    • 或跳转至该指南的从 Spring Initializr 开始

待一切就绪后,可以检查一下 gs-messaging-redis/complete 目录中的代码。

 

启动一个 Redis 服务器

在构建消息传递应用之前,我们需要配置处理接收和发送消息的服务器。

Redis 是一款开源的,BSD 许可的键值数据存储,还附带了消息传递系统。该服务器可从 https://redis.io/download 免费获得。我们可以手动下载它,或如果使用 Mac,则可以通过 Homebrew 来进行下载,方法是在终端窗口中运行以下命令:

brew install redis

解包 Redis 之后,可以通过运行以下命令以默认配置来启动它:

redis-server

我们应该看到类似于以下内容的消息:

[35142] 01 May 14:36:28.939 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[35142] 01 May 14:36:28.940 * Max number of open files set to 10032
                _._
              _.-``__ ''-._
        _.-``    `.  `_.  ''-._           Redis 2.6.12 (00000000/0) 64 bit
    .-`` .-```.  ```\/    _.,_ ''-._
  (    '      ,       .-`  | `,    )     Running in stand alone mode
  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
  |    `-._   `._    /     _.-'    |     PID: 35142
    `-._    `-._  `-./  _.-'    _.-'
  |`-._`-._    `-.__.-'    _.-'_.-'|
  |    `-._`-._        _.-'_.-'    |           https://redis.io
    `-._    `-._`-.__.-'_.-'    _.-'
  |`-._`-._    `-.__.-'    _.-'_.-'|
  |    `-._`-._        _.-'_.-'    |
    `-._    `-._`-.__.-'_.-'    _.-'
        `-._    `-.__.-'    _.-'
            `-._        _.-'
                `-.__.-'

[35142] 01 May 14:36:28.941 # Server started, Redis version 2.6.12
[35142] 01 May 14:36:28.941 * The server is now ready to accept connections on port 6379

 

从 Spring Initializr 开始

对于所有的 Spring 应用来说,你应该从 Spring Initializr 开始。Initializr 提供了一种快速的方法来提取应用程序所需的依赖,并为你完成许多设置。该示例仅需要 Spring for Redis 依赖。下图显示了此示例项目的 Initializr 设置:
Spring Initializr 界面

上图显示了选择 Maven 作为构建工具的 Initializr。你也可以使用 Gradle。它还将 com.examplemessaging-redis 的值分别显示为 Group 和 Artifact。在本示例的其余部分,将用到这些值。

以下清单显示了选择 Maven 时创建的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>messaging-redis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>messaging-redis</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值