RabbitMQ使用--Topics通配符模式

本文详细解析了如何使用Spring Cloud和RabbitMQ在Java中构建一个消息队列系统,包括创建Exchange、Queue、绑定以及消费者消费过程。通过实例展示了如何利用topic模式进行路由,适合理解和实践队列和路由策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
在这里插入图片描述
图解:

红色Queue:绑定的是usa.# ,因此凡是以 usa.开头的routing key 都会被匹配到
黄色Queue:绑定的是#.news ,因此凡是以 .news结尾的 routing key 都会被匹配
代码:
producer

public class Producer {
    public static void main(String[] args) throws Exception{
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("139.196.37.163");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("my_vhost");
        //创建连接
        Connection connection = connectionFactory.newConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //创建交换机
        /**
         * String exchange,
         * BuiltinExchangeType type,
         * boolean durable,
         * boolean autoDelete,
         * boolean internal,
         * Map<String, Object> arguments
         */
        String exchangeName = "test_topic";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true,false,false,null);
        //创建队列
        String queue1Name = "test_topic_queue1";
        String queue2Name = "test_topic_queue2";
        channel.queueDeclare(queue1Name,true,false,false,null);
        channel.queueDeclare(queue2Name,true,false,false,null);
        //绑定队列跟交换机
        channel.queueBind(queue1Name,exchangeName,"#.error");
        channel.queueBind(queue1Name,exchangeName,"order.*");
        channel.queueBind(queue2Name,exchangeName,"*.*");
        channel.basicPublish(exchangeName,"order.info",null,"body".getBytes());
        //9. 释放资源
        channel.close();
        connection.close();
    }
}

Consumer代码:

public class Consumer1 {
    public static void main(String[] args) throws Exception{

    ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("139.196.37.163");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("my_vhost");
    //创建连接
    Connection connection = connectionFactory.newConnection();
    //创建频道
    Channel channel = connection.createChannel();
    //创建队列,如果有就不会创建
    /**
     * String queue,
     * boolean durable,
     * boolean exclusive,
     * boolean autoDelete,
     * Map<String, Object> arguments
     */
        channel.queueDeclare("test_topic_queue1", true, false, false, null);
    /**
     * String queue,
     * boolean autoAck,
     * Map<String, Object> arguments,
     * Consumer callback
     */
    DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

            System.out.println(new String(body));
        }
    };
        channel.basicConsume("test_topic_queue1", true, defaultConsumer);

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值