SpringBoot使用Redis发布订阅pub/sub

本文介绍如何使用Redis的发布/订阅(Pub/Sub)功能进行消息传递。通过Spring Boot中的RedisTemplate,演示了消息的发送与接收流程,包括实现MessageListener接口以监听频道消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.消息发送

RedisTemplate支持Pub/Sub功能,调用convertAndSend方法可以发送一条消息:

​
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * redis生产者测试
     * @param data
     * @return
     */
    @GetMapping("/send1")
    String send1(String data) {
        redisTemplate.convertAndSend("test", data);
        return "success";
    }

​

convertAndSend方法用于向channel发送消息。第一个参数是channel的名称,第二个参数的消息的具体内容。redisTemplate将消息序列化成字节后发送到Redis Server。

2.消息订阅和接收

为了订阅频道test的消息,需要实现MessageListener接口的onMessage方法,代码如下:

public class MyRedisChannelListener implements MessageListener {

    private Log log= LogFactory.getLog(MyRedisChannelListener.class);

    @Override
    public void onMessage(Message message, byte[] bytes) {
        log.info("============>>>>> onMessage");
        byte[] channel=message.getChannel();
        byte[] body=message.getBody();

        try {
            String title=new String(channel,"UTF-8");
            String content=new String(body,"UTF-8");

            log.info("消息频道名称:"+title);
            log.info("消息内容是:"+content);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}

Message:

       通过调用getBody()方法回去消息的内容,getChannel()返回channel名称,他们返回的格式都是是字节数组,因为我们在发送的时候使用的是StringRedisTemplate,也就是发送的时候消息的编码是StringRedisTemplate定义的序列化策略来转换成字节的,因此我们需要选择正确的序列化策略将字节数组转换为我们需要的对象。

convertAndSend方法:

    public void convertAndSend(String channel, Object message) {
        Assert.hasText(channel, "a non-empty channel is required");
        byte[] rawChannel = this.rawString(channel);
        byte[] rawMessage = this.rawValue(message);
        this.execute((connection) -> {
            connection.publish(rawChannel, rawMessage);
            return null;
        }, true);
    }
    private byte[] rawString(String key) {
        return this.stringSerializer.serialize(key);
    }
    private byte[] rawValue(Object value) {
        return this.valueSerializer == null && value instanceof byte[] ? (byte[])((byte[])value) : this.valueSerializer.serialize(value);
    }

Pattern:订阅模式

 

配置MessageListener来监听Channel消息。

@Configuration
public class ChannelMessageConfig {

    @Bean
    MessageListenerAdapter listenerAdapter() {

        return new MessageListenerAdapter(new MyRedisChannelListener());
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter)
    {
        RedisMessageListenerContainer container=new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter,new PatternTopic("test*"));

        return container;
    }
}

MessageListenerAdapter 主要用来对消息进行序列化工作,默认采用StringRedisSerializer。

RedisMessageListenerContainer的主要作用是在Redis客户端接收到消息后,通过PatternTopic派发消息到对应的消息监听者,并构造一个线程池任务来调用MessageListener。

在Spring Boot项目中整合Redis实现发布订阅功能,你需要使用`lettuce`或`spring-data-redis`库来操作Redis,并利用其内置的pub/sub功能。这里是一个简单的例子,首先确保你已经在pom.xml文件中添加了相应的依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-redis</artifactId> </dependency> </dependencies> ``` 然后创建一个RedisTemplate配置类,设置连接池: ```java import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.PubSubMessage; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; public class RedisConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { // 这里填写你的Redis连接配置,如 host、port 等 return new LettuceConnectionFactory("localhost", 6379); } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); // 如果需要序列化/反序列化数据,可以设置序列化策略 // template.setKeySerializer(new StringRedisSerializer()); // template.setValueSerializer(new GenericToStringSerializer<>(Object.class)); return template; } @Bean public RedisMessageListenerContainer messageListenerContainer(RedisTemplate<String, Object> template) { MessageListenerAdapter adapter = new MessageListenerAdapter( (PubSubMessage<String, String> message) -> handleSubscribeMessage(message), "subscribeChannel"); RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(template.getConnectionFactory()); container.addMessageListener(adapter, new ChannelTopic<>("publishChannel")); return container; } private void handleSubscribeMessage(PubSubMessage<String, String> message) { System.out.println("Received message on subscribe channel: " + message.getMessage()); // 这里处理接收到的消息,例如存储到其他地方或转发给其他服务 } } ``` 最后,在`handleSubscribeMessage`方法中,你可以编写处理接收消息的逻辑。当有新的消息发布到`"publishChannel"`时,所有订阅该频道的客户端都会收到通知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值