Redis发布订阅(六)

1. 发布订阅命令

1.1. 什么是redis的发布订阅

Redis发布订阅是一种消息通信模式:发布者发布消息,订阅者接收消息。常用于服务间的业务解耦(如登陆时发送短信等)、还有关注系统、任务业务等。

1.2. 订阅发布流程图

发布者(channel是频道)

img

消费者

img

1.3. 发布端
# publish发布消息命令
本机redis_01:0>publish channel1 helloword
"1"
本机redis_01:0>publish channel1 helloword12334
"1"
本机redis_01:0>
1.4. 消费端
# 订阅一个频道
本机redis_01:0> subscribe channel1
1) "subscribe"
2) "channel1"
3) "1"

1) "message" #消息
2) "channel1" #订阅的频道
3) "helloword" #订阅到的消息值:helloword

1) "message"
2) "channel1"
3) "helloword12334" #订阅到的消息值helloword12334
1.5. 常用命令
publish   # 发布消息
subscribe  # 订阅频道
unsubscribe # 取消频道的订阅

2. SpringBoot实现发布订阅

2.1. 订阅端接收数据类
import org.springframework.stereotype.Component;

@Component
public class RedisSubscriptHandler {

    public void receiveMsg(String msg) {
        System.out.println("redis subscript msg = " + msg);
    }

}
2.2. Redis配置类配置接收数据监听
package com.acx.config;

import com.acx.service.RedisSubscriptHandler;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class RedisConfig {

    @Bean
    public RedisMessageListenerContainer listenerContainer(RedisConnectionFactory connectionFactory,
                                                           MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        listenerContainer.addMessageListener(listenerAdapter, new PatternTopic("channel123"));
        return listenerContainer;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(RedisSubscriptHandler handler) {
        return new MessageListenerAdapter(handler, "receiveMsg");
    }

}

2.3. controller模拟消息发布
package com.acx.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis_test/sub_msg")
public class RedisPublishController {

    private static final Logger logger = LoggerFactory.getLogger(RedisPublishController.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("publish")
    public String publishMsg() {
        logger.info("begin request redis publish msg");
        for (int i = 0; i < 10; i++) {
            String msg = "测试redis的发布订阅  序号 = " + i;
            redisTemplate.convertAndSend("channel123", msg);
        }
        return "ok";
    }

}
2.4. 调用最终结果
redis subscript msg = 测试redis的发布订阅  序号 = 0
redis subscript msg = 测试redis的发布订阅  序号 = 1
redis subscript msg = 测试redis的发布订阅  序号 = 2
redis subscript msg = 测试redis的发布订阅  序号 = 3
redis subscript msg = 测试redis的发布订阅  序号 = 4
redis subscript msg = 测试redis的发布订阅  序号 = 5
redis subscript msg = 测试redis的发布订阅  序号 = 6
redis subscript msg = 测试redis的发布订阅  序号 = 7
redis subscript msg = 测试redis的发布订阅  序号 = 8
redis subscript msg = 测试redis的发布订阅  序号 = 9
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值