一。 下载Erlang,RabbitMq并安装;见 https://blog.csdn.net/zhaodj5660/article/details/79813749
二。 安装RabbitMQ-Plugins,这个相当于是一个管理界面,方便我们在浏览器界面查看RabbitMQ各个消息队列以及exchange的工作情况,安装方法是:打开命令行cd进入rabbitmq的sbin目录(我的目录是:D:Program FilesRabbitMQServerrabbitmq_server-3.7.4sbin),输入:rabbitmq-plugins enable rabbitmq_management命令,稍等会会发现出现plugins安装成功的提示,默认是安装6个插件:
三。上代码--新建一个SpringBoot项目rabbitmq-hello;
导包:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46<?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.zdj.rabbitmq</groupId> <artifactId>rabbitmq-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>rabbitmq-hello</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--rabbitmq 依赖--> <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>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>
3.1 application.yaml中配置RabbitMq的相关配置信息
1
2
3
4
5
6
7
8spring: application: name: rabbitmq-hello rabbitmq: host: localhost port: 5672 username: guest password: guest
3.2 配置RabbitMq配置类;
1
2
3
4
5
6
7
8
9
10
11
12
13
14package com.zdj.rabbitmq; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by Administrator on 2018/4/8. */ @Configuration public class RabbitConfig { @Bean public Queue helloQueue(){ return new Queue("hello"); } }
3.3 发送消息类HelloSender ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package com.zdj.rabbitmq; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; /** * Created by Administrator on 2018/4/8. */ @Component public class HelloSender { @Autowired private AmqpTemplate rabbitTemplate; public void send(){ String context="hello,传递消息时间 "+ new Date(); System.out.println("Sender : "+context); this.rabbitTemplate.convertAndSend("hello",context); } }
3.4 接收消息类 HelloReceiver;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.zdj.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * Created by Administrator on 2018/4/8. */ @Component @RabbitListener(queues = "hello") public class HelloReceiver { @RabbitHandler public void process(String str){ System.out.println("Receiver: "+str); } }
3.5 编写测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package com.zdj.rabbitmq; 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(classes = RabbitmqHelloApplication.class) // 启动接收应用,测试发送的测试类 public class RabbitmqHelloApplicationTests { @Autowired private HelloSender sender; @Test public void hello() throws Exception{ sender.send(); } }
四。测试运行
4.1 启动应用主类RabbitmqHelloApplication,从控制台中,我们看到如下内容,程序创建了一个访问127.0.0.1:5672
中 guest 的连接。(运行接收消息的应用程序,即程序入口类;RabbitmqHelloApplication)
可以看到创建了一个新的连接;同时,我们通过RabbitMQ的控制面板,可以看到Connection和Channels中包含当前连接的条目。
4.2 运行单元测试类 RabbitmqHelloApplicationTests ; 我们可以看到控制台中输出下面的内容,消息被发送到了RabbitMQ Server的hello
队列中。
4.3 切换到应用主类的控制台,我们可以看到类似如下输出,消费者对hello
队列的监听程序执行了,并输出了接受到的消息信息。
五: 总结:
通过上面的简单示例,我们在Spring Boot应用中引入spring-boot-starter-amqp
模块,进行简单配置就完成了对RabbitMQ的消息生产和消费的开发内容。实际应用中远不止这么简单,具体查阅RabbitMq官方教程。
最后
以上就是明亮过客最近收集整理的关于SpringBoot整合RabbitMq之简单演示(Direct模式)的全部内容,更多相关SpringBoot整合RabbitMq之简单演示(Direct模式)内容请搜索靠谱客的其他文章。
发表评论 取消回复