RabbitMQ
为什么需要用它?
1.异步通讯(节省时间,异步去执行任务)
2.应用解耦(避免因为一个系统故障导致另外一个请求完成不了)
3.流量削峰(先将请求储存起来,等需要的时候再使用)
4.日志处理
5.消息通讯
加订单 减库存
50ms 50ms
继承thread 实现runable接口 callable会有返回值
请购的瞬时的流量是非常大的 1w/s 10w/s
队列 fifo first in first out
相当于使用一个消息队列 把用户抢购请求存在消息队列 等待抢购结束 通过队列先后方式( fifo first in first out)判定有没有库存没库存就抢购失败
理论什么的直接去官方网站查看
https://note.oddfar.com/pages/e38dcb/#%E4%BB%80%E4%B9%88%E6%98%AF%E4%B8%AD%E9%97%B4%E4%BB%B6
直接使用docker-commpose
docker-commpose.yaml里面添加就行
需要在docker放这个文件
「public」https://www.aliyundrive.com/s/pZsqyEneghB 提取码: 3lg0
报错的话输入这些代码
1. docker network rm traefik
2. docker network create --driver overlay --attachable traefik
3.docker-compose down
4.docker-compose up -d
开5个端口
服务器地址加15672就可以访问客户端
- "15672:15672"
- "4369:4369"
- "5672:5672"
- "25672:25672"
rabbitmq:
hostname: rabbitmq
environment:
RABBITMQ_DEFAULT_VHOST: "root"
RABBITMQ_DEFAULT_USER: "root"
RABBITMQ_DEFAULT_PASS: "123456"
image: "rabbitmq:3.9.14-management"
restart: always
volumes:
- "/usr/local/bank/rabbitmq/data:/var/lib/rabbitmq"
- "/usr/local/bank/rabbitmq/log:/var/lib/rabbitmq/log"
ports:
- "15672:15672"
- "4369:4369"
- "5672:5672"
- "25672:25672"
helloworld模式
点对点
发布订阅模式 一对多
分裂模式(广播模式)
主题模式
如果是新手的话先去官网
https://note.oddfar.com/pages/e38dcb/#%E4%BB%80%E4%B9%88%E6%98%AF%E4%B8%AD%E9%97%B4%E4%BB%B6
安装直接跳过就行了从简单案例开始官方文档写的相当好了
直接开启springboot实战
pom.xml
<dependencies>
<dependency>
<groupId>com.changan</groupId>
<artifactId>common-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--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>
</dependencies>
yaml
spring:
application:
name: rabbitmq-service
#主机 用户名 密码
rabbitmq:
host: 119.23.176.114
username: root
password: 123456
virtual-host: root
server:
port: 8084
先新建一个rabbitmq-service服务
新建类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VqatsvWK-1656472903530)(C:\Users\shenjian\AppData\Roaming\Typora\typora-user-images\image-20220629091705045.png)]
RabbitMQConfig
package com.changan.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @program: t139springcloudalibaba
* @description: 交换机配置类
* @author: Mr.shen
* @create: 2022-06-24 15:03
**/
@Configuration
public class RabbitMQConfig {
/**
* 生成一个交换机
*
* @return
*/
@Bean("bootExchange")
public DirectExchange directExchange() {
return new DirectExchange("bootExchange");
}
/***
* 生成一个队列
* @return
*/
@Bean("bootQueue")
public Queue queue() {
return new Queue("bootQueue");
}
/***
* 将队列和交换机绑定
* @param queue
* @param exchange
* @return
*/
//声明队列 B 绑定 X 交换机
@Bean
public Binding queuebBindingEX(
@Qualifier("bootExchange") DirectExchange exchange, @Qualifier("bootQueue") Queue queue) {
return BindingBuilder.bind(queue).to(exchange).with("boot");
}
}
Messageconsumer
package com.changan.consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import com.rabbitmq.client.Channel;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @program: t139springcloudalibaba
* @description:
* @author: Mr.shen
* @create: 2022-06-24 15:15
**/
@RestController
@Slf4j
@Component
public class Messageconsumer {
@RabbitListener(queues = "bootQueue")
public void getMsg(Message message, Channel channel) {
System.out.println("从队列中获取到的消息是:" + new String(message.getBody()));
log.info("当前时间:{},死信队列信息{}",new Date().toString(),message);
}
}
MessageProducer
package com.changan.product;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: t139springcloudalibaba
* @description:
* @author: Mr.shen
* @create: 2022-06-24 14:44
**/
@RestController
public class MessageProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("/sendMsg")
public void sendMsg() {
//给队列发组发送信息
rabbitTemplate.convertAndSend("bootExchange", "boot", "hello rabbitMq");
System.out.println("发送成功!");
}
}