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

springboot整合redis发布订阅(4步搞定)

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

springboot整合redis发布订阅(4步搞定)

springboot整合redis系列集合

  • 1.springboot整合redis序列化(3步搞定)
  • 2.springboot整合redis发布订阅(4步搞定)
  • 3.springboot整合redis数据结构对应使用场景讲解
  • 4.springboot整合redis分布式锁

springboot整合redis发布订阅(4步搞定)
  • 项目结构图
  • 1、pom文件添加依赖
  • 2、yml文件配置连接
  • 3、添加监听者
    • 添加监听者1号
    • 添加监听者2号
  • 4、添加配置类(序列化、建立监听与频道绑定关系)
  • 进行测试
    • 添加一个DemoPubController
    • 运行项目,Postman调用测试
      • 发布消息
      • 监听器接收到了消息
  • 总结


项目结构图

1、pom文件添加依赖

1.依赖版本springboot会自动匹配,所以不用关注
2.fastjson依赖方便我们管理json格式数据


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


   com.alibaba
    fastjson
    1.2.72

2、yml文件配置连接

redis安装教程请移步查看: docker快速安装redis(两步轻松搞定)

port:端口号
host:ip地址
password:密码

spring:
  redis:
    port: 6379
    host: 127.0.0.1
    password: 123456
3、添加监听者 添加监听者1号
package com.phy.demo.listen;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;


public class ConsumerRedisOneListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.err.println("订阅者-1号,接收到消息:"+new String(message.getBody()));
    }
}
添加监听者2号
package com.phy.demo.listen;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;


public class ConsumerRedisTwoListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.err.println("订阅者-2号,接收到消息:"+new String(message.getBody()));
    }
}
4、添加配置类(序列化、建立监听与频道绑定关系)
package com.phy.demo.config;

import com.phy.demo.listen.ConsumerRedisOneListener;
import com.phy.demo.listen.ConsumerRedisTwoListener;
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.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {
    
    @Autowired
    private LettuceConnectionFactory connectionFactory;

    
    @Bean
    public ConsumerRedisOneListener consumeRedisOneListener() {
        return new ConsumerRedisOneListener();
    }
    
    @Bean
    public ConsumerRedisTwoListener consumeRedisTwoListener() {
        return new ConsumerRedisTwoListener();
    }
    
    @Bean
    public ChannelTopic channelCore() {
        return new ChannelTopic("channelCore");
    }
    
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 频道绑定监听器1号
        container.addMessageListener(consumeRedisOneListener(), channelCore());
        // 频道绑定监听器2号
        container.addMessageListener(consumeRedisTwoListener(), channelCore());
        return container;
    }
    
    @Bean
    public RedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        // 替换默认序列化
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

至此,springboot整合redis发布订阅/消息队列就已完成,下面我们测试一下


进行测试 添加一个DemoPubController
package com.phy.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/pub")
public class DemoPubController {
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("send")
    public void set(@RequestBody JSONObject json) {
        redisTemplate.convertAndSend("channelCore",json.get("data"));
    }
}
运行项目,Postman调用测试 发布消息

监听器接收到了消息

总结

发布者与监听者,可以是跨项目、跨服务的。引入redis发布/订阅可以解耦
渠道与监听器绑定关系可以是一对多,也可以是多对一,还可以是多对多。根据实际需求设置

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

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

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