网站设计行业前景,金融网站框架模板,logo设计在线生成免费网站,企业咨询师前言
本节内容主要介绍springboot项目通过集成redis#xff0c;如何利用redis的订阅发布机制#xff0c;完成系统消息的发布与订阅功能。Redis中的发布与订阅是一种消息通信模式#xff0c;允许发送者#xff08;发布者#xff09;将消息发送给多个接收者#xff08;订…前言
本节内容主要介绍springboot项目通过集成redis如何利用redis的订阅发布机制完成系统消息的发布与订阅功能。Redis中的发布与订阅是一种消息通信模式允许发送者发布者将消息发送给多个接收者订阅者。在 Redis中发布与订阅通过PUBLISH和SUBSCRIBE命令实现。频道Channel频道是消息的通道用于区分不同类型或主题的消息。订阅者可以选择订阅感兴趣的频道以接收相应的消息。Redis的发布与订阅模式是无状态的即发布者在发送消息之后不需要关心是否有订阅者接收到消息也不需要维护订阅者的信息。当发布者向某个频道发布消息时所有订阅了该频道的订阅者都会接收到相同的消息。这种机制使得消息的发布者和订阅者之间能够实现解耦并支持一对多的消息传递方式即广播形式。
正文
①创建一个web项目引入redis启动器的pom依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId
/dependency
② 在application.yml中添加redis的配置 ③创建redis的配置类 初始化redis工具类RedisTemplate和redis订阅消息的监听容器RedisMessageListenerContainer
package com.yundi.atp.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;Configuration
public class RedisConfig {/*** 初始化一个Redis消息监听容器* param connectionFactory* return*/Beanpublic RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);// 添加其他配置如线程池大小等return container;}Beanpublic RedisTemplateString, String redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplateString, String redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(connectionFactory);redisTemplate.setDefaultSerializer(new StringRedisSerializer());return redisTemplate;}
}④创建redis消息频道的常量
package com.yundi.atp.constant;public class ChannelConstant {/*** 广播通道*/public static final String CHANNEL_GLOBAL_NAME channel-global;/*** 单播通道*/public static final String CHANNEL_SINGLE_NAME channel-single;
}
⑤ 创建一个http请求用于发布基于redis的消息供客户端订阅
package com.yundi.atp.controller;import com.yundi.atp.constant.ChannelConstant;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;RequestMapping(value base)
RestController
public class BaseController {Resourceprivate RedisTemplate redisTemplate;/*** 发布广播消息** param msg*/GetMapping(value /publish/{msg})public void sendMsg(PathVariable(value msg) String msg) {redisTemplate.convertAndSend(ChannelConstant.CHANNEL_GLOBAL_NAME, msg);}
}⑥ 创建一个消息订阅者实现MessageListener接口通过重写onMessage方法订阅消息
package com.yundi.atp.listen;import com.yundi.atp.constant.ChannelConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;Slf4j
Component
public class RedisMessageSubscriber implements MessageListener {Autowiredprivate RedisMessageListenerContainer redisMessageListenerContainer;/*** 订阅消息将订阅者添加到指定的频道*/PostConstructpublic void subscribeToChannel() {//广播消息redisMessageListenerContainer.addMessageListener(this, new ChannelTopic(ChannelConstant.CHANNEL_GLOBAL_NAME));}Overridepublic void onMessage(Message message, byte[] bytes) {String channel new String(message.getChannel(), StandardCharsets.UTF_8);String messageBody new String(message.getBody(), StandardCharsets.UTF_8);log.info(Received message: messageBody from channel: channel);}
}⑦启动项目通过http请求发布消息查看是否能够订阅成功消息 ⑧开启redis客户端测试同样能够订阅到消息证明redis的消息的订阅与发布是无状态的且是广播模式 结语
关于springboot集成redis实现消息的订阅与发布的内容到这里就结束了我们下期见。。。。。。