ToB企服应用市场:ToB评测及商务社交产业平台

标题: Redis发布订阅 [打印本页]

作者: 星球的眼睛    时间: 2024-7-23 03:57
标题: Redis发布订阅
情况:windows10、jdk17、springboot3
        Redis有两种发布订阅模式:频道发布订阅、模式发布订阅
        频道就是消息发到指定频道,订阅此频道的客户端都能收到消息;模式发布订阅就是匹配以xxx为开头的多个频道;我们这里用的是频道发布订阅,模式发布订阅不做讨论
1.详细实现

        redis依赖
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <version>3.0.2</version>
</dependency>
          配置
  1. spring:
  2.   data:
  3.     redis:
  4.       # 连接地址
  5.       host: 127.0.0.1
  6.       # 端口
  7.       port: 6379
  8.       # 数据库
  9.       database: 0
  10.       # 用户名,如果有
  11.       # username:
  12.       # 密码,如果有
  13.       password:
  14.       # 连接超时
  15.       connect-timeout: 5s
  16.       # 读超时
  17.       timeout: 5s
复制代码
1.1 redis 消息发布

         创建redis消息发布者
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.data.redis.core.StringRedisTemplate;
  3. import org.springframework.stereotype.Service;
  4. /**
  5. * @author luobei
  6. * redis发布者
  7. */
  8. @Service
  9. public class RedisPublisher {
  10.     private final StringRedisTemplate redisTemplate;
  11.     @Autowired
  12.     public RedisPublisher(StringRedisTemplate redisTemplate) {
  13.         this.redisTemplate = redisTemplate;
  14.     }
  15.     public void publish(String channel, String message) {
  16.         redisTemplate.convertAndSend(channel, message);
  17.     }
  18. }
复制代码
        业务中发布消息
  1. @Resource
  2. RedisPublisher redisPublisher;
  3. @PostConstruct                    //依赖注入后自动调用方法
  4. public void cache() {
  5.     redisPublisher.publish(Constant.REDIS_CACHE, "refresh");
  6. }
复制代码
1.2 redis 消息订阅

        创建redis消息订阅者
  1. import org.springframework.stereotype.Service;
  2. /**
  3. * @author luobei
  4. * redis订阅者
  5. */
  6. @Service
  7. public class RedisSubscriber {
  8.     public void onMessage(String message) {
  9.         // 处理收到的消息
  10.         System.out.println("Received message: " + message);
  11.     }
  12. }
复制代码
        配置配置文件
  1. /**
  2. * @author luobei
  3. * redis配置类
  4. */
  5. @Configuration
  6. public class RedisConfig {
  7.     @Bean
  8.     public RedisTemplate<String, String> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
  9.         RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
  10.         redisTemplate.setKeySerializer(new StringRedisSerializer());
  11.         redisTemplate.setValueSerializer(new StringRedisSerializer());
  12.         redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  13.         redisTemplate.setHashValueSerializer(new StringRedisSerializer());
  14.         redisTemplate.setConnectionFactory(redisConnectionFactory);
  15.         return redisTemplate;
  16.     }
  17.     private final RedisSubscriber redisSubscriber;
  18.     @Autowired
  19.     public RedisConfig(RedisSubscriber redisSubscriber) {
  20.         this.redisSubscriber = redisSubscriber;
  21.     }
  22.     @Bean
  23.     RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
  24.         RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  25.         //设置 Redis 连接工厂
  26.         container.setConnectionFactory(connectionFactory);
  27.         //添加消息监听器,监听 Constant.REDIS_CACHE 频道
  28.         container.addMessageListener(listenerAdapter(), new PatternTopic(Constant.REDIS_CACHE));
  29.         return container;
  30.     }
  31.     @Bean
  32.     MessageListenerAdapter listenerAdapter() {
  33.         return new MessageListenerAdapter(redisSubscriber, "onMessage");
  34.     }
  35. }
复制代码

使用建议

        对于实时性要求高、系统之间解耦要求高的场景,且可以容忍肯定的消息丢失,Redis 发布订阅是一个很好的选择。 
        对于需要消息可靠性、长期化、复杂的消息路由和大规模消息处理能力的场景,建议使用专业的消息队列系统,如 Kafka、RabbitMQ 等。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4