Spring Boot中使用RabbitMQ
一 Message Broker与AMQP简介
Message Broker是一种消息验证、传输、路由的架构模式,其设计目标主要应用于下面这些场景:
-
消息路由到一个或多个目的地
-
消息转化为其他的表现方式
-
执行消息的聚集、消息的分解,并将结果发送到他们的目的地,然后重新组合相应返回给消息用户
-
调用Web服务来检索数据
-
响应事件或错误
-
使用发布-订阅模式来提供内容或基于主题的消息路由
AMQP是Advanced Message Queuing Protocol的简称,它是一个面向消息中间件的开放式标准应用层协议。AMQP定义了这些特性:
-
消息方向
-
消息队列
-
消息路由(包括:点到点和发布-订阅模式)
-
可靠性
-
安全性
二 RabbitMQ
RabbitMQ就是以AMQP协议实现的一种中间件产品,它可以支持多种操作系统,多种编程语言,几乎可以覆盖所有主流的企业级技术平台。
安装完RabbitMQ后,打开浏览器并访问:http://localhost:15672/,并使用默认用户guest登录,密码也为guest。我们可以看到如下图的管理页面:
从图中,我们可以看到一些基本概念,比如:Connections、Channels、Exchanges、Queue等。
点击Admin标签,在这里可以进行用户的管理。
三 实战
1 新建pom
<dependencies>
<!--spring-boot-starter-amqp用于支持RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2 配置application.properties
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
#用户名和密码请到RabbitMQ的管理页面去配置
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3 创建生产者
package com.didispace.rabbit;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Sender {
/*
创建消息生产者Sender。通过注入AmqpTemplate接口的实例来实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。
在Spring Boot中会根据配置来注入其具体实现。
*/
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "hello " + new Date();
System.out.println("Sender : " + context);
//在该生产者,我们会产生一个字符串,并发送到名为hello的队列中。
this.rabbitTemplate.convertAndSend("hello", context);
}
}
4 创建消费者
package com.didispace.rabbit;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
/*
创建消息消费者Receiver。
通过@RabbitListener注解定义该类对hello队列的监听。
该消费者实现了对hello队列的消费,消费操作为输出消息的字符串内容。
* */
@RabbitListener(queues = "hello")
public class Receiver {
@RabbitHandler
//@RabbitHandler注解来指定对消息的处理方法
public void process(String hello) {
System.out.println("Receiver : " + hello);
}
}
5 创建RabbitMQ的配置类
package com.didispace.rabbit;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
创建RabbitMQ的配置类RabbitConfig,用来配置队列、交换器、路由等高级信息。
这里先以最小化的配置来定义,以完成一个基本的生产和消费过程。
*/
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}
6 启动类
package com.didispace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
7 测试类
package com.didispace;
import com.didispace.rabbit.Sender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
public class HelloApplicationTests {
@Autowired
private Sender sender;
@Test
public void hello() throws Exception {
sender.send();
}
}
四 测试
1 启动hello函数的测试
2 观察RabbitMQ管理页面的变化
五 参考