spring boot 1.5.4 整合rabbitMQ(十七)
1.2.2 创建spring-boot-MQ工程
spring-boot-rabbitMQ
项目源码,
码云地址:https://git.oschina.net/wyait/springboot1.5.4.git
github地址:https://github.com/wyait/spring-boot-1.5.4.git
pom.xml文件:
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- spring boot项目的parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<groupId>com.wyait.mq</groupId>
<artifactId>spring-boot-mq</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<!-- spring boot 引入Web模块。自动配置:tomcat、springmvc、jackson等 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- test使用 -->
<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-log4j</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<!-- 整合rabbitmq,添加amqp依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 配置spring boot之maven插件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application启动类:
@Configuration
@SpringBootApplication
public class MqApplication {
public static void main(String[] args) {
SpringApplication.run(MqApplication.class, args);
}
}
添加log4j.properties、application.properties、application-dev.properties配置文件。
application-dev.properties配置文件中添加rabbitmq配置:
spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=wyait
spring.rabbitmq.password=wyait
spring.rabbitmq.virtual-host=/
# p端收到回调,确认消息发送结果
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.template.mandatory=true
编写RabbitMqConfig配置类:
创建RabbitMQ的配置类RabbitMqConfig,用来配置队列、交换器、路由等高级信息。这里我们以入门为主,先以最小化的配置来定义,以完成一个基本的生产和消费过程。
@Configuration
public class RabbitMqConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}
编写Sender类:
创建消息生产者Sender。通过注入AmqpTemplate接口的实例来实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入其具体实现。在该生产者,我们会产生一个字符串,并发送到名为hello的队列中。
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitMQTemplate;
public void send() {
String context = "hello :" + new Date();
System.out.println("Sender : " + context);
this.rabbitMQTemplate.convertAndSend("hello",context);
}
}
编写Recevier类:
创建消息消费者Receiver。通过@RabbitListener注解定义该类对hello队列的监听,并用@RabbitHandler注解来指定对消息的处理方法。所以,该消费者实现了对hello队列的消费,消费操作为输出消息的字符串内容。
@Component
// 监听“hello”队列
@RabbitListener(queues ="hello")
public class Receiver {
@RabbitHandler
// handler注解来指定对消息的处理方法
public void process(String hello) {
System.out.println("Receiver:" + hello);
}
}
编写test类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes =MqApplication.class)
public class MqApplicationTest{
@Autowired
private Sender send;
@Test
public void test() {
System.out.println("==========发送消息!");
send.send();
}
}
完成程序编写之后,下面开始尝试运行。首先确保RabbitMQ Server已经开始,然后进行下面的操作:
-
启动应用主类,从控制台中,我们看到如下内容,程序创建了一个访问127.0.0.1:5672中wyait的连接。
Created newconnection: rabbitConnectionFactory#1e456bc3:0/[email protected] [delegate=amqp://[email protected]:5672/,localPort= 2801]
同时,我们通过RabbitMQ的控制面板,可以看到Connection和Channels中包含当前连接的条目,Queues中查看队列概况。
-
运行单元测试类,我们可以看到控制台中输出下面的内容,消息被发送到了RabbitMQ Server的hello队列中。
==========发送消息!
Sender : hello:Thu Sep 14 16:06:54 CST 2017
-
切换到应用主类的控制台,我们可以看到类似如下输出,消费者对hello队列的监听程序执行了,并输出了接受到的消息信息。
通过上面的示例,我们在Spring Boot应用中引入spring-boot-starter-amqp模块,进行简单配置就完成了对RabbitMQ的消息生产和消费的开发内容。然而在实际应用中,还有很多内容没有演示,下面继续研究其他消息队列模式,大家也可以查阅RabbitMQ的官方教程,有更全面的了解。