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的发布和订阅的更多相关文章

  1. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

  2. Springboot2.0整合Redis(注解开发)

    一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  3. SpringBoot2.0整合Redis

    Spring Boot2.0在2018年3月份正式发布,相比1.0还是有比较多的改动,例如SpringBoot 自2.0起支持jdk1.8及以上的版本.第三方类库升级.响应式 Spring 编程支持等 ...

  4. springboot2.0整合redis作为缓存以json格式存储对象

    步骤1 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  5. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download          2.新手 ...

  6. redis实现发布(订阅)消息

    redis实现发布(订阅)消息 什么是redis的发布订阅(pub/sub)?   Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能.基于事件的系统中,Pub/S ...

  7. 小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 ...

  8. 第二篇:SpringBoot2.0整合ActiveMQ

    本篇开始将具体介绍SpringBoot如何整合其它项目. 如何创建SpringBoot项目 访问https://start.spring.io/. 依次选择构建工具Maven Project.语言ja ...

  9. SpringBoot2.0 整合 QuartJob ,实现定时器实时管理

    一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...

随机推荐

  1. 网络处理器(Network Processor)

    网络处理器(Network Processor,简称NP),又可以称为交换芯片,专用于实现核心交换机高速转发功能. 根据网络处理器会议(Network Processors Conference)的定 ...

  2. Redis 简介,安装,卸载

    一.Redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  3. After laptop installed fedora23

    en_US.UTF-8和 zh_CN.UTF-8 en和zh是语言, US和CN是分地区. 两者的编码UTF-8都差不多.只是用语言环境来表示时间, 数字, 温度等的不同 自定义程序快捷键: 在选中条 ...

  4. python+selenium的frame表单切换

    switch_to.frame()                               切换frame switch_to.default_content()                切 ...

  5. 什么是HIS、PACS、LIS、RIS

    什么是HIS?医院信息系统的定义(HIS)医院信息系统(Hospital Information System,HIS)在国际学术界已公认为新兴的医学信息学(Medical Informatics)的 ...

  6. C++学习笔记(七)--共用体、枚举、typedef

    1.共用体 union其定义与结构体类似:union 类型名{ 成员表列;};声明变量的方法也类似: a. union 类型名{            b. union { c.类型名 变量名; 成员 ...

  7. data plugin for vs2019

    Reporting Service projects for VS 2019https://marketplace.visualstudio.com/items?itemName=ProBITools ...

  8. [Git] 013 远程仓库篇 第零话 使用前的一些配置

    0. 前言 本地仓库和 GitHub 上的远程仓库之间的传输是通过 "SSH" 加密的,所以使用前需要进行一些设置 这回的任务 设置"身份象征" 创建 &quo ...

  9. hdu-1045.fire net(缩点 + 二分匹配)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  10. 对第一个HelloWorld程序的总结:

    /* 注释的作用 :提高了代码的阅读性:调试程序的重要方法 对第一个程序的总结: 创建:创建一个以.java结尾的文件叫做源文件 编译:(javac 源文件名.java) 会生成一个或多个字节码(.c ...