5.队列和交换机绑定

本文介绍了使用RabbitMQ进行微服务间通信的实现过程,包括创建ConnectionFactory、声明交换机、队列以及绑定。示例展示了如何在订单微服务中监听餐厅和骑手微服务的消息,通过同一个队列与两个交换机绑定,确保消息传递的正确性。测试结果显示绑定成功。
摘要由CSDN通过智能技术生成

交换机是由双方同时声明,队列是由接收方声明并配置绑定关系

1.先使用rabbitmq包进行开发【偏底层】,适合了解原理

目前是每两个微服务之间的通信使用一个单独的交换机:

1.订单微服务和 餐厅微服务之间的通信

,是在订单微服务中声明,监听餐厅发送的消息

 

1.首先创建ConnectionFactory

2.通过ConnectionFactory创建Connection

3.通过Connection创建Channel

4.声明交换机

5.声明消息队列 【如果还需要监听其他微服务发送的消息,还是用同一个队列】

6.将交换机和队列绑定,确定绑定key

      ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setHost("localhost");
        try (Connection connection = connectionFactory.newConnection();
             Channel channel = connection.createChannel()) {

            /*---------------------restaurant---------------------*/
            channel.exchangeDeclare(
                    "exchange.order.restaurant",//交换机名称
                    BuiltinExchangeType.DIRECT,//交换机类型
                    true, //是否持久化
                    false,//如果没有队列需要该交换机,mq是否将其删除
                    null);//是否需要特殊参数

            channel.queueDeclare(
                    "queue.order",//队列名称
                    true,//是否持久化
                    false,//该队列是不是被这个connection独占
                    false,
                    null);//是否需要特殊参数
           //将队列和交换机绑定,确定绑定key
            channel.queueBind(
                    "queue.order",
                    "exchange.order.restaurant",
                    "key.order");

2.订单微服务中监听 餐厅,骑手微服务发送的消息

 

 

 public void handleMessage() throws IOException, TimeoutException, InterruptedException {
        log.info("start linstening message");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setHost("localhost");
        try (Connection connection = connectionFactory.newConnection();
             Channel channel = connection.createChannel()) {

            /*---------------------restaurant---------------------*/
            channel.exchangeDeclare(
                    "exchange.order.restaurant",//交换机名称
                    BuiltinExchangeType.DIRECT,//交换机类型
                    true, //是否持久化
                    false,//如果没有队列需要该交换机,mq是否将其删除
                    null);//是否需要特殊参数

            channel.queueDeclare(
                    "queue.order",//队列名称
                    true,//是否持久化
                    false,//该队列是不是被这个connection独占
                    false,
                    null);//是否需要特殊参数
           //将队列和交换机绑定,确定绑定key
            channel.queueBind(
                    "queue.order",
                    "exchange.order.restaurant",
                    "key.order");


            /*---------------------deliveryman---------------------*/
            channel.exchangeDeclare(
                    "exchange.order.deliveryman",
                    BuiltinExchangeType.DIRECT,
                    true,
                    false,
                    null);


            channel.queueBind(
                    "queue.order",
                    "exchange.order.deliveryman",
                    "key.order");

 
           
        }
    }

 

声明了一个队列,俩个交换机, 将这个队列用相同的绑定key,将其绑定在两个交换机上

 

3.测试:

使用main方法进行测试

 

  public static void handleMessage() throws IOException, TimeoutException, InterruptedException {
        log.info("start linstening message");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setHost("localhost");
        try (Connection connection = connectionFactory.newConnection();
             Channel channel = connection.createChannel()) {

            /*---------------------restaurant---------------------*/
            channel.exchangeDeclare(
                    "exchange.order.111",//交换机名称
                    BuiltinExchangeType.DIRECT,//交换机类型
                    true, //是否持久化
                    false,//如果没有队列需要该交换机,mq是否将其删除
                    null);//是否需要特殊参数

            channel.queueDeclare(
                    "queue.order",//队列名称
                    true,//是否持久化
                    false,//该队列是不是被这个connection独占
                    false,
                    null);//是否需要特殊参数
           //将队列和交换机绑定,确定绑定key
            channel.queueBind(
                    "queue.order",
                    "exchange.order.restaurant",
                    "key.order");


            /*---------------------deliveryman---------------------*/
            channel.exchangeDeclare(
                    "exchange.order.222",
                    BuiltinExchangeType.DIRECT,
                    true,
                    false,
                    null);


            channel.queueBind(
                    "queue.order",
                    "exchange.order.deliveryman",
                    "key.order");

        }
    }


    public static void main(String[] args) throws InterruptedException, TimeoutException, IOException {
            handleMessage();
    }

1.启动rabbitmq网页端:

2.找到exchange

运行后,能够找到,即绑定成功。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值