使用idea创建一个普通Maven工程,也可以创建也给SpringBoot工程,两者都可以。
最终架构图如下, 其中有个一swaggerconfig 配置类,web接口文档工具,可以不用管。
采用springboot2.2.4 版本,JDK1.8版本。多添加了一个web模块依赖,可以方便后面通过页面进行发送消息
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.4.RELEASEversion>
<relativePath/>
parent>
<groupId>com.rabbitmqDemo-bootgroupId>
<artifactId>rabbitmqDemo-bootartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>rabbitmqDemo-bootname>
<description>rabbitmqDemo-bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.amqpgroupId>
<artifactId>spring-rabbit-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
rabbitmq web页面端口为15672,但是代码配置的时候是5672
username 和password 在之前的教程中已经给出配置方式,可查阅我之前的文章。
spring:
rabbitmq:
host: localhost
username: admin
password: qwerasdf
port: 5672
主启动类,和普通springboot启动类一样,需要加载包的最上层,如果不熟悉springboot的同学,可以先补充相关知识。
package com.rabbitmqdemoboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitmqDemoBootApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqDemoBootApplication.class, args);
}
}
package com.rabbitmqdemoboot.config;
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;
/**
* rabbitmq配置文件类
*/
@Configuration
public class RabbitmqConfig {
/**
* 声明普通交换机X
*
* @return 交换机X
*/
@Bean("xExchange")
public DirectExchange xExchange() {
return new DirectExchange(RabbitmqConstant.EXCHANGE_NORMAL_X);
}
/**
* 声明队列 qa
* TTL 10s
*
* @return 交换机qa
*/
@Bean("qaQueue")
public Queue qaQueue() {
Map<String, Object> arguments = new HashMap<>();
//设置死信交换机
arguments.put("x-dead-letter-exchange", RabbitmqConstant.EXCHANGE_DEAD_Y);
//设置死信routingKey
arguments.put("x-dead-letter-routing-key", RabbitmqConstant.ROUTING_KEY_DEAD_YD);
//设置TTL,单位ms
arguments.put("x-message-ttl", 10 * 1000);
return QueueBuilder
.durable(RabbitmqConstant.QUEUE_NORMAL_QA)
.withArguments(arguments)
.build();
}
/**
* 绑定routingKey
* QA Binding X
*/
@Bean
public Binding queueQABindingX(@Qualifier("qaQueue") Queue qaQueue, @Qualifier("xExchange") DirectExchange xExchange) {
return BindingBuilder.bind(qaQueue).to(xExchange).with(RabbitmqConstant.ROUTING_KEY_NORMAL_XA);
}
}
发送消息采用 RabbitTemplate 工具进行发送消息
package com.rabbitmqdemoboot.Controller;
import com.rabbitmqdemoboot.config.RabbitmqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* 发送延迟消息
*/
@RestController
@RequestMapping("/ttl")
public class SendMsgController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMsg/{message}")
public String sendMsg(@PathVariable String message) {
rabbitTemplate.convertAndSend(RabbitmqConstant.EXCHANGE_NORMAL_X, RabbitmqConstant.ROUTING_KEY_NORMAL_XA, "10s TTL queue" + message);
return "success";
}
}
接收器采用监听器进行监听
package com.rabbitmqdemoboot.consumer;
import com.rabbitmq.client.Channel;
import com.rabbitmqdemoboot.config.RabbitmqConstant;
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;
@Slf4j
@Component
public class DeadLetterQueueConsumer {
@RabbitListener(queues = RabbitmqConstant.QUEUE_DEAD_QA)
public void receive(Message message, Channel channel) {
String s = new String(message.getBody());
log.info("current time : {} , receive message : {}", new Date(), s);
}
}