作者:-Dear-xi | 来源:互联网 | 2023-08-26 11:38
!--springboot集成rabbitMQ--从属cygroupidorg.springframework.boot/groupidartifactidspring-bo
! -- springboot集成rabbit MQ--从属cygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-amqp//
spring.application.name=spring boot _ rabbitmqspring.rabbit MQ.host=localhost spring.rabbit MQ.virtual-host=/rabbit MQ spring.rabbit MQ.username=guest spring.rabbit MQ.password=guest spring.rabbit MQ.port=5672队列交换机的类型和绑定
package com.test.spring boot _ rabbit MQ.config; importorg.spring帧work.amqp.core.*; importorg.spring帧work.context.annotation.bean; importorg.spring framework.context.annotation.configuration; @ configurationpublicclassrabbitmqconfig { @ beanpublicqueuemyqueue (} { return new queue (' my queue ) }; } @ beanpublicexchangemyexchange ((/newexchange ) )/returnnewtopicexchange )、topic.biz.ex )、false、false//rrex //returnnewfanoutexchange (' fanout.biz.ex ',false,false,null ); //returnnewheadersexchange (' header.biz.ex ',false,false,null ); //交换机名称、交换机类型()、持久化、自动删除或交换机属性映射集合//returnnewcustomexchange (' custom.biz.ex ',exchange } @ bean public绑定MYB ining () /绑定目标、绑定类型:是去交换机还是排队、交换机名称、路由密钥、绑定属性//new绑定绑定目标,绑定类型:是去交换机还是队列,交换机名称、根密钥、绑定属性//new绑定(,绑定. destination type.quue //将交换机direct.biz.ex绑定到队列myqueue,根key为direct.biz.exreturnnewbinding (' my queue )、binding.destination type
@ rest controller @ request mapping (' test ' ) publicclassrabbitmqtestcontroller ) @ autowiredprivateamqplaterabitttemplater )公共字符串发送消息(pathvariablestringmessage ) Throwsunsupportedencoding消息属性messagepropertiesmessageproperties=memessage ance (.setcontentencoding (消息属性. c Constance ) ) sageproperties.coding //消息代码message build=message builder.withbody (message.getbytes (utf-8 ) ).andproperties (消息属性) . 返回'确定'; }接收信息
@ componentpublicclasshelloconsumer (///) rabbitlistener ) queues='myqueue ' )/publicvoidservice ) string message///@parammessage*@paramvalue* . @ rabbit listener (queues=' my queue ' ) publicvoidservice ) @ parabitlistener @ header (name=' key ' ) String value }