Spring Boot 整合Redis实现Pub/Sub

首先:redis有一个独立的模块来支持消息多播模式,即Pub/Sub。我们先通过redis-cli来模拟一下Pub/Sub功能。先订阅一个消息,

SUBSCRIBE topic-01

然后客户端向channel:topic-01发消息

 可以看到订阅的收到了消息。

我们现在使用Spring Boot 整合Redis,实现消息的订阅,可以实现异步发送邮寄,发送手机验证码,或者异步调用等。

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
RedisChannelListener:实现消息的监听
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public class RedisChannelListener implements MessageListener {


    private static Map<String, Consumer<String>> RULE = new HashMap<>();

    {
        RULE.put(TopicConfig.SEND_EMAIL_TOPIC, this::sendEmail);
        RULE.put(TopicConfig.SEND_PHONE_TOPIC, this::sendPhone);
        RULE.put(TopicConfig.ASYNC_CALL_TOPIC, this::async);
    }



    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] b_channel = message.getChannel();
        byte[] b_body = message.getBody();
        try {
            String channel = new String(b_channel);
            String body = new String(b_body);
            System.out.println("channel is:" + channel + " , body is: " + body);
            RULE.get(channel).accept(body);
        } catch (Exception e) {
        }
    }

    public void async(String s) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("AsyncService exec params is :" + s);
    }

    public void sendEmail(String s) {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("EmailService exec params is :" + s);

    }

    public void sendPhone(String s) {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("PhoneService exec params is :" + s);
    }


}

消息监听的注册

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
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.stereotype.Component;


@Component
public class RedisConfig {


    @Bean
    public MessageListenerAdapter messageListenerAdapter() {
        return new MessageListenerAdapter(new RedisChannelListener());
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(messageListenerAdapter, new PatternTopic(TopicConfig.SEND_EMAIL_TOPIC));
        container.addMessageListener(messageListenerAdapter, new PatternTopic(TopicConfig.SEND_PHONE_TOPIC));
        container.addMessageListener(messageListenerAdapter, new PatternTopic(TopicConfig.ASYNC_CALL_TOPIC));
        return container;
    }


}

定义的一些Topic

public class TopicConfig {

    public final static String SEND_EMAIL_TOPIC = "send_email_topic";
    public final static String SEND_PHONE_TOPIC = "send_phone_topic";
    public final static String ASYNC_CALL_TOPIC = "async_call_topic";
}

写一个单元测试类

import com.hash.me.hashmeredis.config.TopicConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
public class MessageTest {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    void pub() {
        redisTemplate.convertAndSend(TopicConfig.SEND_EMAIL_TOPIC, "send email");
        redisTemplate.convertAndSend(TopicConfig.SEND_PHONE_TOPIC, "send phone code");
        redisTemplate.convertAndSend(TopicConfig.ASYNC_CALL_TOPIC, "async call");
    }
    
}

运行一下测试用例,可以看到如下结果,可以看到消息已经被接收消费掉了

        最后Pub/Sub的生产者传递过来一个消息, Redis会直接找到相应的消费者传递过去。如果一个消费者都没有,那么消息会被直接丢弃。如果开始有三个消费者, 有一个消费者突然挂掉了,生产者会继续发送消息,另外两个消费者可以持续收到消息,但是当挂掉的消费者重新连上的时候,在断连期间生产者发送的消息,对于这个消费者来说就是彻底丢失了如果Redis 停机重启, PubSub 的消息是不会持久化的,毕竟Redis宕机就相当于一个消费者都没有,所有的消息会被直接丢弃。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是一个简单的Spring Boot Redis Pub/Sub示例代码: 首先需要在pom.xml中添加redisspring-boot-starter-data-redis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.6.0</version> </dependency> ``` 然后编写一个Redis消息监听器: ```java @Component public class RedisMessageListener { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private MessageListenerAdapter messageListenerAdapter; @PostConstruct public void init() { new Thread(() -> { Jedis jedis = new Jedis("localhost"); jedis.subscribe(messageListenerAdapter, "test-channel"); }).start(); } @Bean public MessageListenerAdapter messageListenerAdapter() { return new MessageListenerAdapter(new RedisMessageSubscriber()); } public class RedisMessageSubscriber { public void handleMessage(String message) { System.out.println("Received message: " + message); } } } ``` 然后可以在其他地方发布消息到Redis中: ```java @RestController public class RedisController { @Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping("/publish") public String publish() { stringRedisTemplate.convertAndSend("test-channel", "Hello, Redis!"); return "Message sent"; } } ``` 这样,当调用/publish接口时,就会向Redis的test-channel频道发布一条消息,RedisMessageListener中的RedisMessageSubscriber就会收到该消息并进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

指挥官飞飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值