1. 程式人生 > >springboot+rabbitmq 整合例項

springboot+rabbitmq 整合例項

一、新建maven工程:springboot-rabbitmq

二、引入springboot和rabbitmq的依賴

複製程式碼
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <
groupId>com.springboot.rabbitmq</groupId> <artifactId>springboot-rabbitmq</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-rabbitmq</name> <description>springboot-rabbitmq</description> <parent> <groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies> </project>
複製程式碼
spring-boot-starter-test是為了後面寫測試類用,
spring-boot-starter-amqp才是真正的使用rabbitmq的依賴

三、在src/main/resources裡面新增application.properties
該配置檔案主要是對rabbimq的配置資訊
複製程式碼

spring.application.name=springboot-rabbitmqspring.rabbitmq.host=127.0.0.1spring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guestspring.rabbitmq.publisher-confirms=truespring.rabbitmq.virtual-host=/

複製程式碼

四、新建springboot主類Application

該類初始化建立佇列、轉發器,並把佇列繫結到轉發器

複製程式碼
package com.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    final static String queueName = "hello";

    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
    
    @Bean
    public Queue userQueue() {
        return new Queue("user");
    }
    
    //===============以下是驗證topic Exchange的佇列==========
    @Bean
    public Queue queueMessage() {
        return new Queue("topic.message");
    }

    @Bean
    public Queue queueMessages() {
        return new Queue("topic.messages");
    }
  //===============以上是驗證topic Exchange的佇列==========
    
    
    //===============以下是驗證Fanout Exchange的佇列==========
    @Bean
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("fanout.C");
    }
    //===============以上是驗證Fanout Exchange的佇列==========
    

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    /**
     * 將佇列topic.message與exchange繫結,binding_key為topic.message,就是完全匹配
     * @param queueMessage
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    /**
     * 將佇列topic.messages與exchange繫結,binding_key為topic.#,模糊匹配
     * @param queueMessage
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
    
    @Bean
    Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }
    
    
   
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}
複製程式碼

五、各種情景實現

1、最簡單的hello生產和消費實現(單生產者和單消費者)

生產者:

複製程式碼
package com.rabbit.hello;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HelloSender1 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String sendMsg = "hello1 " + new Date();
        System.out.println("Sender1 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("helloQueue", sendMsg);
    }

}
複製程式碼

消費者:

複製程式碼
package com.rabbit.hello;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "helloQueue")
public class HelloReceiver1 {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver1  : " + hello);
    }

}
複製程式碼

controller:

複製程式碼
package com.rabbit.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rabbit.hello.HelloSender1;

@RestController
@RequestMapping("/rabbit")
public class RabbitTest {
    
    @Autowired
    private HelloSender1 helloSender1;
    @Autowired
    private HelloSender1 helloSender2;
    
    @PostMapping("/hello")
    public void hello() {
        helloSender1.send();
    }
}
複製程式碼

啟動程式,執行:

結果如下:

Sender1 : hello1 Thu May 11 17:23:31 CST 2017
Receiver2  : hello1 Thu May 11 17:23:31 CST 2017

2、單生產者-多消費者

生產者:

複製程式碼
package com.rabbit.hello;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HelloSender1 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String msg) {
        String sendMsg = msg + new Date();
        System.out.println("Sender1 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("helloQueue", sendMsg);
    }

}
複製程式碼

消費者1:

複製程式碼
package com.rabbit.hello;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "helloQueue")
public class HelloReceiver1 {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver1  : " + hello);
    }

}
複製程式碼

消費者2:

複製程式碼
package com.rabbit.hello;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "helloQueue")
public class HelloReceiver2 {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver2  : " + hello);
    }

}
複製程式碼

controller:

複製程式碼
package com.rabbit.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rabbit.hello.HelloSender1;

@RestController
@RequestMapping("/rabbit")
public class RabbitTest {
    
    @Autowired
    private HelloSender1 helloSender1;
    @Autowired
    private HelloSender1 helloSender2;
    
    @PostMapping("/hello")
    public void hello() {
        helloSender1.send("hello1");
    }
    
    /**
     * 單生產者-多消費者
     */
    @PostMapping("/oneToMany")
    public void oneToMany() {
        for(int i=0;i<10;i++){
            helloSender1.send("hellomsg:"+i);
        }
        
    }
}
複製程式碼

用post方式執行:

http://127.0.0.1:8080/rabbit/oneToMany

結果如下:

複製程式碼
Sender1 : hellomsg:0Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:1Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:2Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:3Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:4Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:5Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:6Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:7Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:8Thu May 11 17:37:59 CST 2017
Sender1 : hellomsg:9Thu May 11 17:37:59 CST 2017
Receiver2  : hellomsg:1Thu May 11 17:37:59 CST 2017
Receiver1  : hellomsg:0Thu May 11 17:37:59 CST 2017
Receiver1  : hellomsg:3Thu May 11 17:37:59 CST 2017
Receiver1  : hellomsg:4Thu May 11 17:37:59 CST 2017
Receiver1  : hellomsg:5Thu May 11 17:37:59 CST 2017
Receiver2  : hellomsg:2Thu May 11 17:37:59 CST 2017
Receiver1  : hellomsg:6Thu May 11 17:37:59 CST 2017
Receiver2  : hellomsg:7Thu May 11 17:37:59 CST 2017
Receiver2  : hellomsg:8Thu May 11 17:37:59 CST 2017
Receiver1  : hellomsg:9Thu May 11 17:37:59 CST 2017
複製程式碼

從以上結果可知,生產者傳送的10條訊息,分別被兩個消費者接收了

3、多生產者-多消費者

生產者1:

複製程式碼
package com.rabbit.hello;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HelloSender1 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String msg) {
        String sendMsg = msg + new Date();
        System.out.println("Sender1 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("helloQueue", sendMsg);
    }

}
複製程式碼

生產者2:

複製程式碼
package com.rabbit.hello;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HelloSender2 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String msg) {
        String sendMsg = msg + new Date();
        System.out.println("Sender2 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("helloQueue", sendMsg);
    }

}
複製程式碼

消費者1:

複製程式碼
package com.rabbit.hello;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "helloQueue")
public class HelloReceiver1 {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver1  : " + hello);
    }

}
複製程式碼

消費者2:

複製程式碼
package com.rabbit.hello;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "helloQueue")
public class HelloReceiver2 {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver2  : " + hello);
    }

}
複製程式碼

controller:

複製程式碼
    /**
     * 多生產者-多消費者
     */
    @PostMapping("/manyToMany")
    public void manyToMany() {
        for(int i=0;i<10;i++){
            helloSender1.send("hellomsg:"+i);
            helloSender2.send("hellomsg:"+i);
        }
        
    }
複製程式碼

用post方式執行:

http://127.0.0.1:8080/rabbit/manyToMany

結果如下:

複製程式碼
Sender1 : hellomsg:0Fri May 12 09:08:50 CST 2017
Sender2 : hellomsg:0Fri May 12 09:08:50 CST 2017
Sender1 : hellomsg:1Fri May 12 09:08:50 CST 2017
Sender2 : hellomsg:1Fri May 12 09:08:50 CST 2017
Sender1 : hellomsg:2Fri May 12 09:08:50 CST 2017
Sender2 : hellomsg:2Fri May 12 09:08:50 CST 2017
Sender1 : hellomsg:3Fri May 12 09:08:50 CST 2017
Sender2 : hellomsg:3Fri May 12 09:08:50 CST 2017
Sender1 : hellomsg:4Fri May 12 09:08:50 CST 2017
Sender2 : hellomsg:4Fri May 12 09:08:50 CST 2017
Sender1 : hellomsg:5Fri May 12 09:08:50 CST 2017
Sender2 : hellomsg:5Fri May 12 09:08:50 CST 2017
            
           

相關推薦

springboot+rabbitmq 整合例項

一、新建maven工程:springboot-rabbitmq 二、引入springboot和rabbitmq的依賴 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:

springboot+rabbitmq整合示例程

param resource pom del actor .cn pri 完全 pan 關於什麽是rabbitmq,請看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一、新建maven工程:springboot

SpringBoot Kafka 整合 例項 原始碼

  1、使用IDEA新建工程引導方式,建立訊息生產工程 springboot-kafka-producer。 工程POM檔案程式碼如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="htt

RabbitMQ系列之五 SpringBoot RabbitMQ整合

專案git地址:https://github.com/gitcaiqing/SpringBootRabbitMQ 1.專案結構 2.建立SpringBoot專案並引入RabbitMQ 3.專案pom.xml如下 <?xml version="1.0" encoding="

Springboot+RabbitMQ整合示例

一、RabbitMQ簡介          MQ全稱為Message Queue, 訊息佇列(MQ)是一種應用程式對應用程式的通訊方法。應用程式通過讀寫出入佇列的訊息(針對應用程式的資料)來通訊,而無需專用連線來連結它們。訊息傳遞指的是程式之間通過在訊息中傳送資料進行通訊,

簡單記錄一下springboot+rabbitmq整合

1.Windows下安裝RabbitMQ需要以下幾個步驟    (1):下載erlang,原因在於RabbitMQ服務端程式碼是使用併發式語言erlang編寫的,下載地址:http://www.erlang.org/downloads,雙擊.exe檔案進行安裝就好一路下一步

springboot-rabbitmq整合入門(一)

    一 前言 最近專案用到mq,於是學習了一波。直接上程式碼 二 工程結構圖   三 程式碼 一對一 模式:   /** * @Author qianyongchao * @Description * @Da

SpringBoot RabbitMQ 整合使用

前提 上次寫了篇文章,《SpringBoot Kafka 整合使用》,閱讀量還挺高的,於是想想還是把其他幾種 MQ 也和 SpringBoot 整合使用下。 下面是四種比較流行的 MQ : 後面都寫寫和 SpringBoot 整合的文章。 安裝 RabbitMQ 由於換 Mac 了,所以

springboot+dubbo+redis+RabbitMQ 專案整合例項

來源:https://blog.csdn.net/qq_28125445作者:夏佐關於springboot, 之前零零碎碎地寫了一些,今天從專案實戰角度給大家分享一下我的一點經驗。話不多說,先從專案的目錄結構講起。(文章最後貼了原始碼下載地址)如圖:專案分層:parent(頂

springboot簡易整合rabbitmq

寫在前面:本文采用rabbitmq環境是docker單節點。 專案地址:https://github.com/Blankwhiter/AMQP 一、搭建rabbitmq環境 在centos視窗中,執行如下命令拉取映象,以及建立容器: docker pull rabbitmq:3.

RabbitMq整合SpringBoot使用方法

整合方法詳見: 整合好之後,開啟 http://localhost:15672,開啟rabbitMq。 使用方法: 1.定義一個Config類,用於定義Queue和Exchanger,然後將這2個類繫結起來,用於傳送檔案。 @Configuration public clas

springboot+elasticsearch + rabbitMQ實現全文檢索(springboot+ES整合

known https vnr builder mod hystrix connector uid bsp springboot 2.X 能用 springboot-data-ES 5.X的 用特殊方式引入 5.X的ES 配置 bootstrap.xml 因為在調試,所

springboot 簡單整合rabbitmq

pom檔案新增: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp<

Rabbitmq 整合Spring,SpringBoot與Docker

最近開始研究訊息佇列框架Rabbitmq,之前對訊息佇列只是有個基本概念而已。至於使用場景也侷限於傳送郵件,傳送系統訊息等。 既然開始學習了,首先上官網把基本概念擼清除後,再看看Spring官方整合文件AMQP,基本上摸清了套路。 專案開始前的準備:

RabbitMQ的安裝及和springboot整合

Message Broker與AMQP簡介 Message Broker是一種訊息驗證、傳輸、路由的架構模式,其設計目標主要應用於下面這些場景: 訊息路由到一個或多個目的地 訊息轉化為其他的表現方式 執行訊息的聚集、訊息的分解,並將結果傳送到他們的目的地,然後重新組合

第5篇 RabbitMQ整合SpringBoot實現Direct模式

直接程式碼  專案結構 pom需要增加對RabbitM的支援 Pom檔案如下 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/P

springboot(整合篇):RabbitMQ整合詳解

RabbitMQ 即一個訊息佇列,主要是用來實現應用程式的非同步和解耦,同時也能起到訊息緩衝,訊息分發的作用。 訊息中介軟體在網際網路公司的使用中越來越多,剛才還看到新聞阿里將RocketMQ捐獻給了apache,當然了今天的主角還是講RabbitMQ。訊息中介軟體最主要的作用是解耦,中介軟體最標準的用法是

SpringBoot+Mybatis(註解開發)整合例項

新增依賴:pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:

隨便談談RabbitMQspringBoot進行整合

先說說題外話,本來只想找到一個springBoot快速整合RabbitMQ的例子,用起來就行的。但是百度搜了一大通, 各有各的玩法,但是就是沒找到一個自己心儀的方式。最終發現,稍微看看springBoot的jar包,頓時覺得清晰好多。 順便說明一下,這個文

MQ訊息佇列--RabbitMQ整合Spring理論及例項講解

今天Boss叫我去他的小黑屋分配任務,出門就記得倆詞“MQ”、“訊息佇列”。從來都沒聽說過這讓我怎麼搞?對於這種情況我慣有的方法論就是:先搞清楚它是什麼、有什麼用、有什麼工具可用、怎麼用,然後就是……擼起袖子使勁幹吧! 1、什麼是訊息佇列 訊息是指在兩個