当前位置:   article > 正文

springboot 整合Redis订阅发布实现消息通知_springboot vue 系统中 做一个通知消息 redis

springboot vue 系统中 做一个通知消息 redis

最近做一个项目修改,B模块包含了A模块,但现在要在A模块里调用B模块里面定义的类实现修改,为了避免模块间的循环引用,决定转而使用Redis的发布订阅通知功能。

修改参考了(7条消息) springboot 整合使用redis发布订阅功能_springboot redis发布订阅_逆风飞翔的小叔的博客-CSDN博客

①检查最外层POM有无Redis依赖,以及yaml文件的redis配置

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. url: jdbc:mysql://IP:3306/warehouse?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
  5. username: root
  6. password: 666
  7. druid:
  8. max-active: 100
  9. initial-size: 10
  10. max-wait: 60000
  11. min-idle: 5
  12. redis:
  13. host: localhost
  14. port: 6379
  15. password: ''
  16. cache:
  17. type: redis

②检查Redis配置类里的redisTemplate配置(A模块内)

  1. @Bean
  2. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  3. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  4. template.setConnectionFactory(connectionFactory);
  5. //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
  6. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  7. ObjectMapper mapper = new ObjectMapper();
  8. mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  9. mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  10. jackson2JsonRedisSerializer.setObjectMapper(mapper);
  11. template.setValueSerializer(jackson2JsonRedisSerializer);
  12. //使用StringRedisSerializer来序列化和反序列化redis的key值
  13. template.setKeySerializer(new StringRedisSerializer());
  14. template.afterPropertiesSet();
  15. return template;
  16. }

③A模块内自定义RedisSubConfig(和Redis配置类一个包下),往容器(RedisMessageListenerContainer)内添加消息监听器以及它监听的管道(channel)

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.data.redis.connection.RedisConnectionFactory;
  4. import org.springframework.data.redis.listener.ChannelTopic;
  5. import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  6. import org.springframework.data.redis.connection.MessageListener;
  7. @Configuration
  8. public class RedisSubConfig {
  9. @Bean
  10. public RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListener listener) {
  11. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  12. container.setConnectionFactory(factory);
  13. //订阅频道topic1 和 topic2 这个container 可以添加多个 messageListener
  14. container.addMessageListener(listener, new ChannelTopic("topic1"));
  15. //container.addMessageListener(listener, new ChannelTopic("topic2"));
  16. return container;
  17. }
  18. }

④B模块内自定义消息监听器,重写里面的onMessage方法,并定义消息处理方法。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.data.redis.connection.Message;
  3. import org.springframework.data.redis.connection.MessageListener;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class RedisMessageListener implements MessageListener {
  8. @Autowired
  9. private RedisTemplate redisTemplate;
  10. @Override
  11. public void onMessage(Message message, byte[] pattern) {
  12. // 获取消息
  13. byte[] messageBody = message.getBody();
  14. // 使用值序列化器转换
  15. Object msg = redisTemplate.getValueSerializer().deserialize(messageBody);
  16. // 获取监听的频道
  17. byte[] channelByte = message.getChannel();
  18. // 使用字符串序列化器转换
  19. Object channel = redisTemplate.getStringSerializer().deserialize(channelByte);
  20. // 渠道名称转换
  21. String patternStr = new String(pattern);
  22. System.out.println(patternStr);
  23. System.out.println(channel+":"+msg);
  24. if("topic1".equals(channel)){
  25. handleMsg(msg);
  26. }
  27. }
  28. }

⑤在A模块中原先需要使用B模块代码的地方发送消息

  1. @RestController
  2. public class BussinessController {
  3. @Autowired
  4. private RedisTemplate redisTemplate;
  5. @GetMapping("/bussiness")
  6. public String business(String jsonString){
  7. //A模块主业务
  8. ...
  9. //发送消息到B模块处理
  10. redisTemplate.convertAndSend("topic1", jsonString);
  11. return "success";
  12. }
  13. }

至此修改完成。

该功能与springboot使用了redis哪个database无关(应用A可以使用db0,应用B可以使用db1),可以在不同的应用间使用。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/754545
推荐阅读
相关标签
  

闽ICP备14008679号