RabbitMQ 最简单入门
1. 搭建
1.1 下载rabbitMq
RabbitMQ官网下载址:http://www.rabbitmq.com/install-windows.html
Erlang官网下载地址:http://www.erlang.org/downloads
直接官网下载、安装
RabbitMQ用Erlang编程语言开发的,需要安装Erlang语言开发包
1.2 环境变量
在命令窗口中切换到此次MQ的安装目录下的sbin,运行命令:rabbitmq-plugins enable rabbitmq_management,这样就可以添加可视化插件了。
启动rabbitmq,浏览器输入 http://127.0.0.1:15672/ 就可以访问了
1.3 添加用户
超级管理员(administrator)
可登陆管理控制台(启用management plugin的情况下),可查看所有的信息,并且可以对用户,策略(policy)进行操作。
监控者(monitoring)
可登陆管理控制台(启用management plugin的情况下),同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)
策略制定者(policymaker)
可登陆管理控制台(启用management plugin的情况下), 同时可以对policy进行管理。
普通管理者(management)
仅可登陆管理控制台(启用management plugin的情况下),无法看到节点信息,也无法对策略进行管理。
无角色
无法登陆管理控制台,通常就是普通的生产者和消费者。
新建一个virtual host
点击name 下 .*
给用户添加操作权限
检查 用户 virtual-host相互绑定
virtual-host 虚拟机就检查有没有绑定账号
2. 简单5种模式demo
.properties 文件
spring.application.name=spirng-boot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
.pom 文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>demo_1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<name>demo_1</name>
<description>Demo project for Spring Boot</description>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.1 简单队列
confing
package com.demo.easy;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfigEasy {
@Bean
public Queue queue(){
return new Queue("q_hello",true);
}
}
生产者
package com.demo.easy;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class RabbitSendEasy {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(){
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("Sender Time: " + date);
this.rabbitTemplate.convertAndSend("rabbitEasy",date);
}
}
消费者 使用注解声明队列
package com.demo.easy;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queuesToDeclare = @Queue("rabbitEasy"))
public class RabbitRecevier {
@RabbitHandler
public void process(String data){
System.out.println("Recevier1 消费消息 :"+data);
}
}
测试
package com.demo.easy;
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 RabbitTestEasy {
@Autowired
private RabbitSendEasy rabbitSendEasy;
@Test
public void sendEasy(){
rabbitSendEasy.send();
}
}
2.2 Work模式
confing
生产者 增加方法
package com.demo.easy;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class RabbitSendEasy {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(){
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("Sender Time: " + date);
this.rabbitTemplate.convertAndSend("q_hello",date);
}
public void send(int i){
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("index:"+i+" Sender Time: " + date);
this.rabbitTemplate.convertAndSend("q_hello",String.valueOf(i));
}
}
新建一个消费者
package com.demo.easy;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "q_hello")
public class RabbitRecevierEasy2 {
@RabbitHandler
public void process(String data){
System.out.println("Recevier2 消费消息 第:" + data + "条");
}
}
测试
package com.demo.easy;
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 RabbitTestEasy {
@Autowired
private RabbitSendEasy rabbitSendEasy;
@Test
public void sendEasy(){
rabbitSendEasy.send();
}
@Test
public void sendWork(){
for (int i = 0; i < 100; i++) {
rabbitSendEasy.send(i);
}
}
}
2.3 发布/订阅者模式
confing
package com.demo.Publish;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfigPublish {
@Bean
public Queue aMessage() {
return new Queue("rabbit_publish_a");
}
@Bean
public Queue bMessage() {
return new Queue("rabbit_publish_b");
}
@Bean
public Queue cMessage() {
return new Queue("rabbit_publish_c");
}
@Bean
FanoutExchange fanoutExchange(){
return new FanoutExchange("fanoutExchangePublish");
}
@Bean
Binding bindingExchangeA(Queue aMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(aMessage).to(fanoutExchange);
}
@Bean
Binding bindingExchangeB(Queue bMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(bMessage).to(fanoutExchange);
}
@Bean
Binding bindingExchangeC(Queue cMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(cMessage).to(fanoutExchange);
}
}
生产者
package com.demo.Publish;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitSendPublish {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(Integer i){
System.out.println("发布 Sender : " + i);
amqpTemplate.convertAndSend("fanoutExchangePublish","",i);
}
}
建立三个消费者
修改监听队列名称 @RabbitListener(queues = "rabbit_publish_a")
修改控制台日志方面观看 System.out.println("A:订阅 消息:"+i);
其余代码相同
消费者
package com.demo.Publish;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "rabbit_publish_a")
public class RabbitRecevier1Publish {
@RabbitHandler
public void recevier(Integer i){
System.out.println("A:订阅 消息:"+i);
}
}
测试
package com.demo.Publish;
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 RabblitTestPublish {
@Autowired
private RabbitSendPublish rabbitSendPublish;
@Test
public void send1(){
rabbitSendPublish.send(15672);
}
}
2.4 路由模式
confing
package com.demo.routing;
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;
@Configuration
public class RabiitConfigRouting {
@Bean
TopicExchange topicExchange() {
return new TopicExchange("routingTopicExchange");
}
@Bean
public Queue messageA(){
return new Queue("rabbit_routing_a");
}
@Bean
public Queue messageB(){
return new Queue("rabbit_routing_b");
}
@Bean
public Queue messageC(){
return new Queue("rabbit_routing_c");
}
@Bean
Binding bindingExchangeMessageA(Queue messageA, TopicExchange topicExchange) {
return BindingBuilder.bind(messageA).to(topicExchange).with("rabbitRouting.A");
}
@Bean
Binding bindingExchangeMessageB(Queue messageB, TopicExchange topicExchange) {
return BindingBuilder.bind(messageB).to(topicExchange).with("rabbitRouting.A");
}
@Bean
Binding bindingExchangeMessageC(Queue messageC, TopicExchange topicExchange) {
return BindingBuilder.bind(messageC).to(topicExchange).with("rabbitRouting.C");
}
}
生产者
package com.demo.routing;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitSendRouting {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(Integer i){
System.out.println("Send key : rabbitRoutingA i;"+i);
amqpTemplate.convertAndSend("routingTopicExchange","rabbitRouting.A",i);
}
public void send2(Integer i){
System.out.println("Send key : rabbitRoutingB i;"+i);
amqpTemplate.convertAndSend("routingTopicExchange","rabbitRouting.B",i);
}
public void send3(Integer i){
System.out.println("Send key : rabbitRoutingC i;"+i);
amqpTemplate.convertAndSend("routingTopicExchange","rabbitRouting.C",i);
}
}
创建三个消费者
修改 @RabbitListener(queues = “rabbit_routing_a”)
@RabbitListener(queues = “rabbit_routing_b”)
@RabbitListener(queues = “rabbit_routing_c”)
修改 System.out.println(“Recevier1/2/3 消费消息 i:”+i);
package com.demo.routing;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "rabbit_routing_a")
public class RabbitRecevier1Routing {
@RabbitHandler
public void recevier(Integer i){
System.out.println("Recevier1 消费消息 i:"+i);
}
}
测试
package com.demo.routing;
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 RabbitTestRouting {
@Autowired
private RabbitSendRouting rabbitSendRouting;
@Test
public void send1(){
rabbitSendRouting.send(15672);
}
@Test
public void send2(){
rabbitSendRouting.send2(15672);
}
@Test
public void send3(){
rabbitSendRouting.send3(15672);
}
}
test 1
test3
2.5 通配符模式
confing
package com.demo.routing;
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;
@Configuration
public class RabiitConfigRouting {
@Bean
TopicExchange topicExchange() {
return new TopicExchange("routingTopicExchange");
}
@Bean
public Queue messageA(){
return new Queue("rabbit_routing_a");
}
@Bean
public Queue messageB(){
return new Queue("rabbit_routing_b");
}
@Bean
public Queue messageC(){
return new Queue("rabbit_routing_c");
}
@Bean
Binding bindingExchangeMessageA(Queue messageA, TopicExchange topicExchange) {
return BindingBuilder.bind(messageA).to(topicExchange).with("rabbitRouting.*");
}
@Bean
Binding bindingExchangeMessageB(Queue messageB, TopicExchange topicExchange) {
return BindingBuilder.bind(messageB).to(topicExchange).with("rabbitRouting.#");
}
@Bean
Binding bindingExchangeMessageC(Queue messageC, TopicExchange topicExchange) {
return BindingBuilder.bind(messageC).to(topicExchange).with("rabbitRouting.C");
}
}
修改路由模式 Configuration
test1
test2
test3
#:匹配一个或多个单词 每个词以.分隔
*:只能匹配一个词
2.6 总结
简单模式: 1对1
work模式: 1对多 每一个消息必须只能一个消费者消费
发布订阅: 生产者绑定交换机 交换机将消息发到队列 多个消费者的消息是一样的
路由模式: 生产者发送消息(带routingkey)到交换机交换机根据routingkey发送到相应队列
通配符模式:routingkey 增加通配符 #/*
常见问题
Execution of Rabbit message listener failed.
去RabbitMQ客户端上的队列消息先删除,那些队列消息还是原先的错误的消息。
Caused by: java.io.EOFException: null
at java.io.DataInputStream.readUnsignedByte
检查端口号/http端口号
java.net.SocketException: Socket Closed oSounhMonMethod
用户角色缺少权限 查看角色是否有足够权限。
rabbitmq SocketException: Socket Closed
确认用户是否有权限 不要用guest用户