springboot2.0整合redis的发布和订阅
1.Maven引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.redis属性配置
spring.redis.database=
spring.redis.host=127.0.0.1
spring.redis.port=
spring.redis.password=******
server.port=
3.设置监听相关对象
3.1接听对象
RedisReceiver可以是普通类或者继承MessageListener,普通类的写法如下,接收的时候只接收到消息,没有频道名
package com.example.redistest.config; import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component; @Component
public class RedisReceiver { public void receiveMessage(String message) {
// TODO 这里是收到通道的消息之后执行的方法
System.out.println(message);
}
}
继承MessageListener,就能拿到消息体和频道名。
package com.example.redistest.config; import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component; @Component
public class RedisReceiver implements MessageListener { @Override
public void onMessage(Message message, byte[] pattern) {
System.out.println(new String(message.getBody()));
System.out.println(new String(message.getChannel()));
}
}
3.2 配置监听适配器、消息监听容器
container.addMessageListener(listenerAdapter, new PatternTopic("channel:test"));
消息监听容器增加监听的消息,第一个参数是监听适配器,第2个参数是监听的频道。
package com.example.redistest.config; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; @Configuration
@EnableCaching
public class RedisCacheConfig { @Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// 可以添加多个 messageListener,配置不同的交换机
container.addMessageListener(listenerAdapter, new PatternTopic("channel:test"));
return container;
} @Bean
MessageListenerAdapter listenerAdapter(RedisReceiver receiver) {
System.out.println("消息适配器1");
return new MessageListenerAdapter(receiver, "onMessage");
} @Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
} }
3.3 消息发送
package com.example.redistest.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date; @RequestMapping("/redis")
@Controller
public class RedisController { @Autowired
StringRedisTemplate template; /**
* 发布消息
*
* @param id
* @return
*/
@RequestMapping("/sendMessage/{id}")
public String sendMessage(@PathVariable String id) {
for(int i = 1; i <= 5; i++) {
template.convertAndSend("channel:test", String.format("我是消息{%d}号: %tT", i, new Date()));
}
return "";
} }
测试
postman访问http://localhost:5555/redis/sendMessage/1

接收消息后打印

springboot2.0整合redis的发布和订阅的更多相关文章
- SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...
- Springboot2.0整合Redis(注解开发)
一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- SpringBoot2.0整合Redis
Spring Boot2.0在2018年3月份正式发布,相比1.0还是有比较多的改动,例如SpringBoot 自2.0起支持jdk1.8及以上的版本.第三方类库升级.响应式 Spring 编程支持等 ...
- springboot2.0整合redis作为缓存以json格式存储对象
步骤1 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- SpringBoot2.x整合Redis实战 4节课
1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download 2.新手 ...
- redis实现发布(订阅)消息
redis实现发布(订阅)消息 什么是redis的发布订阅(pub/sub)? Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能.基于事件的系统中,Pub/S ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解
笔记 3.SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-bo ...
- 第二篇:SpringBoot2.0整合ActiveMQ
本篇开始将具体介绍SpringBoot如何整合其它项目. 如何创建SpringBoot项目 访问https://start.spring.io/. 依次选择构建工具Maven Project.语言ja ...
- SpringBoot2.0 整合 QuartJob ,实现定时器实时管理
一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...
随机推荐
- Hadoop 服务SYS CPU过高导致宕机问题
最近某hadoop集群多次出现机器宕机,现象为瞬间机器的sys cpu增长至100%,机器无法登录.只能硬件重启,ganglia cpu信息如下: 首先怀疑有用户启动了比较奇葩的job,导致不合理的系 ...
- leetcode-mid-Linked list- 103. Binary Tree Zigzag Level Order Traversal
mycode 99.24% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x) ...
- Linux下查看分区内目录及文件占用空间容量
转载linux下使用 du查看某个文件或目录占用磁盘空间的大小 du -ah --max-depth=1 这个是我想要的结果 a显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘 ...
- Gradle 详解
Gradle简单来说,就是工程的管理,帮我们做了依赖,打包,部署,发布等工作.就像一个管家管理我们的项目,我们只用关心写代码就可以了. 1 gradle-wraaper.properties 主工程的 ...
- selinux 了解2
凡是对内核级, 如selinux的修改, 不只是对软件, 程序的修改, 那么修改之后都要重新启动. 针对windows下的截图, 像linux下的screenshot截图那样设置快捷键 shift+s ...
- AppStore IPv6-only 解决--看我就够了
自2016年6月1日起,苹果要求所有提交App Store的iOS应用必须支持IPv6-only环境,背景也是众所周知的,IPv4地址已基本分配完毕,同时IPv6比IPv4也更加高效,向IPv6过渡是 ...
- 【HTML】<!DOCTYPE html>作用
1.定义: DOCTYPE标签是一种标准通用标记语言的文档类型声明,它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(DTD)来解析文档. <!DOCTYPE> 声明 ...
- Vue引入Jquery和Bootstrap
一.引入jquery包 npm i jquery 二.配置jquery 在webpack.base.conf.js中加载juery插件 所以要配置该文件 三.引入Bootstrap npm i bo ...
- MVC4:ajax Json 应用
1)Json基础 2)Json 字符串和Json对象 3)应用例子 4)JsonHelper 1)Json 基础 JSON中对象通过"{}"来标识,一个"{}" ...
- 011-elasticsearch5.4.3【四】-聚合操作【二】-桶聚合【bucket】过滤、嵌套、反转、分组、排序、范围
一.概述 bucketing(桶)聚合:划分不同的“桶”,将数据分配到不同的“桶”里.非常类似sql中的group语句的含义. metric既可以作用在整个数据集上,也可以作为bucketing的子聚 ...