环境搭建
docker
环境下的安装:
输入命令:docker run -di --name=tensquare_rabbitmq -p 5671:5617 -p 5672:5672 -p 4369:4369 -p 15671:15671 -p 15672:15672 -p 25672:25672 rabbitmq:management
- 浏览器访问:
http://192.168.200.157:15672/#/(ip看自己的虚拟机的ip)
- 输入用户名和密码,都为
guest
- 创建消息队列
准备工作
依赖添加
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</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>
<!--springBoot核心-->
<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>
<!--springBoot消息队列依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<repositories>
<!--指定仓库下载地址-->
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!--指定插件下载地址-->
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
配置文件application
注意层次和空格
server:
port: 9008
spring:
application:
#不推荐写下划线
name: mq-demo
rabbitmq:
host: 192.168.200.157
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitApplication.class,args);
}
}
核心代码
直接模式接收消息
- 创建消息队列
“aaa”
- 接收消息
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@RabbitListener(queues = "aaa")//监听队列aaa
@Component
public class Cust1 {
@RabbitHandler//消息处理
public void getMessage(String msg){
//先进先出的原则
System.out.println("直接模式接收到消息是"+msg);
}
}
- 生产消息
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)//springBoot单元测试固定格式
@SpringBootTest(classes = RabbitApplication.class)//springBoot测试需要指定启动类
public class productor {
@Autowired
private RabbitTemplate rabbitTemplate;//注入消息生产类
@Test
public void sendMsg(){
rabbitTemplate.convertAndSend("aaa","我的消息1");//对列,消息
}
}
分裂模式接收消息
- 创建消息队列
q1,q2
- 创建交换机
nanjing
- 依次将消息队列添加到交换机
nanjing
中
- 接收消息
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@RabbitListener(queues = "q1")
@Component
public class Cust2 {
@RabbitHandler
public void cus(String msg){
System.out.println("分裂模式q1接收消息"+msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@RabbitListener(queues = "q2")
@Component
public class Cut3 {
@RabbitHandler
public void cus(String msg){
System.out.println("分裂模式q2接收消息"+msg);
}
}
- 创建消息
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)//固定格式
@SpringBootTest(classes = RabbitApplication.class)//springBoot测试需要指定启动类
public class productor {
@Autowired
private RabbitTemplate rabbitTemplate;//注入消息生产类
@Test
public void sendMsg2(){
rabbitTemplate.convertAndSend("nanjing","","分裂模式消息哈哈");
}
}
运行结果:
主题形式接收消息
- 创建消息队列:
“qq01”,"qq02","qq03"
- 创建交换机
yuhuatai,type
类型选择topic
- 依次将消息队列添加到交换机
yuhuatai
中,并添加主题类型
主题类型可以自定义,#
为通配符 - 接收消息
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@RabbitListener(queues = "qq01")
@Component
public class Cut4 {
@RabbitHandler
public void cus(String msg){
System.out.println("分裂模式qq01接收消息"+msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@RabbitListener(queues = "qq02")
@Component
public class Cut5 {
@RabbitHandler
public void cus(String msg){
System.out.println("分裂模式qq02接收消息"+msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@RabbitListener(queues = "qq03")
@Component
public class Cut6 {
@RabbitHandler
public void cus(String msg){
System.out.println("分裂模式qq03接收消息"+msg);
}
}
- 创建消息
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)//固定格式
@SpringBootTest(classes = RabbitApplication.class)//springBoot测试需要指定启动类
public class productor {
@Autowired
private RabbitTemplate rabbitTemplate;//注入消息生产类
@Test
public void sendMsg3(){
rabbitTemplate.convertAndSend("yuhuatai","happy.aa","主题模式消息哈哈");//交换机类型,主题类型,消息
}
}
运行结果:
总结
消息中间件RabbitMQ
主要解决应用耦合,异步消息,流量 削锋等问题,实现高性能,高可用,可伸缩和终一致性,通俗的说,如果没有这个中间件,当一个用户发送信息时,另一个用户不在线,即便下次上线也无法接收到消息,使用这个便可以实现异步接收消息。
消息中间件RabbitMQ
主要分为直接模式接收消息,分裂模式接收消息,和主题形式接收消息。
直接模式接收消息:只需要消息队列,不需要创建交换机,最为简单
分裂模式接收消息:需要创建交换机
主题形式接收消息:在分裂模式的基础上需要添加主题类型