日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot——RabbitMQ

發布時間:2024/2/28 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot——RabbitMQ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自https://blog.csdn.net/lyhkmm/article/details/78772919

RabbitMq的介紹

RabbitMq的基本原理可以自行上網查閱,或者點擊傳送門:RabbitMQ的基本原理。

使用配置

1、老規矩,先在pom.xml中添加相關依賴:

?
  • <!--消息隊列模塊-->

  • <dependency>

  • <groupId>org.springframework.boot</groupId>

  • <artifactId>spring-boot-starter-amqp</artifactId>

  • </dependency>

  • 2、在application.properties添加rabbitmq的相關信息:

    ?
  • spring.application.name=spirng-boot-rabbitmq

  • spring.rabbitmq.host=127.0.0.1

  • spring.rabbitmq.port=5672

  • spring.rabbitmq.username=guest

  • spring.rabbitmq.password=guest

  • 端口、用戶名和密碼都是默認的,根據自己的實際情況配置,rabbitmq的安裝教程網上很多了,這里暫時不介紹,以后有時間補上。

    3、配置隊列:

    ?

    ?
  • package com.lyh.demo;

  • ?
  • import org.springframework.amqp.core.Queue;

  • import org.springframework.context.annotation.Bean;

  • import org.springframework.context.annotation.Configuration;

  • ?
  • /**

  • * @Author:linyuanhuang

  • * @Description:隊列配置,隊列的名稱,發送者和接受者的名稱必須一致,否則接收不到消息

  • * @Date:2017/12/11 14:50

  • */

  • @Configuration

  • public class RabbitMqConfig {

  • @Bean

  • public Queue Queue1() {

  • return new Queue("lyhTest1");

  • }

  • }

  • 4、發送者通過Controller類發送消息:

    ?

    ?
  • package com.lyh.demo.controller;

  • ?
  • import org.springframework.amqp.core.AmqpTemplate;

  • import org.springframework.beans.factory.annotation.Autowired;

  • import org.springframework.web.bind.annotation.RequestMapping;

  • import org.springframework.web.bind.annotation.RestController;

  • ?
  • import java.util.Date;

  • ?
  • @RestController

  • public class SendController {

  • @Autowired

  • private AmqpTemplate amqpTemplate;

  • ?
  • @RequestMapping("/send")

  • public String send(){

  • String content="Date:"+new Date();

  • amqpTemplate.convertAndSend("lyhTest1",content);

  • return content;

  • }

  • }

  • 5、創建接受者Receiver1,新建類:

    ?

    ?
  • package com.lyh.demo.Receiver;

  • ?
  • import org.springframework.amqp.rabbit.annotation.RabbitHandler;

  • import org.springframework.amqp.rabbit.annotation.RabbitListener;

  • import org.springframework.stereotype.Component;

  • ?
  • @Component

  • @RabbitListener(queues = "lyhTest1")

  • public class Receiver1 {

  • ?
  • @RabbitHandler

  • public void receiver(String msg){

  • System.out.println("Test1 receiver1:"+msg);

  • }

  • }

  • ?

    6、測試

    瀏覽器訪問地址:http://localhost:8080/send,如下圖:

    終端輸出接受的內容:

    查看RabbitMQ的Web客戶端http://localhost:15672,需要自己安裝RabbitMQ的客戶端,可以自己上網查閱相關教程。帳號密碼和配置文件一樣,如下圖:可以在列表里看到之前創建的隊列。

    一對多的使用配置

    1、一對多,一個發送者發送消息,多個接受者接受同一個消息,添加新的接收者Receiver2:

    ?

    ?
  • package com.lyh.demo.Receiver;

  • ?
  • import org.springframework.amqp.rabbit.annotation.RabbitHandler;

  • import org.springframework.amqp.rabbit.annotation.RabbitListener;

  • import org.springframework.stereotype.Component;

  • ?
  • @Component

  • @RabbitListener(queues = "lyhTest1")

  • public class Receiver2 {

  • ?
  • @RabbitHandler

  • public void receiver(String msg){

  • System.out.println("Test1 receiver2:"+msg);

  • }

  • }

  • ?

    2、發送者循環發送10個消息,在SendController添加一對多發送方法:

    ?

    ?
  • @RequestMapping("/multiSend")

  • public String multiSend(){

  • StringBuilder times=new StringBuilder();

  • for(int i=0;i<10;i++){

  • long time=System.nanoTime();

  • amqpTemplate.convertAndSend("lyhTest1","第"+i+"次發送的時間:"+time);

  • times.append(time+"<br>");

  • }

  • return times.toString();

  • }

  • ?

    3、測試,瀏覽器訪問http://localhost:8080/multiSend,如下圖:

    ?


    ?

    4、終端輸出接收數據:

    ?

    Test1 receiver2:第1次發送的時間:25953655163399 Test1 receiver1:第0次發送的時間:25953641137213 Test1 receiver2:第2次發送的時間:25953655403734 Test1 receiver1:第3次發送的時間:25953655591967 Test1 receiver1:第5次發送的時間:25953655949458 Test1 receiver2:第4次發送的時間:25953655772971 Test1 receiver1:第6次發送的時間:25953656111790 Test1 receiver1:第8次發送的時間:25953656492471 Test1 receiver1:第9次發送的時間:25953656687330 Test1 receiver2:第7次發送的時間:25953656277133

    可以看到發送者發送一個消息被多個接收者接收,注意這里的消息只能被消費一次

    多對多的使用配置

    1、在配置類RabbbitMqConfig添加新的隊列名lyhTest2:

    ?

    ?
  • @Configuration

  • public class RabbitMqConfig {

  • @Bean

  • public Queue Queue1() {

  • return new Queue("lyhTest1");

  • }

  • @Bean

  • public Queue Queue2() {

  • return new Queue("lyhTest2");

  • }

  • }

  • 2、修改Receiver2接收隊列名為lyhTest2:

    ?

    ?
  • @Component

  • @RabbitListener(queues = "lyhTest2")

  • //這里的lyhTest2是多對多,如果要測試一對多改成lyhTest1

  • public class Receiver2 {

  • ?
  • @RabbitHandler

  • public void receiver(String msg){

  • System.out.println("Test2 receiver2:"+msg);

  • }

  • }

  • 3、在SendController添加多對多發送消息的方法:

    ?
  • @RequestMapping("/multi2MultiSend")

  • public String mutil2MutilSend(){

  • StringBuilder times=new StringBuilder();

  • for(int i=0;i<10;i++){

  • long time=System.nanoTime();

  • amqpTemplate.convertAndSend("lyhTest1","第"+i+"次發送的時間:"+time);

  • amqpTemplate.convertAndSend("lyhTest2","第"+i+"次發送的時間:"+time);

  • times.append(time+"<br>");

  • }

  • return times.toString();

  • }

  • 4、測試,瀏覽器訪問:http://localhost:8080/multi2MultiSend,如下圖:

    5、終端輸出接收數據:

    ?

    Test1 receiver1:第0次發送的時間:27607875773748 Test2 receiver2:第0次發送的時間:27607875773748 Test2 receiver2:第1次發送的時間:27607882272138 Test2 receiver2:第2次發送的時間:27607882429049 Test1 receiver1:第1次發送的時間:27607882272138 Test2 receiver2:第3次發送的時間:27607882594693 Test1 receiver1:第2次發送的時間:27607882429049 Test2 receiver2:第4次發送的時間:27607882897371 Test1 receiver1:第3次發送的時間:27607882594693 Test2 receiver2:第5次發送的時間:27607883163005 Test1 receiver1:第4次發送的時間:27607882897371 Test2 receiver2:第6次發送的時間:27607883319916 Test2 receiver2:第7次發送的時間:27607883489777 Test1 receiver1:第5次發送的時間:27607883163005 Test1 receiver1:第6次發送的時間:27607883319916 Test2 receiver2:第8次發送的時間:27607883957798 Test2 receiver2:第9次發送的時間:27607884305953 Test1 receiver1:第7次發送的時間:27607883489777 Test1 receiver1:第8次發送的時間:27607883957798 Test1 receiver1:第9次發送的時間:27607884305953

    ? ? ? ?可以看到不同的接收者接收不同發送者發送的消息,消息也可以是實體對象,這里就不做演示。

    Topic Exchange的使用配置

    Topic Exchange是RabbitMQ中最靈活的一種方式,它能夠根據routing_key自由的綁定不同的隊列,可以適用絕大部分的項目需求

    1、新建RabbitMqTopicConfig配置類:

    ?

    package com.lyh.demo;import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @Author:linyuanhuang* @Description:Topic Exchange配置類* @Date:2017/12/11 17:13*/ @Configuration public class {//只接一個topicfinal static String message = "topic.message";//接收多個topicfinal static String messages = "topic.messages";@Beanpublic Queue queueMessage() {return new Queue(RabbitMqTopicConfig.message);}@Beanpublic Queue queueMessages() {return new Queue(RabbitMqTopicConfig.messages);}@BeanTopicExchange exchange() {return new TopicExchange("exchange");}@BeanBinding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");}@BeanBinding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {//這里的#表示零個或多個詞。return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");}

    2、在SendController添加發送消息方法:

    @RequestMapping("/topicSend1")public String topicSend1() {String context = "my topic 1";System.out.println("發送者說 : " + context);this.amqpTemplate.convertAndSend("exchange", "topic.message", context);return context;}@RequestMapping("/topicSend2")public String topicSend2() {String context = "my topic 2";System.out.println("發送者說 : " + context);this.amqpTemplate.convertAndSend("exchange", "topic.messages", context);return context;

    3、創建接收者的方法TopicReceiver1和TopicReceiver2:

    ?

    TopicReceiver1:

    import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "topic.message") public class TopicReceiver1 {@RabbitHandlerpublic void process(String msg) {System.out.println("TopicReceiver1:" + msg);}} TopicReceiver2: import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "topic.messages") public class TopicReceiver2 {@RabbitHandlerpublic void process(String msg) {System.out.println("TopicReceiver2 :" + msg);}

    4、測試:

    瀏覽器訪問http://localhost:8080/topicSend1,終端輸出:

    ? 發送者說 : my topic 1 TopicReceiver1:my topic 1 TopicReceiver2 :my topic 1

    瀏覽器訪問http://localhost:8080/topicSend2,終端輸出:

    發送者說 : my topic 2 TopicReceiver2 :my topic 2 5、總結:

    這里的Topic Exchange 轉發消息主要是根據通配符,隊列topic.message只能匹配topic.message的路由。而topic.messages匹配路由規則是topic.#,所以它可以匹配topic.開頭的全部路由。而topic.#發送的消息也只能是topic.#的接受者才能接收。

    GitHub地址:https://github.com/lyhkmm/spring-boot-examples/tree/master/spring-boot-rabbitmq

    ?

    總結

    以上是生活随笔為你收集整理的Spring Boot——RabbitMQ的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。