springboot+rabbitmq整合示例程
阿新 • • 發佈:2017-05-11
param resource pom del actor .cn pri 完全 pan
關於什麽是rabbitmq,請看另一篇文:
http://www.cnblogs.com/boshen-hzb/p/6840064.html
一、新建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-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=15672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
四、新建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綁定,routing_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綁定,routing_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條消息,分別被兩個消費者接收了
springboot+rabbitmq整合示例程