springboot整合redis实现发布订阅

//监听处理器1
@Bean("messageListener1")
    public MessageListener messageListener1(RedisSerializer<String> redisSerializer) {
        System.out.println("init messageListener1");
        return (message, pattern) -> {
            final byte[] channel = message.getChannel();
            final byte[] body = message.getBody();

            String text = redisSerializer.deserialize(body);
            String topic = redisSerializer.deserialize(channel);
            log.info("client|{},topic|{},text|{}", 1, topic, text);
        };
    }
//监听处理器2
@Bean("messageListener2")
public MessageListener messageListener2(RedisSerializer<String> redisSerializer) {
    System.out.println("init messageListener2");
    return (message, pattern) -> {
        final byte[] channel = message.getChannel();
        final byte[] body = message.getBody();
        String text = redisSerializer.deserialize(body);
        String topic = redisSerializer.deserialize(channel);
        log.info("client|{},topic|{},text|{}", 2, topic, text);
    };
}
  //消息序列化
    @Bean
    public RedisSerializer RedisSerializer() {
        return new StringRedisSerializer();
    }
//redis监听容器
    @Bean("redisMessageListenerContainer")
    RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory, RedisSerializer<String> redisSerializer) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(messageListener1(redisSerializer), new PatternTopic("topic1"));
        container.addMessageListener(messageListener2(redisSerializer), new PatternTopic("topic1"));
        return container;
    }


	@Autowired
    private StringRedisTemplate redisTemplate;
    
    @GetMapping("/publish")
    public RestModel publish() {
        redisTemplate.convertAndSend("topic1", "nijia-倪佳");
        return RestModel.succ();
    }
好的,我可以回答这个问题。以下是一个简单的 Spring Boot 整合 Redis 实现发布订阅消息的例子: 1. 首先,在 pom.xml 文件中添加 Redis 相关依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置 Redis 连接信息: ``` spring.redis.host=localhost spring.redis.port=6379 ``` 3. 创建一个 Redis 发布者: ``` @Component public class RedisPublisher { @Autowired private RedisTemplate<String, Object> redisTemplate; public void publish(String channel, Object message) { redisTemplate.convertAndSend(channel, message); } } ``` 4. 创建一个 Redis 订阅者: ``` @Component public class RedisSubscriber { @Autowired private MessageListenerAdapter messageListenerAdapter; @PostConstruct public void init() { redisTemplate.execute((RedisConnection connection) -> { connection.subscribe(messageListenerAdapter, "channel"); return null; }); } @PreDestroy public void destroy() { redisTemplate.execute((RedisConnection connection) -> { connection.unsubscribe(messageListenerAdapter, "channel"); return null; }); } } ``` 5. 在需要发布消息的地方调用 RedisPublisher 的 publish 方法: ``` @Autowired private RedisPublisher redisPublisher; redisPublisher.publish("channel", "message"); ``` 6. 在需要订阅消息的地方实现 MessageListener 接口: ``` @Component public class MyMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] pattern) { String channel = new String(message.getChannel()); String messageBody = new String(message.getBody()); System.out.println("Received message: " + messageBody + " from channel: " + channel); } } ``` 以上就是一个简单的 Spring Boot 整合 Redis 实现发布订阅消息的例子。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值