1、概述
前面我们已经进行了入门程序的编写,也讲解了两种工作模式:发布/订阅模式和路由模式。这一次进行SpringBoot和RabbitMQ的整合开发,用到的工作模式还是路由模式。基于入门程序进行开发,整体的工程结构如下:
2、搭建环境
2.1 父工程的pom.xml
在rabbitmq的工程的pom文件中,完善依赖,具体如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>com.zdw.rabbitmq</groupId>
<artifactId>rabbitmq</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>demo-ribbitmq-prodcure</module>
<module>demo-ribbitmq-consumer</module>
</modules>
<properties>
<spring-boot.version>2.0.1.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.2 创建新的模块
在rabbitmq工程,new——》module,创建两个新的模块,生产者模块:springboot-rabbitmq-prodcure,消费者模块:springboot-rabbitmq-consumer。在两个模块的pom.xml中都加上如下依赖:
<dependencies>
<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>
</dependency>
</dependencies>
2.3 SpringBoot的配置文件application.yml
2.3.1 生产者的application.yml
在生产者模块的resoures目录下,创建application.yml
server:
port: 44000
spring:
application:
name: springboot-rabbitmq-prodcure
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
2.3.2 消费者的application.yml
在消费者模块的resoures目录下,创建application.yml
server:
port: 44001
spring:
application:
name: springboot-rabbitmq-consumer
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
2.4 创建启动类
生产者:在生产者的src/main/java下创建包:com.zdw.prodcure
package com.zdw.prodcure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Create By zdw on 2019/9/19
*/
@SpringBootApplication
public class SpringBootProdcureApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootProdcureApplication.class,args);
}
}
消费者:在消费者的src/main/java下创建包:com.zdw.consumer
package com.zdw.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Create By zdw on 2019/9/19
*/
@SpringBootApplication
public class SpringBootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootConsumerApplication.class,args);
}
}
3、创建RabbitMQ的主要配置类
3.1 生产者的RabbitMQ配置类
在生产者的src/main/java下创建包:com.zdw.prodcure.config,创建配置类RabbitConfig:
package com.zdw.prodcure.config;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Create By zdw on 2019/9/19
*/
@Configuration
public class RabbitConfig {
//定义队列,交换机和路由key
//public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
//public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
public static final String EXCHANGE_DIRECT_INFORM="exchange_direct_inform";
public static final String ROUTINGKEY_EMAIL="inform.email";
public static final String ROUTINGKEY_SMS="inform.sms";
//声明交换机,设置持久化
@Bean(EXCHANGE_DIRECT_INFORM)
public Exchange exchangeDeclare(){
return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_INFORM).durable(true).build();
}
}
3.1 消费者的RabbitMQ配置类
在消费者的src/main/java下创建包:com.zdw.consumer.config,创建配置类RabbitConfig:
package com.zdw.consumer.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Create By zdw on 2019/9/19
*/
@Configuration
public class RabbitConfig {
//定义队列,交换机和路由key
public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
public static final String EXCHANGE_DIRECT_INFORM="exchange_direct_inform";
public static final String ROUTINGKEY_EMAIL="inform.email";
public static final String ROUTINGKEY_SMS="inform.sms";
//声明交换机,设置持久化
@Bean(EXCHANGE_DIRECT_INFORM)
public Exchange exchangeDeclare(){
return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_INFORM).durable(true).build();
}
//声明短信队列
@Bean(QUEUE_INFORM_SMS)
public Queue queueMsg(){
return new Queue(QUEUE_INFORM_SMS);
}
//声明邮件队列
@Bean(QUEUE_INFORM_EMAIL)
public Queue queueEmail(){
return new Queue(QUEUE_INFORM_EMAIL);
}
//短信队列绑定到交换机,指定路由key
@Bean
public Binding bindMsgToExchange(@Qualifier(QUEUE_INFORM_SMS) Queue queueMsg,
@Qualifier(EXCHANGE_DIRECT_INFORM) Exchange exchange){
return BindingBuilder.bind(queueMsg).to(exchange).with(ROUTINGKEY_SMS).noargs();
}
//邮件队列绑定到交换机,指定路由key
@Bean
public Binding bindEmailToExchange(@Qualifier(QUEUE_INFORM_EMAIL) Queue queueEmail,
@Qualifier(EXCHANGE_DIRECT_INFORM) Exchange exchange){
return BindingBuilder.bind(queueEmail).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
}
}
4、代码实现
4.1 生产者代码
生产者使用测试类来发送消息到RabbitMQ,在src/main/test下面创建包:com.zdw.prodcure.test,创建测试类:ProdcureTest
SpringBoot提供了一个模板类用来发送消息:RabbitTemplate,具体代码如下:
package com.zdw.prodcure.test;
import com.zdw.prodcure.config.RabbitConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Create By zdw on 2019/9/19
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProdcureTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testProdcureToMsg(){
String message = "SpringBoot整合RabbitMQ发送给短信队列(QUEUE_MSG)的消息";
rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_DIRECT_INFORM,RabbitConfig.ROUTINGKEY_SMS,message);
System.out.println("Send message is :"+message+"-----");
}
@Test
public void testProdcureToEmail(){
String message = "SpringBoot整合RabbitMQ发送给邮件队列(QUEUE_EMAIL)的消息";
rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_DIRECT_INFORM,RabbitConfig.ROUTINGKEY_EMAIL,message);
System.out.println("Send message is :"+message+"-----");
}
}
4.2 消费者代码
创建包:com.zdw.consumer.handler,创建消息处理类ConsumerMsgHandler
消费者使用注解@RabbitListener来监听队列
package com.zdw.consumer.handler;
import com.rabbitmq.client.Channel;
import com.zdw.consumer.config.RabbitConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Create By zdw on 2019/9/19
* 监听消息
*/
@Component
public class ConsumerMsgHandler {
//监听短信队列
@RabbitListener(queues = {RabbitConfig.QUEUE_INFORM_SMS})
public void msgConsumer(String msg, Message message, Channel channel){
System.out.println("receive sms msg is:"+msg);
System.out.println("receive sms message is:"+message.getBody());
System.out.println("receive sms channel is:"+channel);
}
//监听邮件队列
@RabbitListener(queues = {RabbitConfig.QUEUE_INFORM_EMAIL})
public void emailConsumer(String msg, Message message, Channel channel){
System.out.println("receive sms msg is:"+msg);
System.out.println("receive sms message is:"+message.getBody());
System.out.println("receive sms channel is:"+channel);
}
}
先启动生产者的测试类,可以通过地址:http://localhost:15672 查看,已经声明了一个交换机(这是这次启动生产者的作用),但是这次发送的消息会丢失,因为没有绑定队列。当然其实可以先启动消费者,因为我们的消费者的RabbitConfig类中,也进行了交换机的声明操作。
代码下载:https://download.csdn.net/download/zengdongwen/11784368