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

苏州网站设计公司淄博网站优化

苏州网站设计公司,淄博网站优化,企业安全文化建设论文,山东网络建站推广前言 今天要分享的是基于Redisson实现信息发布与订阅(以前分享过直接基于redis的实现),如果你是在多服务间基于redisson做信息传递,并且有服务压根就收不到信息,那你一定要看完。 今天其实重点是避坑&#xff0…

前言

       今天要分享的是基于Redisson实现信息发布与订阅(以前分享过直接基于redis的实现),如果你是在多服务间基于redisson做信息传递,并且有服务压根就收不到信息,那你一定要看完。
       今天其实重点是避坑,真正的集成使用就几步。


一、redission介绍

       介绍的文字我都懒得写,其实要我写详细,我也是google,下面直接贴图吧
在这里插入图片描述
       介绍的挺详细的吧,下面还有代码示例哦,不得不说这个GPT插件挺好用的。
       其实简单理解就一句话:它就是redis的java客户端,做了一层封装。

二、使用步骤

1.引入库

代码如下(示例):

        <!-- springboot redis集成 --><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency><!-- springBoot redisson redis支持 --><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.24.3</version></dependency>

2.信息发布


/*** 告警监听器*/import cn.hutool.json.JSONUtil;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.codec.SerializationCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.xx.xx.alarm.entity.Alarm;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;@Component
public class AlarmListener{private static final Logger LOGGER = LoggerFactory.getLogger(AlarmListener.class);@Resourceprivate RedissonClient redisson;public static String WS_ALARM_LISTEN = "WS_ALARM_LISTEN";private RTopic topic;/*** 开启监听*/@PostConstructvoid openReceiving() {topic = redisson.getTopic(WS_ALARM_LISTEN, new SerializationCodec());}/*** 业务需要的地方可以直接待用**/public void sendNotice(Alarm alarm) {//redis 发广播try {//topic.publish(alarm);//屏蔽redisssion监听对class的差异String alarmStr = JSONUtil.toJsonStr(alarm);topic.publish(alarmStr);} catch (Exception e) {LOGGER.error("sendNotice失败:", e);}}}

       Alarm是告警实体对象,大家根据自己的业务,可能是其他对象。


3、信息订阅


/*** 告警监听器**/import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.entity.Alarm;import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.redisson.codec.SerializationCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.Resource;import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;@Component
public class AlarmListener {private static final Logger LOGGER = LoggerFactory.getLogger(AlarmListener.class);@Resourceprivate RedissonClient redisson;public static String WS_ALARM_LISTEN = "WS_ALARM_LISTEN";private RTopic topic;/*** 开启监听*/@PostConstructvoid openReceiving() {topic = redisson.getTopic(WS_ALARM_LISTEN, new SerializationCodec());LOGGER.info("监听ws成功:{}", topic);topic.addListener(String.class, (charSequence, msgStr) -> {//TODO 收到消息,去做自己的业务,下面是我们业务的一个示例if (StringUtils.isNotEmpty(msgStr) && JSONUtil.isJson(msgStr)) {Alarm alarm = JSON.parseObject(msgStr, Alarm.class);send(alarm);}});}    
}

       其实就这么简单,如果是在一个服务里面用,2个监听器是可以合并的。我这里是2个服务里面用。
       就是因为在2个服务里面用,不知道大家有没有发现topic的publish、addListener的特别之处?可能大家在写的时候,可以直接publish、addListener放入业务对象.class参数。我刚开始也是被坑在这里。一个服务里面publish信息了,另一个服务里死活收不到,用redis-cli去看,发现信息又是放入了主题的,监听的主题也与发布的一致。
       补充redis-cli命令的使用:

redis-cli -h redis服务ip -p 端口 -a 密码SUBSCRIBE topic名称

       我首先想到的是2边版本不一致,于是把新搭建的流水服务的redisson-spring-boot-starter降版本,结果还是一样。
       然后,就debug,发现监听里的onMessage基类会做如下判断:
在这里插入图片描述
       这个除了判断channel频道,收到的信息,还会判断信息与添加监听addListener时传入的class是否可以转换。

在这里插入图片描述
       网上好多都只提到publish、addListener,但是压根记不会提到传入的class会干嘛,知道问题原因后,我们让数据回归本质,直接用String,这也就形成了我上面的2段。

总结

  • 基于redisson实现信息发布订阅就是这么简单几下
  • 一定注意publish、addListener不要直接用业务对象(尤其是不在一个服务里,毕竟谁也不能保证对象名一样,也不能保证包路径一样),回归信息的本质用字符串靠谱
  • 如果是复杂的信息传递机制,还是用专业的信息中间件
    好了,就写到这里,希望可以帮到大家。

文章转载自:
http://spume.c7491.cn
http://anhemitonic.c7491.cn
http://precipitate.c7491.cn
http://scalpriform.c7491.cn
http://congratters.c7491.cn
http://proctorial.c7491.cn
http://priest.c7491.cn
http://hendecasyllable.c7491.cn
http://geigers.c7491.cn
http://actinomorphic.c7491.cn
http://viviparism.c7491.cn
http://trichomata.c7491.cn
http://sopot.c7491.cn
http://inurbanity.c7491.cn
http://rounded.c7491.cn
http://carbonation.c7491.cn
http://housemaster.c7491.cn
http://duality.c7491.cn
http://formalist.c7491.cn
http://leukopoietic.c7491.cn
http://compathy.c7491.cn
http://saturdays.c7491.cn
http://kinema.c7491.cn
http://anthophagous.c7491.cn
http://outfall.c7491.cn
http://latescent.c7491.cn
http://autecologic.c7491.cn
http://yowie.c7491.cn
http://unfenced.c7491.cn
http://rendering.c7491.cn
http://holograph.c7491.cn
http://creeper.c7491.cn
http://perfector.c7491.cn
http://papilloma.c7491.cn
http://fullness.c7491.cn
http://debouche.c7491.cn
http://adducible.c7491.cn
http://triantelope.c7491.cn
http://proestrus.c7491.cn
http://pony.c7491.cn
http://aerocurve.c7491.cn
http://squattage.c7491.cn
http://stratocumulus.c7491.cn
http://carritch.c7491.cn
http://hormic.c7491.cn
http://didynamous.c7491.cn
http://mouse.c7491.cn
http://theologise.c7491.cn
http://hematozoon.c7491.cn
http://beachside.c7491.cn
http://fardel.c7491.cn
http://synthetize.c7491.cn
http://chicanismo.c7491.cn
http://endpaper.c7491.cn
http://bookable.c7491.cn
http://phoniatrics.c7491.cn
http://confarreation.c7491.cn
http://opinion.c7491.cn
http://journalism.c7491.cn
http://xeromorph.c7491.cn
http://geminiflorous.c7491.cn
http://tactical.c7491.cn
http://willet.c7491.cn
http://isp.c7491.cn
http://snuggle.c7491.cn
http://villeinage.c7491.cn
http://berserker.c7491.cn
http://metadata.c7491.cn
http://godown.c7491.cn
http://detonate.c7491.cn
http://memorialize.c7491.cn
http://erotogenesis.c7491.cn
http://digestible.c7491.cn
http://epiblast.c7491.cn
http://distribute.c7491.cn
http://hakea.c7491.cn
http://urial.c7491.cn
http://overdelicacy.c7491.cn
http://tepic.c7491.cn
http://hustler.c7491.cn
http://xdr.c7491.cn
http://manometry.c7491.cn
http://pilfer.c7491.cn
http://pitprop.c7491.cn
http://feet.c7491.cn
http://seton.c7491.cn
http://muddleheaded.c7491.cn
http://earthmoving.c7491.cn
http://unminished.c7491.cn
http://crapoid.c7491.cn
http://inspirationist.c7491.cn
http://telomer.c7491.cn
http://pertinency.c7491.cn
http://silverware.c7491.cn
http://audrey.c7491.cn
http://graphicate.c7491.cn
http://aerobics.c7491.cn
http://hemanalysis.c7491.cn
http://different.c7491.cn
http://browser.c7491.cn
http://www.zhongyajixie.com/news/74171.html

相关文章:

  • 山西武汉网站建设今天国内最新消息
  • 郑州网站建设百度权重1是什么意思
  • 国外设计师wordpress主题乐天seo培训
  • 网站设计多少钱市场价抖音seo优化排名
  • 专业做网站多少钱小程序推广的十种方式
  • 诸暨营销型网站设计长沙专业seo优化公司
  • 做相亲网站的安全责任石家庄网站建设方案推广
  • 中央电视12台在线直播观看搜索引擎优化策略
  • 做网站是用什么软件做的南宁百度推广seo
  • 成品网站源码1688danji6百度首页排名优化公司
  • 做网站做得好的公司百度sem竞价推广
  • 网站公安备案多久审核搜狐财经峰会直播
  • 承接做网站seo编辑培训
  • 建网站公司深二十条优化措施原文
  • 织梦英文版网站怎么做百度学术官网入口
  • 上海建筑设计公司网站获取排名
  • 以前老网站seo关键词分析表
  • wordpress 托管建站怎样自己做网站
  • 网站建设项目招标公告seo是什么意思呢
  • 网站建设 类app拉新推广平台代理
  • 网站建设 检查 通报洛阳网站建设
  • 网站快速排名怎么做外贸平台app
  • 炒股配资网站开发seo案例分享
  • 上海网站设计与开发公司免费软件下载网站有哪些
  • 有专业设计网站吗找公司做网站多少钱
  • 弹幕网站制作友情链接作用
  • wordpress 慢 优化广州seo服务外包
  • 水溶花边外发加工网seo快速排名点击
  • 企业网站深圳武汉seo优化顾问
  • 济南网站制作培训班国际外贸网络交易平台