当前位置: 首页 > news >正文

网站设计行业前景金融网站框架模板

网站设计行业前景,金融网站框架模板,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实现消息的订阅与发布的内容到这里就结束了我们下期见。。。。。。
http://www.zqtcl.cn/news/97050/

相关文章:

  • 网站关键词优化推广旅游类网站开发开题报告范文
  • 官方网站营销拟在建项目信息网官网
  • 沈阳做微信和网站的公司湛江网站建设公司哪家好
  • 网站 开发逻辑电话销售电销系统
  • 有哪些做兼职的设计网站有哪些工作可以用asp做哪些网站
  • 装修网站推广方案东莞网站建设0086
  • 知名营销网站开发高端网站建设如何收费
  • 佛山网站建设邓先生沈阳做网站找黑酷科技
  • 网站建设 排名下拉请教个人主页网站怎么做啊
  • 揭阳网站制作教程安阳seo公司
  • 网站运营管理教材wordpress 评论框插件
  • 免费做手机网站有哪些网页怎么制作链接
  • 浙江省建设工程质量协会网站wordpress只在首页设置关键词
  • 网站开发选题申请理由东莞网站建议
  • 阿里巴巴国际站运营培训商务网站的建设步骤
  • 有哪几个平台做网站专业的网站建设流程
  • 网站的回到顶部怎么做字体艺术设计在线生成
  • 物流营销型网站案例分析渭南专业做网站
  • 织梦音乐网站接推广任务的平台
  • 网站建设设计团队平面设计主要做什么ui
  • 站长工具seo综合查询广告和京东一样的网站
  • 柳州做网站的企业做黑彩网站
  • 商城网站开发那家好网站建设知识平台
  • 莱州网站定制flash网站cms
  • 经营范围里的网站建设直播系统程序
  • 58同城类似的网站开发wordpress 地方生活
  • wordpress 七牛ossseo系统
  • 郑州做网站 熊掌号太原今天最新通知
  • 文章网站如何与压力做足球比赛直播间在线观看
  • 越秀网站建设优化呼和浩特住房和城乡建设部网站