文章目录
系列文章:
RabbitMQ 系列文章
1. 概述
本篇增加了常见的配置语法,包括交换机类型等,并提供一个url触发发送数据,额外提供了swagger,方法发送数据。
2. 代码
pom.xml
<?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>
<groupId>com.baiqi</groupId>
<artifactId>springboot_rabbitmq</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<name>springboot_rabbitmq Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springboot_rabbitmq</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
application.properties:
server.port=8080
#这几个是默认配置。 不配也行。
spring.rabbitmq.host=10.40.65.183
spring.rabbitmq.port=5672
spring.rabbitmq.username=bq123
spring.rabbitmq.password=bq123
spring.rabbitmq.addresses=81.71.140.7
spring.rabbitmq.virtual-host=/baiqi
#spring.rabbitmq.addresses=172.16.48.10:5672,172.16.48.11:5672,172.16.48.12:5672
RabbitApplication :
package com.baiqi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitApplication.class, args);
}
}
Swagger2 :
package com.baiqi.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
// http://127.0.0.1:8080/swagger-ui.html
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.baiqi"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("图灵白起-Spring Boot中使用spring-boot-starter-amqp集成rabbitmq")
.description("测试SpringBoot整合进行各种工作模式信息的发送")
/*
.termsOfServiceUrl("https://www.jianshu.com/p/c79f6a14f6c9")
*/
.contact("roykingw")
.version("1.0")
.build();
}
}
ProducerController:
主要侧重 注入RabbitTemplate
package com.baiqi.springboot;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
@RestController
public class ProducerController {
@Autowired
private RabbitTemplate rabbitTemplate;
//helloWorld 直连模式
@ApiOperation(value="helloWorld发送接口",notes="直接发送到队列")
@GetMapping(value="/helloWorldSend")
public Object helloWorldSend(String message) throws AmqpException, UnsupportedEncodingException {
//设置部分请求参数
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//发消息
rabbitTemplate.send("helloWorldqueue",new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : "+message;
}
//工作队列模式
@ApiOperation(value="workqueue发送接口",notes="发送到所有监听该队列的消费")
@GetMapping(value="/workqueueSend")
public Object workqueueSend(String message) throws AmqpException, UnsupportedEncodingException {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//制造多个消息进行发送操作
for (int i = 0; i <10 ; i++) {
rabbitTemplate.send("work_sb_mq_q", new Message(message.getBytes("UTF-8"),messageProperties));
}
return "message sended : "+message;
}
// pub/sub 发布订阅模式 交换机类型 fanout
@ApiOperation(value="fanout发送接口",notes="发送到fanoutExchange。消息将往该exchange下的所有queue转发")
@GetMapping(value="/fanoutSend")
public Object fanoutSend(String message) throws AmqpException, UnsupportedEncodingException {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send("fanoutExchange", "", new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : "+message;
}
//routing路由工作模式 交换机类型 direct
@ApiOperation(value="direct发送接口",notes="发送到directExchange。exchange转发消息时,会往routingKey匹配的queue发送")
@GetMapping(value="/directSend")
public Object routingSend(String routingKey,String message) throws AmqpException, UnsupportedEncodingException {
if(null == routingKey) {
routingKey="china.changsha";
}
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send("directExchange", routingKey, new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : routingKey >"+routingKey+";message > "+message;
}
//topic 工作模式 交换机类型 topic
@ApiOperation(value="topic发送接口",notes="发送到topicExchange。exchange转发消息时,会往routingKey匹配的queue发送,*代表一个单词,#代表0个或多个单词。")
@GetMapping(value="/topicSend")
public Object topicSend(String routingKey,String message) throws AmqpException, UnsupportedEncodingException {
if(null == routingKey) {
routingKey="changsha.kf";
}
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send("topicExchange", routingKey, new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : routingKey >"+routingKey+";message > "+message;
}
}
ConcumerReceiver :
package com.baiqi.springboot;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class ConcumerReceiver {
//直连模式的多个消费者,会分到其中一个消费者进行消费。类似task模式
//通过注入RabbitContainerFactory对象,来设置一些属性,相当于task里的channel.basicQos
@RabbitListener(queues="helloWorldqueue")
public void helloWorldReceive(String message) {
System.out.println("helloWorld模式 received message : " +message);
}
//工作队列模式
@RabbitListener(queues="work_sb_mq_q")
public void wordQueueReceiveq1(String message) {
System.out.println("工作队列模式1 received message : " +message);
}
@RabbitListener(queues="work_sb_mq_q")
public void wordQueueReceiveq2(String message) {
System.out.println("工作队列模式2 received message : " +message);
}
//pub/sub模式进行消息监听
@RabbitListener(queues="fanout.q1")
public void fanoutReceiveq1(String message) {
System.out.println("发布订阅模式1received message : " +message);
}
@RabbitListener(queues="fanout.q2")
public void fanoutReceiveq2(String message) {
System.out.println("发布订阅模式2 received message : " +message);
}
//Routing路由模式
@RabbitListener(queues="direct_sb_mq_q1")
public void routingReceiveq1(String message) {
System.out.println("Routing路由模式routingReceiveq11111 received message : " +message);
}
@RabbitListener(queues="direct_sb_mq_q2")
public void routingReceiveq2(String message) {
System.out.println("Routing路由模式routingReceiveq22222 received message : " +message);
}
//topic 模式
//注意这个模式会有优先匹配原则。例如发送routingKey=hunan.IT,那匹配到hunan.*(hunan.IT,hunan.eco),之后就不会再去匹配*.ITd
@RabbitListener(queues="topic_sb_mq_q1")
public void topicReceiveq1(String message) {
System.out.println("Topic模式 topic_sb_mq_q1 received message : " +message);
}
@RabbitListener(queues="topic_sb_mq_q2")
public void topicReceiveq2(String message) {
System.out.println("Topic模式 topic_sb_mq_q2 received message : " +message);
}
}
2.1 配置各种交换机和队列的语法
2.1.1 路由模式
路由模式|Routing模式 交换机类型:direct:
package com.baiqi.springboot.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
路由模式|Routing模式 交换机类型:direct
*/
@Configuration
public class DirectConfig {
//声明队列
@Bean
public Queue directQ1() {
return new Queue("direct_sb_mq_q1");
}
@Bean
public Queue directQ2() {
return new Queue("direct_sb_mq_q2");
}
//声明exchange
@Bean
public DirectExchange setDirectExchange() {
return new DirectExchange("directExchange");
}
//声明binding,需要声明一个routingKey
@Bean
public Binding bindDirectBind1() {
return BindingBuilder.bind(directQ1()).to(setDirectExchange()).with("china.changsha");
}
@Bean
public Binding bindDirectBind2() {
return BindingBuilder.bind(directQ2()).to(setDirectExchange()).with("china.beijing");
}
}
2.1.2 Fanout广播模式(发布订阅模式)
Fanout模式需要声明exchange,并绑定queue,由exchange负责转发到queue上。
广播模式 交换机类型设置为:fanout
package com.baiqi.springboot.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Fanout模式需要声明exchange,并绑定queue,由exchange负责转发到queue上。
* 广播模式 交换机类型设置为:fanout
*/
@Configuration
public class FanoutConfig {
//声明队列
@Bean
public Queue fanoutQ1() {
return new Queue("fanout.q1");
}
@Bean
public Queue fanoutQ2() {
return new Queue("fanout.q2");
}
//声明exchange
@Bean
public FanoutExchange setFanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
//声明Binding,exchange与queue的绑定关系
@Bean
public Binding bindQ1() {
return BindingBuilder.bind(fanoutQ1()).to(setFanoutExchange());
}
@Bean
public Binding bindQ2() {
return BindingBuilder.bind(fanoutQ2()).to(setFanoutExchange());
}
}
2.1.3 HelloWorld直连模式
HelloWorld rabbitmq课上讲解的第一个工作模式
直连模式只需要声明队列,所有消息都通过队列转发。
无需设置交换机
package com.baiqi.springboot.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* HelloWorld rabbitmq课上讲解的第一个工作模式
* 直连模式只需要声明队列,所有消息都通过队列转发。
* 无需设置交换机
*/
@Configuration
public class HelloWorldConfig {
@Bean
public Queue setQueue() {
return new Queue("helloWorldqueue");
}
}
2.1.4 Topics通配符模式
Topics模式 交换机类型 topic
package com.baiqi.springboot.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
Topics模式 交换机类型 topic
* */
@Configuration
public class TopicConfig {
//声明队列
@Bean
public Queue topicQ1() {
return new Queue("topic_sb_mq_q1");
}
@Bean
public Queue topicQ2() {
return new Queue("topic_sb_mq_q2");
}
//声明exchange
@Bean
public TopicExchange setTopicExchange() {
return new TopicExchange("topicExchange");
}
//声明binding,需要声明一个roytingKey
@Bean
public Binding bindTopicHebei1() {
return BindingBuilder.bind(topicQ1()).to(setTopicExchange()).with("changsha.*");
}
@Bean
public Binding bindTopicHebei2() {
return BindingBuilder.bind(topicQ2()).to(setTopicExchange()).with("#.beijing");
}
}
2.1.5 Work queues 工作队列模式
和Hello World!入门模式没什么区别,区别在于消费端
package com.baiqi.springboot.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WorkConfig {
//声明队列
@Bean
public Queue workQ1() {
return new Queue("work_sb_mq_q");
}
}
2.2 运行
访问 http://127.0.0.1:8080/swagger-ui.html
输入信息,有的需要相关的routekey才会打印: