【Redis】基于redis实现订阅发布

背景

业务发展过程中,希望做到异步解耦,但是又不想引入MQ中间件,在中小型服务中,就可以考虑使用redis自带的订阅发布来解决这个问题。使用 Redis 实现消息的订阅和发布时,可以通过 Spring Boot 集成 Redis 来方便地实现。

引入redis依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置 Redis

application.properties 文件中,添加 Redis 配置:

properties 复制代码
spring.redis.host=localhost
spring.redis.port=6379

编写代码

  1. Redis 配置

    创建一个配置类来配置 Redis 的连接工厂和监听器:

    java 复制代码
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.listener.ChannelTopic;
    import org.springframework.data.redis.listener.RedisMessageListenerContainer;
    import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
    import org.springframework.data.redis.core.StringRedisTemplate;
    
    @Configuration
    public class RedisConfig {
    
        @Bean
        public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                       MessageListenerAdapter listenerAdapter) {
            RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            container.setConnectionFactory(connectionFactory);
            container.addMessageListener(listenerAdapter, topic());
            return container;
        }
    
        @Bean
        public MessageListenerAdapter listenerAdapter(RedisMessageSubscriber subscriber) {
            return new MessageListenerAdapter(subscriber, "onMessage");
        }
    
        @Bean
        public ChannelTopic topic() {
            return new ChannelTopic("messageQueue");
        }
    
        @Bean
        public StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
            return new StringRedisTemplate(connectionFactory);
        }
    }
  2. 创建消息订阅者

    编写一个类来处理收到的消息:

    java 复制代码
    import org.springframework.stereotype.Service;
    
    @Service
    public class RedisMessageSubscriber {
    
        public void onMessage(String message, String channel) {
            System.out.println("Received message: " + message + " from channel: " + channel);
        }
    }
  3. 创建消息发布者

    编写一个发布者通过 Redis 模板发送消息:

    java 复制代码
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.listener.ChannelTopic;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MessagePublisher {
    
        @Autowired
        private StringRedisTemplate template;
    
        @Autowired
        private ChannelTopic topic;
    
        @GetMapping("/publish")
        public String publish(@RequestParam String message) {
            template.convertAndSend(topic.getTopic(), message);
            return "Message published: " + message;
        }
    }

如果需要监听多个channel,可以通过RedisConfig中添加新的消息适配器。

java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class RedisConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter1,
            MessageListenerAdapter listenerAdapter2) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter1, topic1());
        container.addMessageListener(listenerAdapter2, topic2());
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter1(RedisMessageSubscriber subscriber) {
        return new MessageListenerAdapter(subscriber, "onMessage");
    }

    @Bean
    public MessageListenerAdapter listenerAdapter2(RedisMessageSubscriber subscriber) {
        return new MessageListenerAdapter(subscriber, "onMessage");
    }

    @Bean
    public ChannelTopic topic1() {
        return new ChannelTopic("channelOne");
    }

    @Bean
    public ChannelTopic topic2() {
        return new ChannelTopic("channelTwo");
    }

    @Bean
    public StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
}

同时RedisMessageSubscriber 也可以书写多个来区分不同的业务场景下不同业务处理。

相关推荐
sekaii6 分钟前
ReDistribution plan细节
linux·服务器·数据库
10km27 分钟前
java:Apache Commons Configuration2占位符解析异常的正确解法:${prefix:name:-default}
java·apache·configuration2·变量插值·interpolation
customer0828 分钟前
【开源免费】基于SpringBoot+Vue.JS个人博客系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
qq_4592384932 分钟前
SpringBoot整合Redis和Redision锁
spring boot·redis·后端
灰色人生qwer35 分钟前
SpringBoot 项目配置日志输出
java·spring boot·后端
焱焱枫42 分钟前
自适应SQL计划管理(Adaptive SQL Plan Management)在Oracle 12c中的应用
数据库·sql·oracle
2301_793069821 小时前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
阿华的代码王国1 小时前
【从0做项目】Java搜索引擎(6)& 正则表达式鲨疯了&优化正文解析
java·后端·搜索引擎·正则表达式·java项目·从0到1做项目
服务端相声演员1 小时前
Oracle JDK、Open JDK zulu下载地址
java·开发语言
是姜姜啊!1 小时前
java连接redis
java·redis