1,新建spring boot項目,過程百度
2,引入maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
3,修改rabbitmq配置文件
4,新建direct配置類
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DirectConfig {
@Bean
public Queue directQueue(){
return new Queue("direct",false); //隊列名字,是否持久化
}
@Bean
public DirectExchange directExchange(){
return new DirectExchange("direct",false,false);//交換器名稱、是否持久化、是否自動刪除
}
@Bean
Binding binding(Queue queue, DirectExchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("direct");
5,新建消息發送類
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 消息發送--生產消息
*/
@Component
public class Sender {
@Autowired
AmqpTemplate rabbitmqTemplate;
public void send(String message){
System.out.println("發送消息:"+message);
rabbitmqTemplate.convertAndSend("direct",message);
}
public void send2(String message){
System.out.println("發送消息:"+message);
rabbitmqTemplate.convertAndSend("direct2",message);
}
}
6,新建消息接收類
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "direct")
public class Receiver {
@RabbitHandler
public void handler(String message){
System.out.println("接收消息:"+message);
}
}
新建controller測試接口
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/rabbitmq")
public class MyRabbitmqController {
@Autowired
Sender sender;
@RequestMapping("/sender")
@ResponseBody
public String sender(){
System.out.println("send string:hello world");
sender.send("hello world");
return "sending...";
}
@RequestMapping("/sender2")
@ResponseBody
public String sender2(){
System.out.println("send string:hello world");
sender.send2("hello world2");
return "sending2...";
}
}
瀏覽器輸入:http://localhost:8089/rabbitmq/sender
成功
Fanout Exchange
3.3.2.1 配置隊列
創建FanoutConfig.java代碼如下:
package com.example.rabbitmq.mq;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutConfig {
final static String QUEUE_NAME = "fanout"; //隊列名稱
final static String QUEUE_NAME2 = "fanout2"; //隊列名稱
final static String EXCHANGE_NAME = "myfanout"; //交換器名稱
@Bean
public Queue queueFanout() {
return new Queue(FanoutConfig.QUEUE_NAME);
}
@Bean
public Queue queueFanout2() {
return new Queue(FanoutConfig.QUEUE_NAME2);
}
//配置交換器
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange(FanoutConfig.EXCHANGE_NAME);
}
// 綁定隊列到交換器
@Bean
Binding bindingFanoutExchangeQueue(Queue queueFanout, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queueFanout).to(fanoutExchange);
}
// 綁定隊列到交換器
@Bean
Binding bindingFanoutExchangeQueue2(Queue queueFanout2, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queueFanout2).to(fanoutExchange);
}
}
3.3.2.2 發送消息
創建FanoutSender.java代碼如下:
package com.example.rabbitmq.mq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FanoutSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(String message) {
System.out.println("發送消息:" + message); this.rabbitTemplate.convertAndSend(FanoutConfig.EXCHANGE_NAME,FanoutConfig.QUEUE_NAME, message);
}
public void send2(String message) {
System.out.println("發送消息2:" + message); this.rabbitTemplate.convertAndSend(FanoutConfig.EXCHANGE_NAME,FanoutConfig.QUEUE_NAME2, message);
}
}
3.3.2.3 消費消息
創建兩個監聽類,第一個FanoutReceiver.java代碼如下:
package com.example.rabbitmq.mq;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
@RabbitListener(queues = "fanout")
public class FanoutReceiver {
@RabbitHandler
public void process(String msg) {
System.out.println("Fanout(FanoutReceiver)消費消息:" + msg);
}
}
第二個FanoutReceiver2.java代碼如下:
package com.example.rabbitmq.mq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout2")
public class FanoutReceiver2 {
@RabbitHandler
public void process(String message) {
System.out.println("Fanout(FanoutReceiver2)消費消息:" + message);
}
}
3.3.2.4 測試代碼
創建FanoutTest.java代碼如下:
package com.example.rabbitmq.mq;
import com.example.rabbitmq.RabbitmqApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.text.SimpleDateFormat;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RabbitmqApplication.class)
public class FanoutTest {
@Autowired
private FanoutSender sender;
@Test
public void Test() throws InterruptedException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
sender.send("Time1 => " + sf.format(new Date()));
sender.send2("Date2 => " + sf.format(new Date()));
}
}
運行測試代碼,輸出結果如下:
發送消息:Time1 => 2018-09-11
發送消息2:Date2 => 2018-09-11
Fanout(FanoutReceiver2)消費消息:Time1 => 2018-09-11
Fanout(FanoutReceiver2)消費消息:Date2 => 2018-09-11
Fanout(FanoutReceiver)消費消息:Time1 => 2018-09-11
Fanout(FanoutReceiver)消費消息:Date2 => 2018-09-11
總結: 可以看出fanout會把消息分發到所有訂閱到該交換器的隊列,fanout模式是忽略路由鍵的。
3.3.3 Topic Exchange
3.3.3.1 配置隊列
@Configuration
public class TopicConfig {
final static String QUEUE_NAME = "log";
final static String QUEUE_NAME2 = "log.all";
final static String QUEUE_NAME3 = "log.all.error";
final static String EXCHANGE_NAME = "topicExchange"; //交換器名稱
@Bean
public Queue queuetopic() {
return new Queue(TopicConfig.QUEUE_NAME);
}
@Bean
public Queue queuetopic2() {
return new Queue(TopicConfig.QUEUE_NAME2);
}
@Bean
public Queue queuetopic3() {
return new Queue(TopicConfig.QUEUE_NAME3);
}
// 配置交換器
@Bean
TopicExchange topicExchange() {
return new TopicExchange(TopicConfig.EXCHANGE_NAME);
}
// 綁定隊列到交換器,並設置路由鍵(log.#)
@Bean
Binding bindingtopicExchangeQueue(Queue queuetopic, TopicExchange topicExchange) {
return BindingBuilder.bind(queuetopic).to(topicExchange).with("log.#");
}
// 綁定隊列到交換器,並設置路由鍵(log.*)
@Bean
Binding bindingtopicExchangeQueue2(Queue queuetopic2, TopicExchange topicExchange) {
return BindingBuilder.bind(queuetopic2).to(topicExchange).with("log.*");
}
// 綁定隊列到交換器,並設置路由鍵(log.*.error)
@Bean
Binding bindingtopicExchangeQueue3(Queue queuetopic3, TopicExchange topicExchange) {
return BindingBuilder.bind(queuetopic3).to(topicExchange).with("log.*.error");
}
}
3.3.3.2 發佈消息
@Component
public class TopicSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void topicSender(String message) {
String routingKey = "log.all.error";
System.out.println(routingKey + " 發送消息:" + message);
this.rabbitTemplate.convertAndSend(TopicConfig.EXCHANGE_NAME, routingKey, message);
}
}
3.3.3.3 消費消息
@Component
@RabbitListener(queues = "log")
public class TopicReceiver {
@RabbitHandler
public void process(String msg) {
System.out.println("log.# 消費消息:" + msg);
}
}
@Component
@RabbitListener(queues = "log.all")
public class TopicReceiver2 {
@RabbitHandler
public void process(String msg) {
System.out.println("log.* 消費消息:" + msg);
}
}
@Component
@RabbitListener(queues = "log.all.error")
public class TopicReceiver3 {
@RabbitHandler
public void process(String msg) {
System.out.println("log.*.error 消費消息:" + msg);
}
}
3.3.3.4 測試代碼
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RabbitmqApplication.class)
public class FanoutTest {
@Autowired
private FanoutSender fanoutSender;
@Test
public void Test() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
fanoutSender.send("Time1 => " + sf.format(new Date()));
fanoutSender.send2("Date2 => " + sf.format(new Date()));
}
}
輸出結果:
log.all.error 發送消息:time => 2018-09-11
log.# 消費消息:time => 2018-09-11
log.*.error 消費消息:time => 2018-09-11