RabbitMq几种模式及消息发送确认,消费确认

模式1:简单模式(Simple / HelloWorld 单生产单消费)

简单模式实质上就是fanout模式,简单模式在投递消息的时候没有指定交换机,消息会被投递到一个默认的交换机

模式2:工作模式(Work单发送多接收)

工作模式和简单模式的区别在于消费者,生产者都是向默认交换机中投递消息,工作模式中多个消费者同时监听一个队列

模式3:发布、订阅模式(Publish/Subscribe)

上面两种模式实质上都是发布、订阅模式(fanout模式),只不过使用了默认交换机代替了手动创建交换机
fanout模式创建一个交换机,并把交换机和队列绑定,一个消费者监听一个队列

    //声明fanout模式交换机队列
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanout_exchange_name");
    }
    //声明队列
    @Bean
    public Queue smsQueue(){
        return new Queue("sms_fanout_queue",true);
    }
    //完成绑定关系(队列和交换机完成绑定关系)
    @Bean
    public Binding smsBinding(){
        return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
    }

模式4:路由模式(Routing)


在fanout模式的基础上使用routkey将交换机和队列绑定,生产者投递消息时根据routekey将消息投递进交换机,
交换机会根据routekey将消息发送到队列中。

    //声明fanout模式交换机队列
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("direct_exchange_name");
    }

    //声明队列
    @Bean
    public Queue directSmsQueue() {
        return new Queue("sms_direct_queue", true);
    }
    //完成绑定关系(队列和交换机完成绑定关系)
    @Bean
    public Binding directSmsBinding() {
        return BindingBuilder.bind(directSmsQueue()).to(directExchange()).with("sms");
    }

模式5:通配符(或主题)模式(Topics ,按topic发送接收)


使用更加自由灵活的topic通配符代替routekey

使用mq传输对象类型
rabbitmq只支持传输String,byte类型,实现Serializable接口的类且传递的对象的包名、类名、属性名必须一致
如果没有实现接口则会报如下异常

posted @ 2021-09-10 14:38  小白白白白白白白白白  阅读(456)  评论(1编辑  收藏  举报