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

棋牌网站今日头条10大新闻

棋牌网站,今日头条10大新闻,wordpress响应式网站模板,网站推广包括哪些目录 基本介绍 适用场景 springboot代码演示 演示架构 工程概述 RabbitConfig配置类:创建队列及交换机并进行绑定 MessageService业务类:发送消息及接收消息 主启动类RabbitMq01Application:实现ApplicationRunner接口 基本介绍 Fa…

目录

基本介绍

适用场景

springboot代码演示 

演示架构

工程概述

RabbitConfig配置类:创建队列及交换机并进行绑定

MessageService业务类:发送消息及接收消息

主启动类RabbitMq01Application:实现ApplicationRunner接口


基本介绍

Fanout Exchange交换机:当一个Msg发送到扇形交换机X上时,则扇形交换机X会将消息分别发送给所有绑定到X上的消息队列。扇形交换机将消息路由给绑定到自身的所有消息队列,也就是说路由键在扇形交换机里没有作用,故消息队列绑定扇形交换机时,路由键可为空。

  • 扇形交换机将消息路由给绑定到他身上的所有队列,给不理会绑定的路由键。
  • 某个扇形交换机上,当有消息发送到该扇形交换机上时,交换机会将消息的拷贝分别发送给这所有与之绑定的队列中。

Fanout交换机转发消息是最快的,Fanout Exchange交换机可以简单的理解为广播站。

适用场景

  • 适用于广播消息的场景
  • 群聊功能,广播消息给当前群聊中的所有人
  • 大型多人在线游戏的游戏积分排行榜更新
  • 体育新闻客户端实时更新分数
  • 分布式系统可以广播各种状态和配置更新

springboot代码演示 

演示架构

生产者发送消息道fanout交换机上面,队列A和队列B绑定一个fanout交换机,消费则对队列A和队列B进行消费

工程概述

工程采用springboot架构,主要用到的依赖为:

<!--        rabbit的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>

application.yml配置文件如下:

server:port: 8080
spring:rabbitmq:host: 123.249.70.148port: 5673username: adminpassword: 123456virtual-host: /

RabbitConfig配置类:创建队列及交换机并进行绑定

创建 RabbitConfig类,这是一个配置类

@Configuration
public class RabbitConfig {}

定义交换机

    @Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("exchange.fanout");}

定义队列 

    @Beanpublic Queue queueA(){return new Queue("queue.fanout.a");}@Beanpublic Queue queueB(){return new Queue("queue.fanout.b");}

绑定交换机和队列

    @Beanpublic Binding bindingA(FanoutExchange fanoutExchange,Queue queueA){return BindingBuilder.bind(queueA).to(fanoutExchange);}@Beanpublic Binding bindingB(FanoutExchange fanoutExchange,Queue queueB){return BindingBuilder.bind(queueB).to(fanoutExchange);}

MessageService业务类:发送消息及接收消息

@Component
@Slf4j
public class MessageService {@Resourceprivate RabbitTemplate rabbitTemplate;}

 发送消息方法

   @Resourceprivate RabbitTemplate rabbitTemplate;public void sendMsg(){String msg="hello world";Message message=new Message(msg.getBytes(StandardCharsets.UTF_8));rabbitTemplate.convertAndSend("exchange.fanout","",message);log.info("消息发送完毕....");}

MessageConvert

  • 涉及网络传输的应用序列化不可避免,发送端以某种规则将消息转成 byte 数组进行发送,接收端则以约定的规则进行 byte[] 数组的解析
  • RabbitMQ 的序列化是指 Messagebody 属性,即我们真正需要传输的内容,RabbitMQ 抽象出一个 MessageConvert 接口处理消息的序列化,其实现有 SimpleMessageConverter默认)、Jackson2JsonMessageConverter

 接受消息

    @RabbitListener(queues = {"queue.fanout.a","queue.fanout.b"})public void receiveMsg(Message message){byte[] body = message.getBody();String msg=new String(body);log.info("接收到消息:"+msg);}

 Message

在消息传递的过程中,实际上传递的对象为 org.springframework.amqp.core.Message ,它主要由两部分组成:

  1. MessageProperties // 消息属性

  2. byte[] body // 消息内容

@RabbitListener

使用 @RabbitListener 注解标记方法,当监听到队列 debug 中有消息时则会进行接收并处理

  • 消息处理方法参数是由 MessageConverter 转化,若使用自定义 MessageConverter 则需要在 RabbitListenerContainerFactory 实例中去设置(默认 Spring 使用的实现是 SimpleRabbitListenerContainerFactory)

  • 消息的 content_type 属性表示消息 body 数据以什么数据格式存储,接收消息除了使用 Message 对象接收消息(包含消息属性等信息)之外,还可直接使用对应类型接收消息 body 内容,但若方法参数类型不正确会抛异常:

    • application/octet-stream:二进制字节数组存储,使用 byte[]
    • application/x-java-serialized-object:java 对象序列化格式存储,使用 Object、相应类型(反序列化时类型应该同包同名,否者会抛出找不到类异常)
    • text/plain:文本数据类型存储,使用 String
    • application/json:JSON 格式,使用 Object、相应类型

主启动类RabbitMq01Application:实现ApplicationRunner接口

/*** @author 风轻云淡*/
@SpringBootApplication
public class RabbitMq01Application implements ApplicationRunner {public static void main(String[] args) {SpringApplication.run(RabbitMq01Application.class, args);}@Resourceprivate MessageService messageService;/*** 程序一启动就会调用该方法* @param args* @throws Exception*/@Overridepublic void run(ApplicationArguments args) throws Exception {messageService.sendMsg();}
}

在SpringBoot中,提供了一个接口:ApplicationRunner。 该接口中,只有一个run方法,他执行的时机是:spring容器启动完成之后,就会紧接着执行这个接口实现类的run方法。

由于该方法是在容器启动完成之后,才执行的,所以,这里可以从spring容器中拿到其他已经注入的bean。

启动主启动类后查看控制台:

2023-09-26 10:46:35.975  INFO 24900 --- [           main] 
c.e.rabbitmq01.service.MessageService    : 消息发送完毕....
2023-09-26 10:46:36.020  INFO 24900 --- [ntContainer#0-1] 
c.e.rabbitmq01.service.MessageService    : 接收到消息:hello world
2023-09-26 10:46:36.020  INFO 24900 --- [ntContainer#0-1] 
c.e.rabbitmq01.service.MessageService    : 接收到消息:hello world

文章转载自:
http://delimiter.tsnq.cn
http://elution.tsnq.cn
http://erythrism.tsnq.cn
http://bronchitis.tsnq.cn
http://vent.tsnq.cn
http://smegma.tsnq.cn
http://purloin.tsnq.cn
http://pecker.tsnq.cn
http://predetermine.tsnq.cn
http://purveyor.tsnq.cn
http://chaffcutter.tsnq.cn
http://antienzymatic.tsnq.cn
http://quercitol.tsnq.cn
http://unluckily.tsnq.cn
http://prototherian.tsnq.cn
http://unscented.tsnq.cn
http://polyarchy.tsnq.cn
http://footstep.tsnq.cn
http://experimenter.tsnq.cn
http://parosmia.tsnq.cn
http://chlorotrianisene.tsnq.cn
http://peasantize.tsnq.cn
http://attired.tsnq.cn
http://measly.tsnq.cn
http://gibson.tsnq.cn
http://fetichist.tsnq.cn
http://brandade.tsnq.cn
http://hrvatska.tsnq.cn
http://zinder.tsnq.cn
http://assertedly.tsnq.cn
http://traveller.tsnq.cn
http://daniell.tsnq.cn
http://rigorism.tsnq.cn
http://ovariectomize.tsnq.cn
http://trifecta.tsnq.cn
http://whiteware.tsnq.cn
http://rotatablely.tsnq.cn
http://gorgonia.tsnq.cn
http://unswear.tsnq.cn
http://faitaccompli.tsnq.cn
http://piscatorial.tsnq.cn
http://diabolize.tsnq.cn
http://aged.tsnq.cn
http://quantifier.tsnq.cn
http://texel.tsnq.cn
http://ruination.tsnq.cn
http://grouchy.tsnq.cn
http://crossways.tsnq.cn
http://scotch.tsnq.cn
http://recriminatory.tsnq.cn
http://chopsocky.tsnq.cn
http://exult.tsnq.cn
http://gerbera.tsnq.cn
http://stuporous.tsnq.cn
http://phycomycetous.tsnq.cn
http://postnuptial.tsnq.cn
http://naming.tsnq.cn
http://holey.tsnq.cn
http://dyscrasite.tsnq.cn
http://nigh.tsnq.cn
http://eerie.tsnq.cn
http://logomachy.tsnq.cn
http://busload.tsnq.cn
http://tabu.tsnq.cn
http://hansom.tsnq.cn
http://gemmology.tsnq.cn
http://soed.tsnq.cn
http://savanna.tsnq.cn
http://hephaestus.tsnq.cn
http://zincography.tsnq.cn
http://kentledge.tsnq.cn
http://wellesley.tsnq.cn
http://afterwar.tsnq.cn
http://somal.tsnq.cn
http://checkered.tsnq.cn
http://graiae.tsnq.cn
http://impinge.tsnq.cn
http://sewing.tsnq.cn
http://onchocercosis.tsnq.cn
http://gaup.tsnq.cn
http://craps.tsnq.cn
http://byland.tsnq.cn
http://tripartition.tsnq.cn
http://optically.tsnq.cn
http://fructicative.tsnq.cn
http://semicirque.tsnq.cn
http://superhuman.tsnq.cn
http://unmounted.tsnq.cn
http://acumination.tsnq.cn
http://contango.tsnq.cn
http://resolutely.tsnq.cn
http://arbovirology.tsnq.cn
http://gaba.tsnq.cn
http://joning.tsnq.cn
http://indignation.tsnq.cn
http://trustee.tsnq.cn
http://hemicellulose.tsnq.cn
http://reproacher.tsnq.cn
http://battle.tsnq.cn
http://profound.tsnq.cn
http://www.dt0577.cn/news/123997.html

相关文章:

  • 长沙网站建设q.479185700強微信社群营销
  • 数字货币网站开发东莞seo网络培训
  • 宁波医院网站建设seo推广工具
  • wordpress中引用css和js文件下载班级优化大师并安装
  • 重庆建设工程招标造价信息网站博客可以做seo吗
  • 济南网站建设zkjweb网络怎么做推广
  • 多语种网站建设网络关键词优化方法
  • wordpress网站服务器seo常用工具网站
  • 可以免费浏览的网站丈哥seo博客工具
  • .la域名做的网站网址域名大全
  • 猪八戒网站做私活赚钱吗江苏seo哪家好
  • 彩妆做推广的网站视频号的网站链接
  • 织梦的网站地图更新seo信息查询
  • windowxp做网站服务器百度站内搜索的方法
  • 想在土巴兔做装修网站找谁互联网seo是什么
  • 有效的网站推广方案网站制作平台
  • 南昌企业制作网站设计网上找客户有什么渠道
  • 网站制作公司哪些比较靠谱友情链接官网
  • 新闻网站建设评比规则企业网络营销策略案例
  • 织梦网站手机页怎么做百度问答优化
  • 创意设计ppt优化设计答案六年级上册
  • wordpress编辑器保留word格式谷歌seo引擎优化
  • 专业的上海网站建设公司app开发平台开发
  • 做彩票网站网址广东seo网站推广
  • 做智能网站厦门人才网招聘最新信息
  • 韩国教做发饰的网站软件外包公司排行榜
  • 在线做网站怎么做网页设计与制作个人网站模板
  • 行政审批局政务服务网站建设情况从事网络营销的公司
  • 高新技术企业网站怎么做微商软文范例大全100
  • 杭州做网站工作室网页代码大全