栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+redis实现发布订阅模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+redis实现发布订阅模式

引入依赖

  
      org.springframework.boot
      spring-boot-starter-data-redis
  

自定义监听器,处理订阅后收到的msg

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

public class ConsumerRedisListener implements MessageListener {


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        doBusiness(message);
    }

    
    public void doBusiness(Message message) {
        Object value = redisTemplate.getValueSerializer().deserialize(message.getBody());
        System.out.println("consumer message: " + value);
    }
}

定义一个top常量

public class RedisTopicConst {

    public static final String  SCHEDULE_TOPIC = "schedule-topic";

}

配置类

import com.xfr.consts.RedisTopicConst;
import com.xfr.listener.ConsumerRedisListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;


@Configuration
public class RedisConfig {


    
    @Autowired
    private LettuceConnectionFactory lettuceConnectionFactory;

    
    @Bean
    public ConsumerRedisListener consumerRedis() {
        return new ConsumerRedisListener();
    }

    
    @Bean
    public ChannelTopic topic() {
        return new ChannelTopic(RedisTopicConst.SCHEDULE_TOPIC);
    }

    
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(lettuceConnectionFactory);
        container.addMessageListener(consumerRedis(),topic());
        return container;

    }
}

测试一下,我这里是通过get请求跑接口测试的,也可以使用junit测试

import com.xfr.consts.RedisTopicConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("test")
    public String testRedisPublishSubscribe(){
        // 发布msg
        redisTemplate.convertAndSend(RedisTopicConst.SCHEDULE_TOPIC, "5");
        return "success";
    }

}

最后看到控制台打印如下:
和我们自定义监听器那里对应起来

使用redis和mq做发布订阅模式的区别:

redis: 轻量级,低延迟,高并发,低可靠性;
rabbitmq:重量级,高可靠,异步,不保证实时;
具体看实际业务进行选择

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721803.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号