本文,講解 Spring Boot 如何集成 RabbitMQ,實現消息隊列。javascript
博客地址:blog.720ui.com/java
RabbitMQ 是一個在 AMQP 基礎上完整的,可複用的企業消息系統。git
關於 RabbitMQ 的使用,能夠閱讀以前的 RabbitMQ 實戰教程。github
Spring Boot 整合 RabbitMQ 是很是容易,只須要兩個步驟。spring
首先,在 pom.xml 中增長 RabbitMQ 依賴。springboot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>複製代碼
第二步,在 src/main/resources/application.properties 中配置信息。微信
#rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest複製代碼
咱們來實現一個簡單的發送、接收消息。app
在 Spring Boot 中使用 @Bean 註冊一個隊列。spring-boot
@Configuration
public class RabbitMQConfig {
public static final String QUEUE_NAME = "spring-boot-simple";
@Bean
public Queue queue() {
return new Queue(QUEUE_NAME);
}
}複製代碼
建立消息生產者 Sender。經過注入 AmqpTemplate 接口的實例來實現消息的發送。單元測試
@Service
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
System.out.println("梁桂釗 發送消息...");
rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "你好, 梁桂釗!");
}
}複製代碼
建立消息消費者 Receiver。經過 @RabbitListener 註解定義對隊列的監聽。
@Service
public class Receiver {
@Autowired
private AmqpTemplate rabbitTemplate;
@RabbitListener(queues = "spring-boot-simple")
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
}
}複製代碼
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.lianggzone.springboot" })
public class RunMain {
public static void main(String[] args) {
SpringApplication.run(RunMain.class, args);
}
}複製代碼
建立單元測試用例
public class RabbitMQTest {
@Autowired
private Sender sender;
@Test
public void send() throws Exception {
sender.send();
}
}複製代碼
通過上面的實戰案例,咱們對 Spring Boot 整合 RabbitMQ 有了必定的瞭解。如今,咱們再來看下 RabbitMQ 路由場景。
在 RabbitMQConfig 中,咱們註冊 隊列,轉發器,監聽等。
@Configuration
public class RabbitMQConfig2 {
public static final String QUEUE_NAME = "spring-boot";
public static final String QUEUE_EXCHANGE_NAME = "spring-boot-exchange";
@Bean
public Queue queue() {
// 是否持久化
boolean durable = true;
// 僅建立者能夠使用的私有隊列,斷開後自動刪除
boolean exclusive = false;
// 當全部消費客戶端鏈接斷開後,是否自動刪除隊列
boolean autoDelete = false;
return new Queue(QUEUE_NAME, durable, exclusive, autoDelete);
}
@Bean
public TopicExchange exchange() {
// 是否持久化
boolean durable = true;
// 當全部消費客戶端鏈接斷開後,是否自動刪除隊列
boolean autoDelete = false;
return new TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete);
}
@Bean
public Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(QUEUE_NAME);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
}複製代碼
建立消息生產者 Sender。經過注入 AmqpTemplate 接口的實例來實現消息的發送。
@Service
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
System.out.println("梁桂釗 發送消息...");
rabbitTemplate.convertAndSend(RabbitMQConfig2.QUEUE_NAME, "你好, 梁桂釗!");
}
}複製代碼
建立消息消費者 Receiver。經過 @RabbitListener 註解定義對隊列的監聽。
@Service
public class Receiver {
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
}
}複製代碼
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.lianggzone.springboot" })
public class RunMain {
public static void main(String[] args) {
SpringApplication.run(RunMain.class, args);
}
}複製代碼
建立單元測試用例
public class RabbitMQTest {
@Autowired
private Sender sender;
@Test
public void send() throws Exception {
sender.send();
}
}複製代碼
相關示例完整代碼: springboot-action
(完)
更多精彩文章,盡在「服務端鋪子」微信公衆號!