rabbitmq教程(四)

Routing 路由

在这个教程中,添加一个新的特性,我们可以只订阅消息的一部分。例如,将只连接我们感兴趣的颜色("orange", "black", "green"),并且把消息全部打印在控制台上。

绑定

交换器和队列是一种绑定关系。简单的理解为:队列对来自这个交换器中的信息感兴趣。

绑定可以加上一个额外的参数routingKeySpring-amqp使用通俗易懂的API(建造者模式)使它们之间的关系非常清晰。把交换器和队列放入BindingBuilder中并可以很容易的把队列用路由键(routingKey)绑定到交换器上。

直连交换器

前一个教程中我们的消息系统是以广播的形式传递给所有的消费者。我们想要扩展一下功能,加入基于颜色类型的过滤器。例如,我们想要程序实现接收详细的错误消息并写入硬盘作为日志,不接收Info或者警告日志。

fanout交换器不能实现这个操作,因为它只能笨笨的广播。

我们使用直连direct交换器替代。直连交换器背后的路由算法很简单,绑定的键要精确匹配消息的路由键后,这个消息才能进入队列中。


                                                                                            橙色、黑色、绿色三种路由键

如上图,直连交换器x上绑定了2个队列。第一个队列使用路由键是orange,第二个有2个路由键,blackgreen

在这个设定中,把一个使用路由键为orange的消息推送到交换器上时,那么这个消息将会被路由到队列Q1上。消息使用的路由键是black或者green时将会被路由到Q2。其余没有使用路由键的消息将会被丢弃。

并联绑定

这个可以实现类似fanout交换器的功能。

config.java

package com.example.rabbitmq.test4;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 刘仁楠
 * @date 2018/5/28 16:43
 */
@Configuration(value = "t4Config")
public class Config {

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("direct-exchange");
    }

    /**
     * 定义自动删除匿名队列
     * @author 刘仁楠
     * @date 2018/5/28 16:43
     */
    @Bean
    public Queue autoDeleteQueue0() {
        return new AnonymousQueue();
    }

    @Bean
    public Queue autoDeleteQueue1() {
        return new AnonymousQueue();
    }

    /**
     * 绑定使用路由键为 orange autoDeleteQueue0 队列到直连交换器上
     * @author 刘仁楠
     * @date 2018/5/28 16:44
     */
    @Bean
    public Binding binding0a(DirectExchange directExchange, Queue autoDeleteQueue0) {
        return BindingBuilder.bind(autoDeleteQueue0).to(directExchange).with("orange");
    }

    @Bean
    public Binding binding0b(DirectExchange directExchange, Queue autoDeleteQueue0) {
        return BindingBuilder.bind(autoDeleteQueue0).to(directExchange).with("black");
    }

    @Bean
    public Binding binding1a(DirectExchange directExchange, Queue autoDeleteQueue1) {
        return BindingBuilder.bind(autoDeleteQueue1).to(directExchange).with("black");
    }

    @Bean
    public Binding binding1b(DirectExchange directExchange, Queue autoDeleteQueue1) {
        return BindingBuilder.bind(autoDeleteQueue1).to(directExchange).with("green");
    }

}
receiver.java

package com.example.rabbitmq.test4;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 刘仁楠
 * @date 2018/5/28 16:45
 */
@Component(value = "t4Receiver")
public class Receiver {

    @RabbitListener(queues = "#{autoDeleteQueue0.name}")
    public void receiver0(String str) {
        System.out.println("receiver0++++++++++:" + str);
    }

    @RabbitListener(queues = "#{autoDeleteQueue1.name}")
    public void receiver1(String str) {
        System.out.println("receiver1++++++++++:" + str);
    }
}
send.java

package com.example.rabbitmq.test4;

import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 刘仁楠
 * @date 2018/5/28 16:46
 */
@Component(value = "t4Send")
public class Send {

    @Autowired
    private DirectExchange directExchange;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private String[] keys = {"orange", "black", "green"};

    /**
     * 如果是keys[0]那么只有receiver0     * 如果是keys[1]那么就是类似广播那样,有receive0receive1     * 如果是keys[2]那么只有receive1
     * @author 刘仁楠
     * @date 2018/5/28 16:48
     */
    public void send() {
        String message = "哈哈哈";
        for (int i = 0; i < 5; i++) {
            System.out.println("send++++++++++:".concat(message));
            rabbitTemplate.convertAndSend(directExchange.getName(), keys[2], message);
        }
    }
}

测试:

package com.example.rabbitmq;

import com.example.rabbitmq.test4.Send;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author 刘仁楠
 * @date 2018/5/28 16:46
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqApplicationTests4 {

    @Autowired
    private Send send;

    @Test
    public void send() throws Exception {
        send.send();
    }
}

结果:


源码地址

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值