前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot整合rabbitMQ 死信队列(基本)

springboot整合rabbitMQ 死信队列(基本)

作者头像
一个风轻云淡
发布2022-11-13 11:18:39
5380
发布2022-11-13 11:18:39
举报
文章被收录于专栏:java学习java

创建两个队列QA和QB,两者队列TTL分别设置为10S和40S,然后在创建一个交换机X和死信交换机Y,它们的类型都是direct,创建一个死信队列QD,它们的绑定关系如下

 pom.xml文件

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo6</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo6</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--RabbitMQ依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok
            </groupId>
            <artifactId>lombok</artifactId>
        </dependency>  <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>  <!--RabbitMQ测试依赖-->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations-java5</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

配置文件 

代码语言:javascript
复制
spring.rabbitmq.host=192.168.231.135
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123

配置类 

代码语言:javascript
复制
@Configuration
public class TtlQueueConfig {
    public  static  final  String X_EXCHANGE="X";
    public  static  final  String QUEUE_A="QA";
    public  static  final  String QUEUE_B="QB";
    public  static  final  String Y_DEAD_LETTER_EXCHANGE="Y";
    public static final String DEAD_LETTER_QUEUE="QD";
    //声明XExchange
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }
    //声明YExchange
    @Bean("YExchange")
    public DirectExchange YExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }
    //声明队列A TTL为10s,并绑定到对应的死信交换机
    @Bean("queueA")
    public Queue queueA(){
        Map<String,Object> args=new HashMap<String, Object>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的TTL
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();
    }
    // 声明队列A绑定X交换机
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queue,
                                  @Qualifier("xExchange")DirectExchange exchange)
    {
        return BindingBuilder.bind(queue).to(xExchange()).with("XA");
    }
    //声明队列B ttl为40s,并绑定到对应的死信交换机
    @Bean("queueB")
    public Queue queueB()
    {
        Map<String,Object> args=new HashMap<String, Object>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的TTL
        args.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();
    }
    //声明队列B绑定X交换机
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }
    //声明死信队列QD
    @Bean("queueD")
    public Queue queueD()
    {
        return new Queue(DEAD_LETTER_QUEUE);
    }
    //声明死信队列QD绑定关系
    @Bean
    public Binding deadLetterBindingQAD(@Qualifier("queueD") Queue queue,
                                        @Qualifier("YExchange") DirectExchange exchange)
    {
        return BindingBuilder.bind(queue).to(exchange).with("YD");
    }
}

生产者 

代码语言:javascript
复制
@Slf4j
@RequestMapping("ttl")
@RestController
public class Produce01 {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("sendMsg/{message}")
    public void sendMsg(@PathVariable String message)
    {

        log.info("当前时间:{},发送一条消息给俩个TTL队列:{}", new Date(),message);
        rabbitTemplate.convertAndSend("X","XA","消息来于10s"+message);
        rabbitTemplate.convertAndSend("X","XB","消息来于40s"+message);
    }
}

监听 

代码语言:javascript
复制
@Slf4j
@Component
public class Consumer {
    @RabbitListener(queues = "QD")
    public void receiveD(Message message, Channel channel)
    {
        System.out.println("*****************************");
        String s = new String(message.getBody());
        log.info("当前时间{},死信队列接收到消息---->{}",new Date(),s);
        System.out.println("*****************************");
    }
}

swagg类 (这个玩意我不知道拿来干啥的)

代码语言:javascript
复制
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean  public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)  .
                groupName("webApi")  .apiInfo(webApiInfo())
                .select()
            .build();
    }
    private ApiInfo webApiInfo(){   return new ApiInfoBuilder()  .title("rabbitmq接口文档")  .description("本文档描述了rabbitmq微服务接口定义")  .version("1.0")  .
            contact(new Contact
            ("enjoy6288", "http://atguigu.com",
            "1551388580@qq.com"))  .build();  } }

浏览器输入 

 结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档