内容简介:mq在异步解耦削峰的优势非常突出,现在很多的项目都会用到,掌握mq的知识点,了解如何顺畅的使用mq,可以说是一个必备的职业技能点了接下来我们进入rabbitmq的学习过程在测试之前,需要安装rabbitmq,下面分别给出mac + centos的安装教程
mq在异步解耦削峰的优势非常突出,现在很多的项目都会用到,掌握mq的知识点,了解如何顺畅的使用mq,可以说是一个必备的职业技能点了
接下来我们进入rabbitmq的学习过程
I. 环境准备
在测试之前,需要安装rabbitmq,下面分别给出mac + centos的安装教程
1. mac 安装
安装命令
brew install rabbitmq ## 进入安装目录 cd /usr/local/Cellar/rabbitmq/3.7.5 # 启动 brew services start rabbitmq # 当前窗口启动 rabbitmq-server
启动控制台之前需要先开启插件
./rabbitmq-plugins enable rabbitmq_management
进入控制台: http://localhost:15672/
用户名和密码:guest,guest
2. centos 安装
安装命令
yum install erlang wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el6.noarch.rpm yum install rabbitmq-server-3.6.15-1.el6.noarch.rpm
插件开启
rabbitmq-plugins enable rabbitmq_management # 启动 rabbitmq-server -detached
3. 配置
添加账号,设置权限
## 添加账号 ./rabbitmqctl add_user admin admin ## 添加访问权限 ./rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*" ## 设置超级权限 ./rabbitmqctl set_user_tags admin administrator
4. 项目环境
接下我们创建一个SpringBoot项目,用于简单的体验一下rabbitmq的发布和消费消息
2.2.1.RELEASE 3.7.5
依赖配置文件pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.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>
<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>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
在 application.yml 配置文件中,添加rabbitmq的相关属性
spring:
rabbitmq:
virtual-host: /
username: admin
password: admin
port: 5672
II. 实例演示
接下来我们看一个 hello world 版本的rabbitmq的使用姿势,一个简单发布消息、消费消息
1. 发布消息
消息发布,我们主要借助 AmqpTemplate 来实现
@Component
public class PublishDemo {
@Autowired
private AmqpTemplate amqpTemplate;
public String publish2mq(String ans) {
String msg = "hello world = " + ans;
System.out.println("publish: " + msg);
amqpTemplate.convertAndSend(Pkg.exchange, Pkg.routing, msg);
return msg;
}
}
上面的case中,主要方法在于 amqpTemplate#convertAndSend ,第一个参数为exchangeName, 第二个为routingKey
常量配置如下
class Pkg {
final static String exchange = "topic.e";
final static String routing = "r";
final static String queue = "topic.a";
}
2. 消费消息
消费消息,需要指定Queue,通过routingKey绑定exchange,如下
@Service
public class ConsumerDemo {
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = Pkg.queue, durable = "false", autoDelete = "true"),
exchange = @Exchange(value = Pkg.exchange, ignoreDeclarationExceptions = "true",
type = ExchangeTypes.TOPIC), key = Pkg.routing))
public void consumer(String msg) {
System.out.println("consumer msg: " + msg);
}
}
3. 测试demo
写一个简单的rest接口,用于接收参数,发布消息到mq,并被 ConsumerDemo 消费
@RestController
public class PubRest {
@Autowired
private PublishDemo publishDemo;
@GetMapping(path = {"", "/", "/publish"})
public String publish(String name) {
return publishDemo.publish2mq(name);
}
}
II. 其他
0. 项目
- 工程: https://github.com/liuyueyi/spring-boot-demo
- 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/300-rabbitmq
1. 一灰灰Blog
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
- 一灰灰Blog个人博客 https://blog.hhui.top
- 一灰灰Blog-Spring专题博客 http://spring.hhui.top
打赏 如果觉得我的文章对您有帮助,请随意打赏。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 降低云游戏延迟优化云游戏体验:贝塞斯达推出Orion技术,还公布了免费体验计划
- PyTorch 初体验
- indexedDB 初体验
- golang爬虫初体验
- Netty 入门初体验
- Ansible初体验
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Mastering JavaServer Faces
Bill Dudney、Jonathan Lehr、Bill Willis、LeRoy Mattingly / Wiley / 2004-6-7 / USD 40.00
Harness the power of JavaServer Faces to create your own server-side user interfaces for the Web This innovative book arms you with the tools to utilize JavaServer Faces (JSF), a new standard that wi......一起来看看 《Mastering JavaServer Faces》 这本书的介绍吧!