核心代码
- 配置文件
spring:
application:
name: rabbitmq-provider
#??rabbitMq 配置
rabbitmq:
host: 43.143.217.124
port: 5672
username: guest
password: guest
#??host ?????,??server??host
# virtual-host: JCcccHost
- pom
<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>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<version>6.0.0</version>
</dependency>
<!-- rabbit mq-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.integration</groupId>-->
<!-- <artifactId>spring-integration-amqp</artifactId>-->
<!-- <version>6.0.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
其中核心包是
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
- java配置
package com.it.test_mqtt.component.rabbitmq;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* 每增加一个队列,需要将其绑定到交换机(如果没有该交换机则需要新建交换机),并增加一个 路由key
*/
@Slf4j
@Configuration
@Data
public class RabbitMQConfig2 {
/**
* 普通交换机
*/
private String normalExchange = "normalExchange";
/**
* 普通队列
*/
private String normalQueue = "normalQueue";
/**
* 普通队列路由 key
*/
private String normalQueueRoutingKey = "normalQueueRoutingKey";
/**
* 消息的过期时间,不配置的话,则永不过期
*/
private Integer ttl = 1000 * 15;
/**
* 死信交换机
*/
private String deadExchange = "deadExchange";
/**
* 死信队列
*/
private String deadQueue = "deadQueue";
/**
* 死信路由
*/
private String deadQueueRoutingKey = "deadQueueRoutingKey";
/**
* 普通交换机
*
* @return
*/
@Bean
public Exchange normalExchange() {
return new TopicExchange(normalExchange, true, false);
}
/**
* 配置普通队列
*
* @return
*/
@Bean
public Queue normalQueue() {
Map<String, Object> args = new HashMap<>();
// 消息存活时间
args.put("x-message-ttl", ttl);
// 死信交换机
args.put("x-dead-letter-exchange", deadExchange);
// 死信队列
args.put("x-dead-letter-routing-key", deadQueueRoutingKey);
return new Queue(normalQueue, true, false, false, args);
}
/**
* 死信交换机
*
* @return
*/
@Bean
public Exchange deadExchange() {
return new TopicExchange(deadExchange, true, false);
}
/**
* 声明一个死信队列
*
* @return
*/
@Bean
public Queue deadQueue() {
return new Queue(deadQueue, true, false, false);
}
/**
* 绑定普通队列 到交换机
* @return
*/
@Bean
public Binding normalQueueBining() {
return new Binding(normalQueue, Binding.DestinationType.QUEUE, normalExchange, normalQueueRoutingKey, null);
}
/**
* 死信队列绑定关系建立
* @return
*/
@Bean
public Binding deadQueueBinding(){
return new Binding(deadQueue, Binding.DestinationType.QUEUE, deadExchange, deadQueueRoutingKey, null);
}
}
- 使用
// 需要提前注入 rabbitTemplate,这是springboot自带的 rabbitmq模板
rabbitTemplate.convertAndSend(
rabbitMQConfig.getEventExchange(),
rabbitMQConfig.getCouponReleaseDelayRoutingKey(),
couponRecordMessage
);