RabbitMq——6、主题交换机Topics

简介

本节主要演示交换机的另一种类型:主题类型topic,直连接类型direct必须是生产者发布消息指定的routingKey和消费者在队列绑定时指定的routingKey完全相等时才能匹配到队列上,与direct不同,topic可以进行模糊匹配,可以使用星号*和井号#这两个通配符来进行模糊匹配,其中星号可以代替一个单词;主题类型的转发器的消息不能随意的设置选择键(routing_key),必须是由点隔开的一系列的标识符组成。标识符可以是任何东西,但是一般都与消息的某些特性相关。一些合法的选择键的例子:“quick.orange.rabbit”,你可以定义任何数量的标识符,上限为255个字节。 #井号可以替代零个或更多的单词,只要能模糊匹配上就能将消息映射到队列中。当一个队列的绑定键为#的时候,这个队列将会无视消息的路由键,接收所有的消息
è¿éåå¾çæè¿°

生产者

package com.suirui.mq.rabbit.topic;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author zongx
 * @Date 2020/5/9 14:23
 * @Version 1.0
 * 路由选择Routing
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(AMQP.PROTOCOL.PORT);
        factory.setUsername("guest");
        factory.setPassword("guest");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String EXCHANGE_NAME = "exchange.topic";
//        String[] routingKeys = {"debug", "info", "warning", "error"};
        String[] routingKeys ={"quick.orange.rabbit", "lazy.orange.elephant", "mq.erlang.rabbit", "lazy.brown.fox", "lazy."};
        //生产者不需要队列声明,也不需要声明交换机,只需要指定交换机的名称便可以。队列和交换机的声明可以放到消费者中

        for (int i = 0; i < 10; i++) {
            int random = (int)(Math.random() * 5);
            String routingKey = routingKeys[random];
           String msg = "hello rabbit. " +routingKey +", "+ i;
           //广播类型不需要routingkey,但是不能写null
            channel.basicPublish(EXCHANGE_NAME,routingKey,null,msg.getBytes("utf-8"));
        }

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

    }
}

消费者

package com.suirui.mq.rabbit.topic;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author zongx
 * @Date 2020/5/9 16:44
 * @Version 1.0
 */
public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(AMQP.PROTOCOL.PORT);
        factory.setUsername("guest");
        factory.setPassword("guest");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String EXCHANGE_NAME = "exchange.topic";
        String QUEUE_NAME = "queue.topic.q1";
        //声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        //绑定队列
        String[] routingKeys = {"*.orange.*"};
        for (int i = 0; i < routingKeys.length; i++) {
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKeys[i]);
        }
        System.out.println("Consumer Wating Receive Message");

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" [C] Received '" + message + "', 处理业务中...");
            }
        };

        channel.basicConsume(QUEUE_NAME, true, consumer);
        System.out.println("consumer channel setted");
    }

}


package com.suirui.mq.rabbit.topic;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author zongx
 * @Date 2020/5/9 16:44
 * @Version 1.0
 */
public class Consumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(AMQP.PROTOCOL.PORT);
        factory.setUsername("guest");
        factory.setPassword("guest");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String EXCHANGE_NAME = "exchange.topic";
        String QUEUE_NAME = "queue.topic.q2";
        //声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        //绑定队列
        String[] routingKeys = {"*.*.rabbit", "lazy.#"};
        for (int i = 0; i < routingKeys.length; i++) {
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKeys[i]);
        }
        System.out.println("Consumer Wating Receive Message");

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" [C] Received '" + message + "', 处理业务中...");
            }
        };

        channel.basicConsume(QUEUE_NAME, true, consumer);
        System.out.println("consumer channel setted");
    }

}

RabbitMq——6、主题交换机Topics_第1张图片

RabbitMq——6、主题交换机Topics_第2张图片

RabbitMq——6、主题交换机Topics_第3张图片

RabbitMq——6、主题交换机Topics_第4张图片

你可能感兴趣的:(MQ)