日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

SpringBoot整合RabbitMQ的5種模式實戰

瀏覽:150日期:2023-02-20 13:30:05
目錄一、環境準備二、簡單模式三、工作隊列模式四、廣播模式(Fanout)五、直連模式(Direct)六、通配符模式(Topic)一、環境準備

SpringBoot整合RabbitMQ的5種模式實戰

1、pom依賴

<!-- 父工程依賴 --> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.6.RELEASE</version> </parent> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.0</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.0</version></dependency> </dependencies>

2、配置文件

server: port: 8080spring: rabbitmq: host: 192.168.131.171 port: 5672 username: jihu password: jihu virtual-host: /jihu

3、啟動類

@SpringBootApplicationpublic class RabbitMQApplication { public static void main(String[] args) { SpringApplication.run(RabbitMQApplication.class); }}

5、Swagger2類

@Configuration@EnableSwagger2public class Swagger2 { // http://127.0.0.1:8080/swagger-ui.html @Bean public Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage('com.jihu')).paths(PathSelectors.any()).build(); } private ApiInfo apiInfo() {return new ApiInfoBuilder().title('極狐-Spring Boot中使用spring-boot-starter-amqp集成rabbitmq').description('測試SpringBoot整合進行各種工作模式信息的發送')/*.termsOfServiceUrl('https://www.jianshu.com/p/c79f6a14f6c9')*/.contact('roykingw').version('1.0').build(); }}

6、ProducerController

@RestControllerpublic class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; //helloWorld 直連模式 @ApiOperation(value = 'helloWorld發送接口', notes = '直接發送到隊列') @GetMapping(value = '/helloWorldSend') public Object helloWorldSend(String message) throws AmqpException, UnsupportedEncodingException {//設置部分請求參數MessageProperties messageProperties = new MessageProperties();messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);//發消息rabbitTemplate.send('helloWorldqueue', new Message(message.getBytes('UTF-8'), messageProperties));return 'message sended : ' + message; } //工作隊列模式 @ApiOperation(value = 'workqueue發送接口', notes = '發送到所有監聽該隊列的消費') @GetMapping(value = '/workqueueSend') public Object workqueueSend(String message) throws AmqpException, UnsupportedEncodingException {MessageProperties messageProperties = new MessageProperties();messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);//制造多個消息進行發送操作for (int i = 0; i < 10; i++) { rabbitTemplate.send('work_sb_mq_q', new Message(message.getBytes('UTF-8'), messageProperties));}return 'message sended : ' + message; } // pub/sub 發布訂閱模式 交換機類型 fanout @ApiOperation(value = 'fanout發送接口', notes = '發送到fanoutExchange。消息將往該exchange下的所有queue轉發') @GetMapping(value = '/fanoutSend') public Object fanoutSend(String message) throws AmqpException, UnsupportedEncodingException {MessageProperties messageProperties = new MessageProperties();messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);//fanout模式只往exchange里發送消息。分發到exchange下的所有queuerabbitTemplate.send('fanoutExchange', '', new Message(message.getBytes('UTF-8'), messageProperties));return 'message sended : ' + message; } //routing路由工作模式 交換機類型 direct @ApiOperation(value = 'direct發送接口', notes = '發送到directExchange。exchange轉發消息時,會往routingKey匹配的queue發送') @GetMapping(value = '/directSend') public Object routingSend(String routingKey, String message) throws AmqpException, UnsupportedEncodingException {if (null == routingKey) { routingKey = 'china.changsha';}MessageProperties messageProperties = new MessageProperties();messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);//fanout模式只往exchange里發送消息。分發到exchange下的所有queuerabbitTemplate.send('directExchange', routingKey, new Message(message.getBytes('UTF-8'), messageProperties));return 'message sended : routingKey >' + routingKey + ';message > ' + message; } //topic 工作模式 交換機類型 topic @ApiOperation(value = 'topic發送接口', notes = '發送到topicExchange。exchange轉發消息時,會往routingKey匹配的queue發送,*代表一個單詞,#代表0個或多個單詞。') @GetMapping(value = '/topicSend') public Object topicSend(String routingKey, String message) throws AmqpException, UnsupportedEncodingException {if (null == routingKey) { routingKey = 'changsha.kf';}MessageProperties messageProperties = new MessageProperties();messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);//fanout模式只往exchange里發送消息。分發到exchange下的所有queuerabbitTemplate.send('topicExchange', routingKey, new Message(message.getBytes('UTF-8'), messageProperties));return 'message sended : routingKey >' + routingKey + ';message > ' + message; }}

7、ConcumerReceiver

@Componentpublic class ConcumerReceiver { //直連模式的多個消費者,會分到其中一個消費者進行消費。類似task模式 //通過注入RabbitContainerFactory對象,來設置一些屬性,相當于task里的channel.basicQos @RabbitListener(queues = 'helloWorldqueue') public void helloWorldReceive(String message) {System.out.println('helloWorld模式 received message : ' + message); } //工作隊列模式 @RabbitListener(queues = 'work_sb_mq_q') public void wordQueueReceiveq1(String message) {System.out.println('工作隊列模式1 received message : ' + message); } @RabbitListener(queues = 'work_sb_mq_q') public void wordQueueReceiveq2(String message) {System.out.println('工作隊列模式2 received message : ' + message); } //pub/sub模式進行消息監聽 @RabbitListener(queues = 'fanout.q1') public void fanoutReceiveq1(String message) {System.out.println('發布訂閱模式1received message : ' + message); } @RabbitListener(queues = 'fanout.q2') public void fanoutReceiveq2(String message) {System.out.println('發布訂閱模式2 received message : ' + message); } //Routing路由模式 @RabbitListener(queues = 'direct_sb_mq_q1') public void routingReceiveq1(String message) {System.out.println('Routing路由模式routingReceiveq11111 received message : ' + message); } @RabbitListener(queues = 'direct_sb_mq_q2') public void routingReceiveq2(String message) {System.out.println('Routing路由模式routingReceiveq22222 received message : ' + message); } //topic 模式 //注意這個模式會有優先匹配原則。例如發送routingKey=hunan.IT,那匹配到hunan.*(hunan.IT,hunan.eco),之后就不會再去匹配*.ITd @RabbitListener(queues = 'topic_sb_mq_q1') public void topicReceiveq1(String message) {System.out.println('Topic模式 topic_sb_mq_q1 received message : ' + message); } @RabbitListener(queues = 'topic_sb_mq_q2') public void topicReceiveq2(String message) {System.out.println('Topic模式 topic_sb_mq_q2 received message : ' + message); }}二、簡單模式

隊列配置:

/** * HelloWorld rabbitmq第一個工作模式 * 直連模式只需要聲明隊列,所有消息都通過隊列轉發。 * 無需設置交換機 */@Configurationpublic class HelloWorldConfig {@Beanpublic Queue setQueue() {return new Queue('helloWorldqueue');}}三、工作隊列模式

@Configurationpublic class WorkConfig { //聲明隊列 @Bean public Queue workQ1() {return new Queue('work_sb_mq_q'); }}四、廣播模式(Fanout)

/** * Fanout模式需要聲明exchange,并綁定queue,由exchange負責轉發到queue上。 * 廣播模式 交換機類型設置為:fanout */@Configurationpublic class FanoutConfig {//聲明隊列@Beanpublic Queue fanoutQ1() {return new Queue('fanout.q1');}@Beanpublic Queue fanoutQ2() {return new Queue('fanout.q2');}//聲明exchange@Beanpublic FanoutExchange setFanoutExchange() {return new FanoutExchange('fanoutExchange');}//聲明Binding,exchange與queue的綁定關系@Beanpublic Binding bindQ1() {return BindingBuilder.bind(fanoutQ1()).to(setFanoutExchange());}@Beanpublic Binding bindQ2() {return BindingBuilder.bind(fanoutQ2()).to(setFanoutExchange());}}五、直連模式(Direct)

/* 路由模式|Routing模式 交換機類型:direct*/@Configurationpublic class DirectConfig {//聲明隊列@Beanpublic Queue directQ1() {return new Queue('direct_sb_mq_q1');}@Beanpublic Queue directQ2() {return new Queue('direct_sb_mq_q2');}//聲明exchange@Beanpublic DirectExchange setDirectExchange() {return new DirectExchange('directExchange');}//聲明binding,需要聲明一個routingKey@Beanpublic Binding bindDirectBind1() {return BindingBuilder.bind(directQ1()).to(setDirectExchange()).with('china.changsha');}@Beanpublic Binding bindDirectBind2() {return BindingBuilder.bind(directQ2()).to(setDirectExchange()).with('china.beijing');}}六、通配符模式(Topic)

/*Topics模式 交換機類型 topic* */@Configurationpublic class TopicConfig {//聲明隊列@Beanpublic Queue topicQ1() {return new Queue('topic_sb_mq_q1');}@Beanpublic Queue topicQ2() {return new Queue('topic_sb_mq_q2');}//聲明exchange@Beanpublic TopicExchange setTopicExchange() {return new TopicExchange('topicExchange');}//聲明binding,需要聲明一個roytingKey@Beanpublic Binding bindTopicHebei1() {return BindingBuilder.bind(topicQ1()).to(setTopicExchange()).with('changsha.*');}@Beanpublic Binding bindTopicHebei2() {return BindingBuilder.bind(topicQ2()).to(setTopicExchange()).with('#.beijing');}}

測試

我們啟動上面的SpringBoot項目。

然后我們訪問swagger地址:http://127.0.0.1:8080/swagger-ui.html

SpringBoot整合RabbitMQ的5種模式實戰

然后我們就可以使用swagger測試接口了。

SpringBoot整合RabbitMQ的5種模式實戰

SpringBoot整合RabbitMQ的5種模式實戰

或者可以使用postman進行測試。

到此這篇關于SpringBoot整合RabbitMQ的5種模式實戰的文章就介紹到這了,更多相關SpringBoot整合RabbitMQ模式內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩1区2区日韩1区2区| 蜜桃视频一区二区| 香蕉久久久久久久av网站| 精品亚洲精品| 国产精久久一区二区| 日韩激情中文字幕| 午夜天堂精品久久久久| 亚洲精品九九| 老司机精品视频网| 亚洲性色av| 香蕉精品久久| 激情久久五月| 视频一区欧美精品| 模特精品在线| 麻豆精品久久久| 国产精品日本一区二区不卡视频| 国产视频一区二| 精品一区二区三区在线观看视频| 久久久精品午夜少妇| 日韩中文字幕1| 免费精品一区| 久久av在线| 视频精品一区二区| 日本在线视频一区二区| 国产精品三p一区二区| 高清久久一区| 日本免费一区二区三区四区| 噜噜噜久久亚洲精品国产品小说| 老司机精品视频网| 另类专区亚洲| 高清一区二区三区av| 久久国产乱子精品免费女| www.51av欧美视频| 日韩免费精品| 91精品蜜臀一区二区三区在线| 国产午夜精品一区二区三区欧美| 欧美 日韩 国产一区二区在线视频| 高清av一区| 亚洲影院天堂中文av色| 丝袜美腿一区二区三区| 美女视频黄久久| 日韩av在线中文字幕| 日韩视频免费| 国产成人精品一区二区三区视频| 亚洲综合专区| 国产午夜精品一区在线观看| 日韩中文视频| 婷婷激情图片久久| 久久av影院| 中文字幕一区日韩精品| 久久久久国产精品一区二区| 国产精品香蕉| 亚洲最新av| av不卡免费看| 91精品xxx在线观看| 香蕉国产精品| 日韩中文字幕区一区有砖一区 | 精品一区二区三区的国产在线观看| 欧美专区一区二区三区| 亚洲最新av| 四虎4545www国产精品| 亚洲女人av| 日韩高清在线不卡| 99视频精品| 成人午夜亚洲| 精品欧美日韩精品| 亚洲一区二区毛片| 91成人精品| 国产精品一区二区av交换 | 国产一区二区亚洲| 中文无码日韩欧| 中文在线免费视频| av免费不卡国产观看| 久久成人精品| 综合激情五月婷婷| 日韩毛片视频| 国产欧美日韩亚洲一区二区三区| 日韩精品视频在线看| 日韩在线观看一区| 婷婷综合五月| 亚洲一区二区免费看| 国产在线视频欧美一区| 午夜性色一区二区三区免费视频| 中国字幕a在线看韩国电影| 国产精品传媒麻豆hd| 国产情侣一区在线| 伊人久久成人| 亚洲国内欧美| 麻豆久久一区| 国产精区一区二区| 九一精品国产| 九九九精品视频| 国产日韩一区| 午夜日韩福利| 国产精品对白久久久久粗| 久久精品亚洲欧美日韩精品中文字幕| 日韩精品一二三四| 国产一区二区三区久久| 香蕉成人久久| 久久亚洲美女| 亚洲影视一区| 久久精品国产大片免费观看| 日韩不卡在线| 免费视频亚洲| 99在线观看免费视频精品观看| 国产精品v日韩精品v欧美精品网站| 日韩免费看片| 欧美日韩尤物久久| 亚洲精品网址| 亚洲天堂免费| 91亚洲精品在看在线观看高清| 欧美日韩一区二区三区在线电影| 欧美天堂一区二区| 久久成人福利| 日韩一区二区三区免费视频| 亚洲精品国产精品粉嫩| 欧美日韩一区二区国产 | 免费在线欧美黄色| 一二三区精品| 日本亚洲欧洲无免费码在线| 久久国内精品视频| 美日韩精品视频| 日韩在线第七页| 国产精品一区二区av日韩在线| 国产精品亚洲二区| 97精品国产| 欧美成人精品午夜一区二区| 精品国产一区二区三区性色av| 久久久精品午夜少妇| 中文字幕高清在线播放| 亚洲午夜精品久久久久久app| 在线精品一区| 羞羞答答国产精品www一本| 成人日韩在线观看| 麻豆精品蜜桃视频网站| 午夜久久av| 亚洲在线免费| 免费成人网www| 中文字幕日本一区二区| 欧美精品激情| 日韩国产一区二区| 在线亚洲观看| 国产欧美丝祙| 欧美日韩在线观看视频小说| 毛片在线网站| 午夜久久福利| 黄色成人在线网址| 四虎影视精品| 日本欧美不卡| 亚洲一区二区三区无吗| 亚洲欧美久久久| 国产精品探花在线观看| 97精品资源在线观看| 亚洲精品人人| 久久精品一区二区三区中文字幕| 美女少妇全过程你懂的久久| 国产主播一区| 亚洲人成在线影院| 最近国产精品视频| 久久伊人国产| 精品91福利视频| 美女久久99| 伊人成人网在线看| 一本色道久久精品| 久久av综合| 日韩88av| 欧美日韩精品免费观看视完整| 伊人久久成人| 国产精品99一区二区三区| 国产福利片在线观看| 日韩一区精品视频| 日韩欧美高清一区二区三区| 91亚洲国产| 亚洲午夜一级| 国产精品中文字幕制服诱惑| 午夜宅男久久久| 97精品国产福利一区二区三区| 麻豆视频在线看| 婷婷综合福利| 欧美亚洲国产一区| 精品国产18久久久久久二百| 亚洲精品婷婷| 国产精品调教视频| 成人亚洲一区| 日韩免费av| 欧美久久香蕉| 精品午夜视频| 97视频热人人精品免费| 伊人久久大香伊蕉在人线观看热v| 在线手机中文字幕| 久久av日韩| 欧美伊人久久| 精品国产一区二区三区2021| 亚洲在线久久| 在线亚洲成人| 国产精品午夜一区二区三区| 老鸭窝亚洲一区二区三区| 激情自拍一区| 日韩国产一区二| 美女视频免费精品|