pom.xml添加以下内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.properties:
spring.application.name=spring-boot-rabbitmq
spring.rabbitmq.host=192.168.80.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456
RabbitMQConfig:
配置交换器、队列、绑定关系:
package com.song.songvue.config.rabbitmq;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
// 直连交换器
public static final String SONG_EXCHANGE = "song_exchange";
public static final String SONG_QUEUE = "song_queue";
public static final String ROUTING_KEY = "songKey";
// fanout交换器
public static final String SONG_FANOUT = "song_fanout";
// 邮箱信息队列
public static final String SONG_MAIL_MESSAGE_QUEUE = "song_mail_message_queue";
// 游戏等级队列
public static final String SONG_GAME_LEVEL_QUEUE = "song_game_level_queue";
// topic交换器
public static final String SONG_TOPIC_EXCHANGE = "song_topic_exchange";
// 创建一个队列
@Bean
public Queue queue() {
return new Queue(SONG_QUEUE);
}
// 创建一个直连交换机
@Bean
DirectExchange directExchange() {
return new DirectExchange(SONG_EXCHANGE);
}
// 绑定
@Bean
Binding binding() {
return BindingBuilder.bind(queue()).to(directExchange()).with(ROUTING_KEY);
}
// fanout交换器
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange(SONG_FANOUT);
}
// 创建两个队列
@Bean
Queue mailMessageQueue() {
return new Queue(RabbitMQConfig.SONG_MAIL_MESSAGE_QUEUE);
}
@Bean
Queue gameLevelQueue() {
return new Queue(RabbitMQConfig.SONG_GAME_LEVEL_QUEUE);
}
// 绑定两个队列
@Bean
Binding bindingMail() {
return BindingBuilder.bind(mailMessageQueue()).to(fanoutExchange());
}
@Bean
Binding bindingGame() {
return BindingBuilder.bind(gameLevelQueue()).to(fanoutExchange());
}
// topic
@Bean
TopicExchange topicExchange() {
return new TopicExchange(SONG_TOPIC_EXCHANGE);
}
/**
*
* RoutingKey 为一个点号 "."分隔的字符串
*
* BindingKey 可以存在两种特殊字符串 "*" 和 "#", 用做模糊匹配。
*
* com.#
* "#" 用于匹配多规格单词(可以是0个)
*
* *.song.*
* "*" 用于匹配一个单词
*/
@Bean
Binding bindTopic1() {
return BindingBuilder.bind(queue()).to(topicExchange()).with("com.#");
}
@Bean
Binding bindTopic2() {
return BindingBuilder.bind(gameLevelQueue()).to(topicExchange()).with("*.song.*");
}
@Bean
Binding bindTopic3() {
return BindingBuilder.bind(mailMessageQueue()).to(topicExchange()).with("*.song");
}
}
测试 :
浏览器 http://localhost:9010/sendTopicMsg?message="go go let do it" 简单测试
package com.song.songvue.test;
import com.song.songvue.config.rabbitmq.RabbitMQConfig;
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;
@RestController
public class TestQueue {
@Autowired
private AmqpTemplate amqpTemplate;
// 测试直连交换器
@RequestMapping("/sendMsg")
public String sendMsg(String message) {
amqpTemplate.convertAndSend(RabbitMQConfig.SONG_EXCHANGE, RabbitMQConfig.ROUTING_KEY, message);
return "OK";
}
// 测试fanout类型交换器
@RequestMapping("/sendFanoutMsg")
public String sendFanoutMsg(String message) {
amqpTemplate.convertAndSend(RabbitMQConfig.SONG_FANOUT, null, message);
return "OK";
}
// 测试topic类型交换器
@RequestMapping("/sendTopicMsg")
public String sendTopicMsg(String message) {
amqpTemplate.convertAndSend(RabbitMQConfig.SONG_TOPIC_EXCHANGE, "java.song", message);
return "OK";
}
}