RabbitMQ交换机

交换机,接收消息,根据路由键转发消息到绑定的队列。

在这里插入图片描述

交换机类型

<dependency>
	<groupId>com.rabbitmq</groupId>
	<artifactId>amqp-client</artifactId>
	<version>3.6.5</version>
</dependency>

1.direct

在这里插入图片描述

所有发送到Direct Exchange的消息被转发到RouteKey中指定的Queue,
RouteKey用于队列和交换机绑定,全匹配
注意:Direct模式可以使用RabbitMQ自带的Exchange:default Exchange,所以不需要将Exchange进行任何绑定操作,消息传递时,RouteKey必须完全匹配才会被队列接收,否则该消息会被抛弃。

ProducerExchange

package com.example.rabbit.exchange.direct;

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

public class ProducerExchange {

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.通过channel发送数据
        String exchangeName="test_direct_exchange";
        String routingKey="test.direct";
        String exchangeType="direct";
        //声明一个交换机
        channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
        for(int i=1;i<=5;i++){
            String msg="Produce direct_exchange生产的第"+i+"条消息";
            channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());
        }
        //5.关闭相关连接
        channel.close();
        connection.close();

    }
}

ConsumerExchange

package com.example.rabbit.exchange.direct;

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

public class ConsumerExchange {
    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.声明(创建)一个队列
        //参数1:队列名称;参数2:是否持久化;参数3:是否独占;参数4:是否自动删除;参数5:扩展参数
        String exchangeName="test_direct_exchange";
        String exchangeType="direct";
        //用于交换机和队列绑定
        String routingKey="test.direct";
        String queueName = "test_direct_queue1";

        //声明一个交换机
        channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
        //声明一个队列
        channel.queueDeclare(queueName,true,false,false,null);
        //建立一个绑定关系
        channel.queueBind(queueName,exchangeName,routingKey);
        //5.创建一个消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        //6.设置Channel
        channel.basicConsume(queueName,true,queueingConsumer);
        //7.获取消息
        while (true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("Consumer消费的消息:"+msg);
        }

    }
}

2.topic

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
ConsumerTopicExchange1

package com.example.rabbit.exchange.topic;

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

/**
 * ConsumerTopicExchange1
 */
public class ConsumerTopicExchange1 {
    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.声明(创建)一个队列
        //参数1:队列名称;参数2:是否持久化;参数3:是否独占;参数4:是否自动删除;参数5:扩展参数
        String exchangeName="test_topic_exchange";
        String exchangeType="topic";
        //先测试user.#,在测试user.*,会同时绑定两个,需要去控制台Exchange先去解绑,才能测试出效果
        String routingKey="user.#";//#匹配一个或多个词
        //String routingKey="user.*";//*匹配一个词
        //String queueName = "test_topic_queue02";
        String queueName = "test_topic_queue01";

        //声明一个交换机
        channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
        //声明一个队列
        channel.queueDeclare(queueName,true,false,false,null);
        //建立一个绑定关系
        channel.queueBind(queueName,exchangeName,routingKey);
        //5.创建一个消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        //6.设置Channel
        channel.basicConsume(queueName,true,queueingConsumer);
        //7.获取消息
        while (true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("Consumer消费的消息:"+msg);
            //Envelope envelope = delivery.getEnvelope();
        }

    }
}

ConsumerTopicExchange2

package com.example.rabbit.exchange.topic;

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

public class ConsumerTopicExchange2 {
    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.声明(创建)一个队列
        //参数1:队列名称;参数2:是否持久化;参数3:是否独占;参数4:是否自动删除;参数5:扩展参数
        String exchangeName="test_topic_exchange";
        String exchangeType="topic";
        //先测试user.#,在测试user.*,会同时绑定两个,需要去控制台Exchange先去解绑,才能测试出效果
        //String routingKey="user.#";//#匹配一个或多个词
        String routingKey="user.*";//*匹配一个词
        String queueName = "test_topic_queue02";
        //String queueName = "test_topic_queue01";

        //声明一个交换机
        channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
        //声明一个队列
        channel.queueDeclare(queueName,true,false,false,null);
        //建立一个绑定关系
        channel.queueBind(queueName,exchangeName,routingKey);
        //5.创建一个消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        //6.设置Channel
        channel.basicConsume(queueName,true,queueingConsumer);
        //7.获取消息
        while (true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("Consumer消费的消息:"+msg);
            //Envelope envelope = delivery.getEnvelope();
        }

    }
}

ConsumerTopicExchange3

package com.example.rabbit.exchange.topic;

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

public class ConsumerTopicExchange3 {
    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.声明(创建)一个队列
        //参数1:队列名称;参数2:是否持久化;参数3:是否独占;参数4:是否自动删除;参数5:扩展参数
        String exchangeName="test_topic_exchange";
        String exchangeType="topic";
        //先测试user.#,在测试user.*,会同时绑定两个,需要去控制台Exchange先去解绑,才能测试出效果
        //String routingKey="user.#";//#匹配一个或多个词
        String routingKey="#.save";//*匹配一个词
        String queueName = "test_topic_queue03";
        //String queueName = "test_topic_queue01";

        //声明一个交换机
        channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
        //声明一个队列
        channel.queueDeclare(queueName,true,false,false,null);
        //建立一个绑定关系
        channel.queueBind(queueName,exchangeName,routingKey);
        //5.创建一个消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        //6.设置Channel
        channel.basicConsume(queueName,true,queueingConsumer);
        //7.获取消息
        while (true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("Consumer消费的消息:"+msg);  
        }

    }
}

ProducerTopicExchange

package com.example.rabbit.exchange.topic;

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


public class ProducerTopicExchange {

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.通过channel发送数据
        String exchangeName="test_topic_exchange";
        String routingKey1="user.save";
        String routingKey2="user.update";
        String routingKey3="user.delete.abc";
        String routingKey4="order.save";

        String msg1="Producer topic_exchange生产的消息"+routingKey1;
        String msg2="Producer topic_exchange生产的消息"+routingKey2;
        String msg3="Producer topic_exchange生产的消息"+routingKey3;
        String msg4="Producer topic_exchange生产的消息"+routingKey4;
        channel.basicPublish(exchangeName,routingKey1,null,msg1.getBytes());
        channel.basicPublish(exchangeName,routingKey2,null,msg2.getBytes());
        channel.basicPublish(exchangeName,routingKey3,null,msg3.getBytes());
        channel.basicPublish(exchangeName,routingKey4,null,msg4.getBytes());

        //5.关闭相关连接
        channel.close();
        connection.close();

    }
}

3.fanout
在这里插入图片描述

在这里插入图片描述
ConsumerFanoutExchange

package com.example.rabbit.exchange.fanout;

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

public class ConsumerFanoutExchange {
    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.声明(创建)一个队列
        //参数1:队列名称;参数2:是否持久化;参数3:是否独占;参数4:是否自动删除;参数5:扩展参数
        String exchangeName="test_fanout_exchange";
        String exchangeType="fanout";
        String routingKey="";//不设置路由键
        String queueName = "test_fanout_queue";

        //声明一个交换机
        channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
        //声明一个队列
        channel.queueDeclare(queueName,true,false,false,null);
        //建立一个绑定关系
        channel.queueBind(queueName,exchangeName,routingKey);
        //5.创建一个消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        //6.设置Channel
        channel.basicConsume(queueName,true,queueingConsumer);
        //7.获取消息
        while (true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("Consumer消费的消息:"+msg);
        }

    }
}

ProducerFanoutExchange

package com.example.rabbit.exchange.fanout;

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

public class ProducerFanoutExchange {

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.通过channel发送数据
        String exchangeName="test_fanout_exchange";
        String routingKey="123";//设置不设置路由键都无所谓

        for(int i=1;i<=5;i++){
            String msg="Procuder2 direct_exchange生产的第"+i+"条消息";
            channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());
        }
        //5.关闭相关连接
        channel.close();
        connection.close();

    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值