本文介绍: 在日常开发我们很多时候用到发布/订阅这种模式例如常用的一些消息中间件就会有这种模式,redis中也提供了这种模式,让我们来看下吧。redis发布订阅push/sub)是一种消息通信模式:发布者(push发布消息订阅者(sub接收消息客户端可以订阅任意数量的频道。

在日常开发我们很多时候用到发布/订阅这种模式,例如常用的一些消息中间件就会有这种模式,redis中也提供了这种模式,让我们来看下吧。

redis发布订阅push/sub)是一种消息通信模式:发布者(push)发布消息订阅者(sub接收消息

客户可以订阅任意数量的频道。

1、发布订阅流程

1、客户端订阅消息频道

2、发布者将消息发布到频道

3、频道将消息发送给订阅的客户

2、发布/订阅命令

2.1 订阅

订阅命令subscribe/psubscribe


less

复制代码

//订阅一个多个频道 subscribe channel1 channel2 ... //订阅一个多个符合给定模式的频道,以"*"作为匹配符,如:it*则表示匹配it开头的所有频道 psubscribe pattern [pattern ...]

2.2 发布

发布命令publish


shell

复制代码

//将msg发送指定的频道 publish channel1 msg

切换订阅窗口,可看到接收到的信息

3、SpringBoot实现redis发布订阅

SpringBoot我们常用的开发框架,也会经常用到redis做各种数据缓存,而redis的发布订阅也在日常开发中常用到

3.1 引入redis


xml

复制代码

<dependency&gt; <groupId&gt;org.springframework.boot</groupId&gt; <artifactId&gt;spring-boot-starter-data-redis</artifactId&gt; </dependency&gt;

3.2 配置消息监听

3.2.1 实现MessageListener接口


less

复制代码

/** * @author: jiangjs * @description: redis消息监听类 * @date: 2023/4/7 11:10 **/ @Slf4j @Component public class RedisMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] pattern) { byte[] body = message.getBody(); byte[] channel = message.getChannel(); log.info("------频道名称------:"+new String(channel)); log.info("------消息内容------:"+new String(body)); } }

redis的start类中提供的MessageListener接口中的onMessage方法可以接收用户发布的信息我们获取信息后就可以编写自己业务

3.2.2 创建MessageListenerAdapter适配的类


typescript

复制代码

/** * @author: jiangjs * @description: * @date: 2023/4/7 14:32 **/ @Slf4j @Component public class MessageReceiver { public void receive(String message,String channel) { log.info("------频道名称------:"+ channel); log.info("------消息内容------:"+ message); } }

3.3 redis配置类,并监听渠道

配置文件配置订阅渠道信息


yaml

复制代码

#redis订阅频道 redis: channel: jiashn,lovejiashn

3.3.1 监听MessageListener


typescript

复制代码

@Configuration public class RedisConfig { @Value("${redis.channel}") private String channel; @Bean public RedisMessageListenerContainer container(RedisConnectionFactory factory, RedisMessageListener listener){ List<String> channels = Arrays.asList(StringUtils.split(channel, ",")); List<ChannelTopic> topics = new ArrayList<>(channels.size()); channels.forEach(cha -> topics.add(new ChannelTopic(cha))); RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(factory); //订阅频道 container.addMessageListener(listener,topics); return container; } }

channel配置文件配置的渠道,创建container方法参数包含第二步中的监听器,将监听器与频道进行绑定RedisMessageListenerContainer redis消息监听容器可以监听器与频道进行绑定完成订阅频道。

3.3.2 监听MessageListenerAdpater


typescript

复制代码

/** * @author: jiangjs * @description: redis配置类 * @date: 2023/4/7 11:16 **/ @Configuration public class RedisConfig { @Value("${redis.channel}") private String channel; @Bean public MessageListenerAdapter adapter(MessageReceiver receiver){ return new MessageListenerAdapter(receiver, "receive"); } @Bean public RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter adapter){ List<String> channels = Arrays.asList(StringUtils.split(channel, ",")); List<ChannelTopic> topics = new ArrayList<>(channels.size()); channels.forEach(cha -> topics.add(new ChannelTopic(cha))); RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(factory); //订阅频道 container.addMessageListener(adapter,topics); return container; } }

redis配置里面创建MessageListenerAdapter通过构造函数创建需要绑定监听类(如:MessageReceiver)及监听的方法(如:MessageReceiver中的receive方法),然后通过RedisMessageListenerContainer进行频道订阅。

3.4 发布信息


less

复制代码

@Autowired private RedisTemplate<String,String> template; @GetMapping("/send/{channel}/{msg}") public ResultUtil<String> sendMessage(@PathVariable("channel") String channel, @PathVariable("msg") String msg){ template.convertAndSend(channel,msg); return ResultUtil.success(); }

3.5 执行结果

浏览器访问接口地址,如:http://localhost:8000/send/jiashn/loveQueena 执行结果如下

执行结果我们看到MessageReceiver类中receive接收到了渠道/信息,实现了消息的发布/订阅。

上述就是redis的消息的发布/订阅,谢谢大家,希望对大家有所帮助,谢谢!

作者:抢老婆酸奶的小肥仔
链接https://juejin.cn/post/7233765235953647672

原文地址:https://blog.csdn.net/BASK2311/article/details/130726426

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_37960.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注