SpringBoot框架已經提供了RabbitMQ的使用jar包,開發人員在使用RabbitMQ的時候只須要引用jar包簡單的配置一下就可使用RabbitMQ,這極大的簡化了開發人員的開發成本,提高開發效率。java
話很少說,直接上代碼:git
先在pom.xml
文件添加依賴spring-boot-starter-amqp
以下:github
<?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
文件中配置:spring
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的方式,在代碼裏配置綁定隊列和交換機:apache
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"); } }
生產者:springboot
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); } }
消費者:app
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的生產消費:框架
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上, 地址爲:maven
Spring Boot 教程、技術棧、示例代碼spring-boot
聯繫我
關注公衆號:java之旅