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 -> {});



    }

}
相关推荐
龙哥·三年风水4 小时前
workman服务端开发模式-应用开发-vue-element-admin挂载websocket
分布式·websocket·vue
明达技术4 小时前
分布式 I/O 配合高冗余 PLC,打造高效控制新典范
运维·分布式
技术路上的苦行僧4 小时前
分布式专题(9)之Mysql高可用方案
分布式·mysql·mgr·mha·mysql高可用·mmm
Lin_Miao_095 小时前
Kafka优势
分布式·kafka
小陈far6 小时前
RabbitMQ
分布式·rabbitmq
zhangpfly8 小时前
OpenEuler22.04配置zookeeper+kafka三节点集群
分布式·zookeeper·kafka
员宇宙9 小时前
【RabbitMQ的死信队列】
分布式·rabbitmq·ruby
winks310 小时前
使用RabbitMQ
java·rabbitmq·java-rabbitmq
Allen Bright10 小时前
RabbitMQ中的批量Confirm模式:提升消息可靠性与性能
分布式·rabbitmq
bennybi11 小时前
基于Docker的ETCD分布式集群
分布式·docker·etcd