700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring Boot 集成RabbitMQ

Spring Boot 集成RabbitMQ

时间:2019-12-31 17:52:19

相关推荐

Spring Boot 集成RabbitMQ

在Spring Boot中整合RabbitMQ是非常容易的,通过在Spring Boot应用中整合RabbitMQ,实现一个简单的发送、接收消息的例子。

首先需要启动RabbitMQ服务,并且add一个账户lg或使用guest账户

1. 创建一个Spring Boot项目,pom如下

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><groupId>com.luangeng</groupId><artifactId>boot</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></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><!-- TEST --><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>

2.配置文件

spring.application.name=rabbitmqspring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=lgspring.rabbitmq.password=lg

3.新增类Sender, 发送方,使用RabbitTemplate发送消息

@Componentpublic class Sender {@Autowiredprivate AmqpTemplate rabbitTemplate;private static AtomicInteger count = new AtomicInteger(1);public void send() {String msg = "msg" + count.getAndIncrement() + " at " + new Date();System.out.println("Sender : " + msg);this.rabbitTemplate.convertAndSend(RabbitConfig.queueName, msg);}}

4.新增类Receive, 消息接受方,使用@RabbitListener包装一个Queue

import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.ponent;@Component@RabbitListener(queues = RabbitConfig.queueName)public class Receiver {@RabbitHandlerpublic void process(String msg) {System.out.println("Receiver : " + msg);}}

5.Rabbit配置类,作用是初始化Queue和Exchange等

import org.springframework.amqp.core.*;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class RabbitConfig {public final static String queueName = "myQueue";//注入queue @BeanQueue queue() {return new Queue(queueName, false);}//注入exchange @BeanExchange exchange() {//return new FanoutExchange("fanout-exchange");//return new DirectExchange("direct-exchange");//return new HeadersExchange("headers-exchange");return new TopicExchange("topic-exchange");}//绑定exchange和queue @BeanBinding binding(Queue queue, TopicExchange exchange) {return BindingBuilder.bind(queue).to(exchange).with(queueName);}}

6.主类

@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

7.测试类

@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = Application.class)public class ApplicationTest {@Autowiredprivate Sender sender;@Testpublic void hello() throws Exception {while(true) {sender.send();Thread.sleep(1000);}}}

---

运行测试类,控制台输出如下:

Receiver : msg514 at Sun Aug 06 11:21:03 CST

Sender : msg515 at Sun Aug 06 11:21:04 CST

Receiver : msg515 at Sun Aug 06 11:21:04 CST

Sender : msg516 at Sun Aug 06 11:21:05 CST

Receiver : msg516 at Sun Aug 06 11:21:05 CST

Sender : msg517 at Sun Aug 06 11:21:06 CST

Receiver : msg517 at Sun Aug 06 11:21:06 CST

Sender : msg518 at Sun Aug 06 11:21:07 CST

Receiver : msg518 at Sun Aug 06 11:21:07 CST

此时Rabbit管理页面显示:

说明消息通过RabbitMQ发送接收成功。

end

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。