RabbitMq

信息收集关闭禁用:

 

#进入容器后,cd到以下路径
cd /etc/rabbitmq/conf.d/

#修改 management_agent.disable_metrics_collector = false
echo management_agent.disable_metrics_collector = false > management_agent.disable_metrics_collector.conf

 

插件地址: https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases

放到rabbitmq插件目录

开启插件: rabbitmq-plugins enable rabbitmq_delayed_message_exchange   //不带版本号

 

package com.finnlee.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;

/**
 * @author finnlee
 * @date 2022/8/25 22:46
 */
@Configuration
public class RabbitConfig {
    //普通交换机
    public static final String EXCHANGE_NAME1 = "exA";

    //死信交换机
    public static final String EXCHANGE_NAME2 = "exB";

    //普通队列
    public static final String QUEUE_NAME1 = "queueA";

    //普通队列
    public static final String QUEUE_NAME2 = "queueB";

    //死信队列
    public static final String QUEUE_NAME3 = "queueC";

    //延时队列
    public static final String QUEUE_NAME4 = "queueD";

    //定义交换机
    public static final String EXCHANGE_NAME3 = "exC";

    //队列名称
    public static final String QUEUE_NAME5 = "queueE";

    //定义一个普通交换机
    @Bean("exA")
    public Exchange exA(){
        return new DirectExchange(EXCHANGE_NAME1);
    }

    //定义一个死信交换机
    @Bean("exB")
    public Exchange exB(){
        return new DirectExchange(EXCHANGE_NAME2);
    }

    //定义交换机
    @Bean("exC")
    public CustomExchange exC(){
        Map<String ,Object> argument = new HashMap<>();
        argument.put("x-delayed-type","direct");
        return new CustomExchange(EXCHANGE_NAME3,"x-delayed-message",true,false,argument);
    }
    //普通队列
    @Bean("queueA")
    public Queue queueA(){
        Map<String,Object> argument = new HashMap<>();
        argument.put("x-dead-letter-exchange",EXCHANGE_NAME2);
        argument.put("x-dead-letter-routing-key","XC");
        argument.put("x-message-ttl",10000);
        return QueueBuilder.durable(QUEUE_NAME1).withArguments(argument).build();
    }
    //普通队列
    @Bean("queueB")
    public Queue queueB(){
        Map<String,Object> argument = new HashMap<>();
        argument.put("x-dead-letter-exchange",EXCHANGE_NAME2);
        argument.put("x-dead-letter-routing-key","XC");
        argument.put("x-message-ttl",40000);
        return QueueBuilder.durable(QUEUE_NAME2).withArguments(argument).build();
    }

    //延时队列
    @Bean("queueD")
    public Queue queueD(){
        Map<String,Object> argument = new HashMap<>();
        argument.put("x-dead-letter-exchange",EXCHANGE_NAME2);
        argument.put("x-dead-letter-routing-key","XC");
        return QueueBuilder.durable(QUEUE_NAME4).withArguments(argument).build();
    }


    //定义队列
    @Bean("queueE")
    public Queue queueE(){
        return QueueBuilder.durable(QUEUE_NAME5).build();//QueueBuilder.durable(QUEUE_NAME1).build();
    }

    //绑定
    @Bean
    public Binding queueaBindExa(@Qualifier("queueA") Queue queueA,
                                 @Qualifier("exA") DirectExchange exA){
        return BindingBuilder.bind(queueA).to(exA).with("XA");
    }
    //绑定
    @Bean
    public Binding queuecBindExa(@Qualifier("queueB") Queue queueB,
                                 @Qualifier("exA") DirectExchange exA){
        return BindingBuilder.bind(queueB).to(exA).with("XB");
    }

    //绑定
    @Bean
    public Binding queuedBindExa(@Qualifier("queueD") Queue queueD,
                                 @Qualifier("exA") DirectExchange exA){
        return BindingBuilder.bind(queueD).to(exA).with("XD");
    }
    //死信队列
    @Bean("queueC")
    public Queue queueC(){
        return new Queue(QUEUE_NAME3);
    }
    //绑定
    @Bean
    public Binding queuecBindExb(@Qualifier("queueC") Queue queueC,
                                 @Qualifier("exB") DirectExchange exB){
        return BindingBuilder.bind(queueC).to(exB).with("XC");
    }

    //绑定
    @Bean
    public Binding queueeBindExc(@Qualifier("queueE") Queue queueE,
                                     @Qualifier("exC") CustomExchange exC) {
        return BindingBuilder.bind(queueE).to(exC).with("LD").noargs();

    }

}

  

消费端

@RabbitListener(queues = "queueE")
    public void reviceMsgD(Message message, Channel channel) throws UnsupportedEncodingException {
        String msg = new String(message.getBody(),"UTF-8");
        log.info("插件队列接收时间:{} ,接收到{} 秒的信息",new Date().toString(),msg);
    }

  

生产端

@GetMapping("/sendMsg/delayez/{ttl}")
    public void sendMsgDelayez(@PathVariable("ttl") Integer ttl){
        log.info("当前时间:{} ,发送一条{} 秒的消息",new Date().toString(),ttl);
        rabbitTemplate.convertAndSend("exC","LD"," 消息来自 delayez为 "+ttl+" 的队列 ", message ->{
            message.getMessageProperties().setDelay(ttl);
            return message;
        });
    }

 

posted @ 2022-08-27 15:31  FinnYY  阅读(73)  评论(0编辑  收藏  举报