SpringBoot教程之RabbitMQ示例
SpringBoot框架已经提供了RabbitMQ的使用jar包,开发人员在使用RabbitMQ的时候只需要引用jar包简单的配置一下就可以使用RabbitMQ,这极大的简化了开发人员的开发成本,提升开发效率。
话不多说,直接上代码:
先在pom.xml
文件添加依赖spring-boot-starter-amqp
如下:
<?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>
<artifactId>spring-boot-rabbitmq</artifactId>
<version>1.0-SNAPSHOT</version>
<description>springboot整合RabbitMQ的示例</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在application.properties
文件中配置:
server.port=8085
spring.rabbitmq.host=host
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=password
spring.rabbitmq.virtual-host=virtual-host
我们以topic模式为例,springboot提供了一种用bean的方式,在代码里配置绑定队列和交换机:
代码语言:txt复制package com.example.topic;
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;
/**
* create rabbitmq queue exchange and bind by routingKey
*/
@Configuration
public class TopicRabbitMQConfig {
// 队列:queue.example.topic.new
@Bean
public Queue topicQueue() {
return new Queue("queue.example.topic.new");
}
// 交换机:exchange.topic.example.new
@Bean
TopicExchange topicExchange() {
return new TopicExchange("exchange.topic.example.new");
}
// 绑定关系:routing.key.example.new
@Bean
Binding bindingTopicExchange() {
return BindingBuilder
.bind(topicQueue())
.to(topicExchange())
.with("routing.key.example.new");
}
}
生产者:
代码语言:txt复制package com.example.topic;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TopicProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMessageByTopic() {
String content = "This is a topic type of the RabbitMQ message example";
this.rabbitTemplate.convertAndSend(
"exchange.topic.example.new",
"routing.key.example.new",
content);
}
}
消费者:
代码语言:txt复制package com.example.topic;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "queue.example.topic.new")
public class TopicConsumer {
@RabbitHandler
public void consumer(String message) {
System.out.println(message);
}
}
写一段测试代码测试一下,RabbitMQ的生产消费:
代码语言:txt复制package com.example;
import com.example.direct.DirectProducer;
import com.example.fanout.FanoutProducer;
import com.example.simple.SimpleProducer;
import com.example.topic.TopicProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTest {
@Autowired
private TopicProducer topicProducer;
@Test
public void topicProducerTest() {
topicProducer.sendMessageByTopic();
}
}
这样就能在SpringBoot中使用RabbitMQ了!
外三种模式:direct
、fanout
、head
的代码我放在了github上,
地址为:
Spring Boot 教程、技术栈、示例代码