首先用rabbitmq就是得有一个rabbitmq一般情况下就在虚拟机中安装,其中安装方式有很多种
第一种 下载tar包利用 tar解压在 gcc用make编译
第二种利用yum来安装(这是黑马的借用一下,大家可以从网上找有很多的安装教程)
# root执行
# 1. 准备gpgkey密钥
rpm --import https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
rpm --import https://packagecloud.io/rabbitmq/erlang/gpgkey
rpm --import https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
# 2. 准备仓库文件
vim /etc/yum.repos.d/rabbitmq.repo
# 填入如下内容
##
## Zero dependency Erlang
##
[rabbitmq_erlang]
name=rabbitmq_erlang
baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/$basearch
repo_gpgcheck=1
gpgcheck=1
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_erlang-source]
name=rabbitmq_erlang-source
baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
##
## RabbitMQ server
##
[rabbitmq_server]
name=rabbitmq_server
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_server-source]
name=rabbitmq_server-source
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
# root执行
yum install erlang rabbitmq-server -y
# root执行
# 使用systemctl管控,服务名:rabbitmq-server
systemctl enable rabbitmq-server # 开机自启
systemctl disable rabbitmq-server # 关闭开机自启
systemctl start rabbitmq-server # 启动
systemctl stop rabbitmq-server # 关闭
systemctl status rabbitmq-server # 查看状态
# 方式1(推荐),关闭防火墙
systemctl stop firewalld # 关闭
systemctl disable firewalld # 关闭开机自启
# 方式2,放行5672 25672端口
firewall-cmd --add-port=5672/tcp --permanent # 放行tcp规则下的5672端口,永久生效
firewall-cmd --add-port=15672/tcp --permanent # 放行tcp规则下的15672端口,永久生效
firewall-cmd --add-port=25672/tcp --permanent # 放行tcp规则下的25672端口,永久生效
firewall-cmd --reload # 重新加载防火墙规则
rabbitmq-plugins enable rabbitmq_management
rabbitmqctl add_user admin 'Itheima66^'
rabbitmqctl set_permissions -p "/" "admin" ".*" ".*" ".*"
rabbitmqctl set_user_tags admin administrator
第三种利用docker镜像来使用
打开图形化界面需要用到 rabbitmq-plugins enable rabbitmq_management来打开图形化界面默认为账号guest 密码guest 但是这个只能在本机用所以你要创建用户并设置权限
其中第二个是延时队列的插件
在创建交换机时可以选择有x-delayed-message则代表安装成功
然后就是到了springboot整合rabbitmq
导入依赖版本和你导入的<spring-boot-stater-parent>的版本一致,也可以自己改版本利用<version></version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
然后就到运用了
大差不差先创建一个你想要类型的配置类 这里用延时队列举例
四步走
第一步声明你的交换机、队列、routink的名字 第二步创建一个queue的bean对象 第三步创建一个交换机exchange的bean对象 第四步创建bing把队列与交换机绑定上
package com.wyl.helper;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
//告诉是一个配置类bean
@Configuration
public class DelayedMqConfig {
//声明延时交换机
public static final String ORDER_TIMEING_EXCHAGE_NAME="ordertimeing.exchange";
//声明队列
public static final String ORDER_TIMEING_QUEUE_NAME="ordertimeing.queue";
//声明ROUTING
public static final String ORDER_TIMEING_ROUTING_NAME="ordertimeing.routingkey";
/**
* 声明一个自定义延时队列 采用插件
* @return
*/
@Bean
public Queue delayedQueue(){
return QueueBuilder.durable(ORDER_TIMEING_QUEUE_NAME).build();
}
/**
* 声明交换机 采用插件
* @return
*/
@Bean
CustomExchange delayedExchange(){
Map<String,Object> arguments=new HashMap<>();
arguments.put("x-delayed-type","direct");
return new CustomExchange(ORDER_TIMEING_EXCHAGE_NAME,"x-delayed-message",false,false,arguments);
}
/**
* 将交换机与队列绑定
* @param queue
* @param exchange
* @return
*/
@Bean
public Binding delayedBing(@Qualifier ("delayedQueue") Queue queue,CustomExchange exchange){
//生成器模式
return BindingBuilder.bind(queue).to(exchange).with(ORDER_TIMEING_ROUTING_NAME).noargs();
}
}
找到你想运用的类 我这里是准备在你添加订单时在多长时间被接单就自动取消
package com.wyl.controller;
import com.wyl.result.Result;
import com.wyl.service.OrdersService;
import com.wyl.vo.system.OrdersVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@Api(tags = "订单信息管理")
@RestController
@RequestMapping("/wechat/orders")
public class OrdersController {
@Autowired
private OrdersService ordersService;
//自动装配这个bean就可以进行发消息了
@Autowired
private RabbitTemplate rabbitTemplate;
@PostMapping("/addOrders/{appid}/{ordertype}")
public Result addOrders(@PathVariable int appid, @PathVariable int ordertype, @RequestBody Map<String,String> map ){
//一般情况下不在controller层上写逻辑,测试
String message="这是延时队列";
Integer delayTime= ; //延时时间
//convertAndSend这是一个重载的方法不同参数用的不一样
// 当前这个第一参数是交换机的名字 第二个参数是绑定的routink名字,想要穿的消息,并设置延时多长时间
rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME
,DelayedQueueConfig.DELAYED_ROUTING_NAME,message,
message1 -> {
message1.getMessageProperties().setDelay(delayTime);
return message1;
});
return ordersService.addOrders(appid,ordertype,map);
}
}
最后就是让我们的消费者来监听你想监听的队列 当前是你延时多长时间后他就会被消费者监听到然后进行判断当前传过来的订单id的状态是否被接单,如果被接单就什么不干,如果没有就删除订单
package com.wyl.mqConsumer;
import com.wyl.helper.DelayedMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@Slf4j
public class OrderTimeingConsumer {
//可以注入你想要操作的实体对应的service等等进来
@RabbitListener(queues = DelayedMqConfig.ORDER_TIMEING_EXCHAGE_NAME)
public void reciveDelayQueue(Message message){
//写你想要的逻辑
String msg = new String(message.getBody());
log.info("当前时间:{},收到延时队列的消息:{}",new Date().toString(),msg);
}
}