RabbitMQ: Routing结构

生产者

java 复制代码
package com.qf.mq2302.routing;

import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class EmitLog {
    public static final String EXCHANGE_NAME="emitlogs";


    public static void main(String[] args) throws Exception {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();

        //创建一个路由模式的交换机,默认创出来,不持久化,不自动删除,不是内部交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        String msg="hello routing!!";

        //准备routingKey
        String routingKey="info";

        //发送消息
        channel.basicPublish(EXCHANGE_NAME,routingKey,null,msg.getBytes("utf-8"));

        channel.close();
        connection.close();



    }








}

消费者1号

java 复制代码
package com.qf.mq2302.routing;

import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

import java.io.IOException;

public class ReceiveError {
    private static final String EXCHANGE_NAME="emitlogs";

    public static void main(String[] args) throws Exception {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        //该消费者创建一个自己独占的队列,绑定到指定交换机接收消息。
        String queueName = channel.queueDeclare().getQueue();

        //准备号要绑定时使用的routingkey
        String routingKey = "error";

        //绑定该队列到交换机
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey);

        //设置预留消息队列,也就是,RabbitMQ发过来,我可以存几个。当确认一个就会又发过来一个,
        // 但是这些相当于线程池里的线程,然后每个线程又去开辟一个新的线程去执行,回调方法,
        // 当回调方法确认完事,才会释放当前这个线程,然后去队列里在消费一个过来。
        channel.basicQos(1);

        //autoAck :false不自动确认,需要手动确认,如果手动不确认,就会按照 channel.basicQos(1);的数量,给多少就消费多少,不会再给你发了。
        channel.basicConsume(queueName, false, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String msg = new String(body, "utf-8");
                System.out.println(msg);

                //手动ACK
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            }
        },consumerTag -> {});



    }

}

消费者2号

java 复制代码
package com.qf.mq2302.routing;

import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

import java.io.IOException;

public class ReceiveIOther {
    private static final String EXCHANGE_NAME="emitlogs";

    public static void main(String[] args) throws Exception {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        //该消费者创建一个自己独占的队列,绑定到指定交换机接收消息。
        String queueName = channel.queueDeclare().getQueue();

        //准备号要绑定时使用的routingkey
        String routingKey1 = "error";
        String routingKey2 = "info";
        String routingKey3 = "warn";

        //绑定该队列到交换机
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey1);
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey2);
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey3);

        channel.basicQos(1);

        channel.basicConsume(queueName, false, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                //获取routingKey
                String routingKey = message.getEnvelope().getRoutingKey();
                String msg = new String(body, "utf-8");
                System.out.println(msg);

                //手动ACK
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            }
        },consumerTag -> {});



    }

}
相关推荐
CodeWithMe28 分钟前
【Note】《Kafka: The Definitive Guide》 第5章:深入 Kafka 内部结构,理解分布式日志系统的核心奥秘
分布式·kafka
CodeWithMe44 分钟前
【Note】《Kafka: The Definitive Guide》第一章:Meet Kafka
分布式·kafka
CodeWithMe1 小时前
【Note】《Kafka: The Definitive Guide》 第二章 Installing Kafka:Kafka 安装与运行
分布式·kafka
CodeWithMe5 小时前
【Note】《Kafka: The Definitive Guide》 第8章: Cross-Cluster Data Mirroring
分布式·kafka
Alaia.6 小时前
【kafka 3.9.1】单机版集群部署并配置kafka的SASL认证
分布式·kafka
勤奋的知更鸟7 小时前
Kettle + 大数据实战:从数据采集到分布式处理的完整流程指南
大数据·分布式
Kookoos8 小时前
ABP VNext + Cosmos DB Change Feed:搭建实时数据变更流服务
数据库·分布式·后端·abp vnext·azure cosmos
掘金-我是哪吒21 小时前
分布式微服务系统架构第156集:JavaPlus技术文档平台日更-Java线程池使用指南
java·分布式·微服务·云原生·架构
亲爱的非洲野猪21 小时前
Kafka消息积压的多维度解决方案:超越简单扩容的完整策略
java·分布式·中间件·kafka