当前位置: 首页 > 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.pierceye.com/news/97050/

相关文章:

  • 昆明网站空间好习惯网站
  • 做导航网站赚钱吗建立网站需要多少钱费用
  • 大同网站建设哪家好网站后台登录模板html
  • 网站建设过程中准备的工作手机制作网站
  • 做专业网站设计多少钱代理小企业网站建设
  • 怎样提升网站关键词免费的html模版下载
  • 栖霞网站定制三合一建站网站
  • 免费建立一个个人网站设计官网登录入口
  • 门户网站模板之家北京网上服务平台
  • 合肥网站优化方案东莞做网站那家好
  • 个人备案网站可以做论坛吗山东住房建设厅官网站首页
  • 寺院网站模板网站策划制作公司 北京
  • 昆山教育云平台网站建设宁晋县建设局网站
  • 廊坊网站公司dw做网站背景音乐
  • 阜南做网站搜索引擎优化seo多少钱
  • 贵州建设厅网站怎样查询电工证天津网站备案
  • 常州做网站的公司在盐城做网站的网络公司电话
  • seo站外推广如何用wampp 做网站
  • 怎样用手机做网站中企动力百度百科
  • 哪些网站可以做任务挣钱免费app软件
  • 国内简约网站平潭县机场建设网站
  • wordpress 全站通知wordpress怎样打开速度快
  • 广州市建设职业培训学校网站移除wordpress版本
  • 如何申请一个网站 新网动画制作大师
  • 动易后台 网站统计调查 报表类型怎样使用手机相册备份网站源码
  • 做网站放到百度上需要什么查看wordpress使用什么主题
  • 深圳企业网站seo郑州汉狮专业做网站公司
  • 广东网站建设多少钱辛集专业网站建设
  • 怎样做网站公司的销售小程序推广计划怎么赚钱
  • 网站开发文档编写wordpress小说站群