rabbitmq-topic 多对多模糊匹配分发消息

路由键key通过模糊匹配*.*

生产者3个:


import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author clc
 * @create 2019-04-24 14:59
 */
@Component
public class Sender {

   /* @Resource
    private RabbitTemplate rabbitTemplate;*/
    @Resource
    private AmqpTemplate amqpTemplate;

    //交换器名称
    @Value("${mq.config.exchange}")
    private String exchange;

    /**
     * 发送消息的方法
     */
    public void send(String msg){
        /**
         *向消息队列发送消息
         *参数一:队列的名称
         *参数二:消息
         */
       //amqpTemplate.convertAndSend("hello-queue",msg);
        /**
         *向消息队列发送消息
         *参数一:交换器名称
         *参数二:路由键
         *参数三:消息
         */
       amqpTemplate.convertAndSend(exchange,"user.log.info","User.info:..."+msg);
       amqpTemplate.convertAndSend(exchange,"user.log.debug","User.debug:..."+msg);
       amqpTemplate.convertAndSend(exchange,"user.log.error","User.error:..."+msg);
       amqpTemplate.convertAndSend(exchange,"user.log.warn","User.warn:..."+msg);
    }
}



import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author clc
 * @create 2019-04-24 14:59
 */
@Component
public class Sender1 {

   /* @Resource
    private RabbitTemplate rabbitTemplate;*/
    @Resource
    private AmqpTemplate amqpTemplate;

    //交换器名称
    @Value("${mq.config.exchange}")
    private String exchange;

    /**
     * 发送消息的方法
     */
    public void send(String msg){
        /**
         *向消息队列发送消息
         *参数一:队列的名称
         *参数二:消息
         */
       //amqpTemplate.convertAndSend("hello-queue",msg);
        /**
         *向消息队列发送消息
         *参数一:交换器名称
         *参数二:路由键
         *参数三:消息
         */
       amqpTemplate.convertAndSend(exchange,"pro.log.info","pro.info:..."+msg);
       amqpTemplate.convertAndSend(exchange,"pro.log.debug","pro.debug:..."+msg);
       amqpTemplate.convertAndSend(exchange,"pro.log.error","pro.error:..."+msg);
       amqpTemplate.convertAndSend(exchange,"pro.log.warn","pro.warn:..."+msg);
    }
}


import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author clc
 * @create 2019-04-24 14:59
 */
@Component
public class Sender3 {

   /* @Resource
    private RabbitTemplate rabbitTemplate;*/
    @Resource
    private AmqpTemplate amqpTemplate;

    //交换器名称
    @Value("${mq.config.exchange}")
    private String exchange;

    /**
     * 发送消息的方法
     */
    public void send(String msg){
        /**
         *向消息队列发送消息
         *参数一:队列的名称
         *参数二:消息
         */
       //amqpTemplate.convertAndSend("hello-queue",msg);
        /**
         *向消息队列发送消息
         *参数一:交换器名称
         *参数二:路由键
         *参数三:消息
         */
       amqpTemplate.convertAndSend(exchange,"odr.log.info","odr.info:..."+msg);
       amqpTemplate.convertAndSend(exchange,"odr.log.debug","odr.debug:..."+msg);
       amqpTemplate.convertAndSend(exchange,"odr.log.error","odr.error:..."+msg);
       amqpTemplate.convertAndSend(exchange,"odr.log.warn","odr.warn:..."+msg);
    }

 配置文件

spring.application.name=rabbit_mq
spring.rabbitmq.host=192.168.244.138
spring.rabbitmq.port=5672
spring.rabbitmq.username=clc
spring.rabbitmq.password=123456
server.port=8090


#设置交换器的名称
mq.config.exchange=log.topic

消费者3个:


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息的接收者
 * @author clc
 * @create 2019-04-24 15:05
 */
//bindings:绑定队列
// Queue : value  配置队列名称
// Queue : autoDelete 可删除的临时队列
//Exchange : value 交换器名称
//Exchange : type 指定具体的交换器器类型
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.info}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.TOPIC),
                key="*.log.info"
        )
)
public class Accepter {
    /**接收消息的方法
     * 注解的监听队列名称,可以是数组
     */
   /* @RabbitListener(queues={"hello-queue"})
    public void process(String msg){
        System.out.println("这是接收到的消:{}"+msg);
    }*/

    @RabbitHandler
    public void process(String msg){
        System.out.println("这是接收到的消息info:{}"+msg);
    }
}


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息的接收者
 * @author clc
 * @create 2019-04-24 15:05
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.error}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.TOPIC),
                key="*.log.error"
        )
)
public class ErrorAccepter {
    /**接收消息的方法
     * 注解的监听队列名称,可以是数组
     */
   /* @RabbitListener(queues={"hello-queue"})
    public void process(String msg){
        System.out.println("这是接收到的消:{}"+msg);
    }*/

    @RabbitHandler
    public void process(String msg){
        System.out.println("这是接收到的消息Error:{}"+msg);
    }
}


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息的接收者
 * @author clc
 * @create 2019-04-24 15:05
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.all}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.TOPIC),
                key="*.log.*"
        )
)
public class AllAccepter {
    /**接收消息的方法
     * 注解的监听队列名称,可以是数组
     */
   /* @RabbitListener(queues={"hello-queue"})
    public void process(String msg){
        System.out.println("这是接收到的消:{}"+msg);
    }*/

    @RabbitHandler
    public void process(String msg){
        System.out.println("这是接收到的消息all:{}"+msg);
    }
}

配置文件:

spring.application.name=rabbit_mq
spring.rabbitmq.host=192.168.244.138
spring.rabbitmq.port=5672
spring.rabbitmq.username=clc
spring.rabbitmq.password=123456
server.port=8088

#设置交换器的名称
mq.config.exchange=log.topic

#info级别的队列名称
mq.config.queue.info=log.info

#error级别的队列名称
mq.config.queue.error=log.error

#log级别的队列名称
mq.config.queue.all=log.all

 

你可能感兴趣的:(rabbitmq-topic 多对多模糊匹配分发消息)