SpringBoot2.0系列--09--消息队列(Rabbit).md

Posted by lizhao on 07-09,2019

SpringBoot2.0系列--09--消息队列(Rabbit)

[toc]

前言

JDK出11了,SpringBoot出2.0了,还没有系统的学习过,刚好最近项目中有使用到,就把一些关键的东西列出来,避免忘记 SpringBoot2.0系列--00--目录

介绍

当项目需要拆分,分布式的时候一般需要使用消息队列,Rabbit作为一个消息中间件,在实际项目中使用的比重还是挺大的。

消息中间件最主要的作用是解耦,中间件最标准的用法是生产者生产消息传送到队列,消费者从队列中拿取消息并处理,生产者不用关心是谁来消费,消费者不用关心谁在生产消息,从而达到解耦的目的。

这边主要springboot和RabbitMQ的结合使用。具体介绍可以查看这篇:

// todo 某链接

总流程

  1. 安装rabbit,启动
  2. 配置引用
  3. 简单(一对一下消息)实例
  4. 一对多消息
  5. 多对多消息
  6. 发送对象
  7. Topic Exchange
  8. Fanout Exchange

具体操作

安装rabbit,启动

可以查看

// todo 某链接

配置引用

pom文件

<!-- rabbitmq需要的包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.properties文件

# 这几个是默认的配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

简单(一对一下消息)实例

创建队列配置

@Configuration
public class RabbitConfig {

    // 测试一对一
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }

    @Bean
    public Queue fooQueue() {
        return new Queue("foo");
}

创建生产者

/*
 * Copyright (C), 2015-2018
 * FileName: Sender
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 发送者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.easy;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 〈一句话功能简述〉<br>
 * 〈发送者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
public class Sender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        rabbitTemplate.convertAndSend("hello", context);
    }

    public void sendFoo() {
        String context = "foo " + new Date();
        System.out.println("Foo Sender  : " + context);
        rabbitTemplate.convertAndSend("foo", context);
    }

}

创建消费者

/*
 * Copyright (C), 2015-2018
 * FileName: Receiver
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 消费者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.easy;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈消费者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "hello")
public class Receiver {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver : " + hello);
    }

}

测试类及结果

这边发出去一条,接收到了一条

// 测试一对一
@Test
public void easy() {

    //  结果
    //        Sender : hello Wed Nov 14 19:33:16 GMT+08:00 2018
    //        Receiver : hello Wed Nov 14 19:33:16 GMT+08:00 2018
    sender.send();
    // sender.sendFoo();
}

一对多消息

创建队列配置

    // 测试一对多
    @Bean
    public Queue multimapQueue1() {
        return new Queue("OneToMany");
    }

创建生产者

/*
 * Copyright (C), 2015-2018
 * FileName: one2ManySender
 * Author:   zhao
 * Date:     2018/11/14 16:57
 * Description: 一对多翻译
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 〈一句话功能简述〉<br>
 * 〈一对多翻译〉
 *
 * @author zhao
 * @date 2018/11/14 16:57
 * @since 1.0.1
 */
@Component
public class OneToManySender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "OneToMany " + new Date();
        System.out.println("OneToManySender : " + context);
        rabbitTemplate.convertAndSend("OneToMany", context);
    }

}

创建消费者

/*
 * Copyright (C), 2015-2018
 * FileName: OneToManyReceiver1
 * Author:   zhao
 * Date:     2018/11/14 16:59
 * Description: 一对多接收者1
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈一对多接收者1〉
 *
 * @author zhao
 * @date 2018/11/14 16:59
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "OneToMany")
public class OneToManyReceiver1 {

    @RabbitHandler
    public void process(String foo) {
        System.out.println("OneToManyReceiver1 : " + foo);
    }

}

/*
 * Copyright (C), 2015-2018
 * FileName: OneToManyReceiver2
 * Author:   zhao
 * Date:     2018/11/14 16:59
 * Description: 一对多接收者2
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈一对多接收者2〉
 *
 * @author zhao
 * @date 2018/11/14 16:59
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "OneToMany")
public class OneToManyReceiver2 {

    @RabbitHandler
    public void process(String foo) {
        System.out.println("OneToManyReceiver2 : " + foo);
    }

}

测试类、说明及结果

一个发送端,2个接收端,可以看到结果是平均分布的

// 测试一对多消息
@Test
public void testOneToMany() throws Exception {
    // 一个发送端,2个接收端,可以看到结果是平均分布的
    // 结果
    // OneToManySender : OneToMany Wed Nov 14 19:37:17 GMT+08:00 2018
    //OneToManyReceiver2 : OneToMany Wed Nov 14 19:37:17 GMT+08:00 2018
    //OneToManySender : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManyReceiver1 : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManySender : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManyReceiver2 : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManySender : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManyReceiver1 : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManySender : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManyReceiver2 : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManySender : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManyReceiver1 : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManySender : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    //OneToManyReceiver2 : OneToMany Wed Nov 14 19:37:18 GMT+08:00 2018
    for (int i = 0; i < 100; i++) {
        oneToManySender.send();
        Thread.sleep(100);
    }
}

多对多消息

创建队列配置


// 测试多对多
@Bean
public Queue multimapQueue3() {
    return new Queue("manyToMany");
}

创建生产者

/*
 * Copyright (C), 2015-2018
 * FileName: ManyToManySender
 * Author:   zhao
 * Date:     2018/11/14 16:57
 * Description: 多对多翻译
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 〈一句话功能简述〉<br>
 * 〈多对多翻译〉
 *
 * @author zhao
 * @date 2018/11/14 16:57
 * @since 1.0.1
 */
@Component
public class ManyToManySender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "ManyToManySender1 " + new Date();
        System.out.println("ManyToManySender1 : " + context);
        rabbitTemplate.convertAndSend("manyToMany", context);
    }

}

/*
 * Copyright (C), 2015-2018
 * FileName: ManyToManySender
 * Author:   zhao
 * Date:     2018/11/14 16:57
 * Description: 多对多翻译
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 〈一句话功能简述〉<br>
 * 〈多对多翻译〉
 *
 * @author zhao
 * @date 2018/11/14 16:57
 * @since 1.0.1
 */
@Component
public class ManyToManySender2 {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "ManyToManySender2 " + new Date();
        System.out.println("ManyToManySender2 : " + context);
        rabbitTemplate.convertAndSend("manyToMany", context);
    }

}

创建消费者

/*
 * Copyright (C), 2015-2018
 * FileName: ManyToManyReceiver1
 * Author:   zhao
 * Date:     2018/11/14 16:59
 * Description: 多对多接收者1
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈多对多接收者1〉
 *
 * @author zhao
 * @date 2018/11/14 16:59
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "manyToMany")
public class ManyToManyReceiver1 {

    public int count = 0;

    @RabbitHandler
    public void process(String foo) {
        System.out.println("ManyToManyReceiver1 : " + foo);
        count++;
    }

}

/*
 * Copyright (C), 2015-2018
 * FileName: ManyToManyReceiver2
 * Author:   zhao
 * Date:     2018/11/14 16:59
 * Description: 多对多接收者2
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈多对多接收者2〉
 *
 * @author zhao
 * @date 2018/11/14 16:59
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "manyToMany")
public class ManyToManyReceiver2 {

    public int count = 0;
    @RabbitHandler
    public void process(String foo) {
        System.out.println("ManyToManyReceiver2 : " + foo);
        count++;
    }

}

/*
 * Copyright (C), 2015-2018
 * FileName: ManyToManyReceiver3
 * Author:   zhao
 * Date:     2018/11/14 16:59
 * Description: 多对多接收者1
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.multimap;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈多对多接收者3〉
 *
 * @author zhao
 * @date 2018/11/14 16:59
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "manyToMany")
public class ManyToManyReceiver3 {

    public int count = 0;
    @RabbitHandler
    public void process(String foo) {
        System.out.println("ManyToManyReceiver3 : " + foo);
        count++;
    }

}

测试类、说明及结果

这里是2对3的关系,结果看上去好像不是平均的,我们加上一个count,来统计各个接收者执行的次数,最后发现是66.67.67,所以是平均的

// 测试多对多消息
@Test
public void testManyToMany() throws Exception {
    //这里是2对3的关系,结果看上去好像不是平均的,
    // 我们加上一个count,来统计各个接收者执行的次数,最后发现是66.67.67,
    // 所以是平均的
    // 结果
    //        manyToManyReceiver1.count: 67
    //        manyToManyReceiver2.count: 66
    //        manyToManyReceiver3.count: 67
    //        ManyToManyReceiver3 : ManyToManySender1 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManyReceiver1 : ManyToManySender2 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManySender1 : ManyToManySender1 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManySender2 : ManyToManySender2 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManyReceiver2 : ManyToManySender1 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManyReceiver3 : ManyToManySender2 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManySender1 : ManyToManySender1 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManySender2 : ManyToManySender2 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManyReceiver2 : ManyToManySender2 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManyReceiver1 : ManyToManySender1 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManySender1 : ManyToManySender1 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManySender2 : ManyToManySender2 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManyReceiver1 : ManyToManySender2 Wed Nov 14 19:40:31 GMT+08:00 2018
    //        ManyToManyReceiver3 : ManyToManySender1 Wed Nov 14 19:40:31 GMT+08:00 2018

    for (int i = 0; i < 100; i++) {
        manyToManySender.send();
        manyToManySender2.send();
        Thread.sleep(100);
    }
    System.out.println(
            "manyToManyReceiver1.count: " + manyToManyReceiver1.count + "\n" + "manyToManyReceiver2.count: "
                    + manyToManyReceiver2.count + "\n" + "manyToManyReceiver3.count: " + manyToManyReceiver3.count
                    + "\n");

}

发送对象

创建队列配置

// 测试发送对象
@Bean
public Queue entityQueue() {
    return new Queue("entity");
}

对象

/*
 * Copyright (C), 2015-2018
 * FileName: User
 * Author:   zhao
 * Date:     2018/11/14 18:25
 * Description: 实体--用户
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.entity;

import java.io.Serializable;

/**
 * 〈一句话功能简述〉<br>
 * 〈实体--用户〉
 *
 * @author zhao
 * @date 2018/11/14 18:25
 * @since 1.0.1
 */
public class User implements Serializable {


    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" + "id=" + id + ", name='" + name + '\'' + '}';
    }
}

创建生产者

/*
 * Copyright (C), 2015-2018
 * FileName: Sender
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 发送者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.entity;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 〈一句话功能简述〉<br>
 * 〈发送者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
public class EntitySender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        User user = new User(1, "小王");
        System.out.println("Sender : " + user);
        rabbitTemplate.convertAndSend("entity", user);
    }

}

创建消费者

/*
 * Copyright (C), 2015-2018
 * FileName: Receiver
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 消费者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.entity;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈消费者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "entity")
public class EntityReceiver {

    @RabbitHandler
    public void process(User user) {
        System.out.println("Receiver : " + user.toString());
    }

}

测试类、说明及结果

让实体实现Serializable接口,就能直接发送了

// 测试发送实体
@Test
public void entity() {
    // 让实体实现Serializable接口,就能直接发送了
    // 结果
    // Sender : User{id=1, name='小王'}
    // Receiver : User{id=1, name='小王'}
    entitySender.send();
}

Topic Exchange

创建队列配置

// 测试topic
final static String message = "topic.message";
final static String messages = "topic.messages";

@Bean
public Queue queueMessage() {
    return new Queue(RabbitConfig.message);
}

@Bean
public Queue queueMessages() {
    return new Queue(RabbitConfig.messages);
}

@Bean
TopicExchange exchange() {
    return new TopicExchange("exchange");
}

@Bean
Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
    return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}

@Bean
Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
    return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}

创建生产者

/*
 * Copyright (C), 2015-2018
 * FileName: Sender
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 发送者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.topic;

import com.lizhaobolg.message.rabbit.advance.entity.User;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 〈一句话功能简述〉<br>
 * 〈发送者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
public class TopicSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send1() {
        String context = "hi, i am message 1";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("exchange", "topic.message", context);
    }

    public void send2() {
        String context = "hi, i am messages 2";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("exchange", "topic.messages", context);
    }
}

创建消费者

/*
 * Copyright (C), 2015-2018
 * FileName: Receiver
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 消费者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.topic;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈消费者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "topic.message")
public class TopicReceiver1 {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("TopicReceiver1 : " + hello);
    }

}

/*
 * Copyright (C), 2015-2018
 * FileName: Receiver
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 消费者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.topic;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈消费者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "topic.messages")
public class TopicReceiver2 {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("TopicReceiver2 : " + hello);
    }

}

测试类及结果

// topic -- 测试根据key来绑定队列
@Test
public void topic() {
    // 这里queueMessages这个队列,可以被2个key匹配,
    // 本身topic.messages会绑定过一个队列
    // 所以会执行2次topic.messages的消息

    // 结果
    // Sender : hi, i am message 1
    // Sender : hi, i am messages 2
    // TopicReceiver2 : hi, i am message 1
    // TopicReceiver1 : hi, i am message 1
    // TopicReceiver2 : hi, i am messages 2

    topicSender.send1();
    topicSender.send2();
}

Fanout Exchange

创建队列配置

// 测试fanout
    @Bean
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("fanout.C");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }

创建生产者

/*
 * Copyright (C), 2015-2018
 * FileName: Sender
 * Author:   zhao
 * Date:     2018/11/14 15:28
 * Description: 发送者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.fanout;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 〈一句话功能简述〉<br>
 * 〈发送者〉
 *
 * @author zhao
 * @date 2018/11/14 15:28
 * @since 1.0.1
 */
@Component
public class FanoutSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send1() {
        String context = "hi, fanout msg ";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
    }

}

创建消费者

/*
 * Copyright (C), 2015-2018
 * FileName: FanoutReceiver
 * Author:   zhao
 * Date:     2018/11/14 18:58
 * Description: fanout消费者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.fanout;

import com.lizhaobolg.message.rabbit.advance.entity.User;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈fanout消费者〉
 *
 * @author zhao
 * @date 2018/11/14 18:58
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("FanoutReceiverA : " + msg);
    }

}

/*
 * Copyright (C), 2015-2018
 * FileName: FanoutReceiver
 * Author:   zhao
 * Date:     2018/11/14 18:58
 * Description: fanout消费者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.fanout;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈fanout消费者〉
 *
 * @author zhao
 * @date 2018/11/14 18:58
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("FanoutReceiverB : " + msg);
    }

}

/*
 * Copyright (C), 2015-2018
 * FileName: FanoutReceiver
 * Author:   zhao
 * Date:     2018/11/14 18:58
 * Description: fanout消费者
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaobolg.message.rabbit.advance.fanout;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈fanout消费者〉
 *
 * @author zhao
 * @date 2018/11/14 18:58
 * @since 1.0.1
 */
@Component
@RabbitListener(queues = "fanout.C")
public class FanoutReceiverC {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("FanoutReceiverC : " + msg);
    }

}

测试类及结果

这里属于广播,所以发送一次,三个客户端都能接收到

// fanout -- 广播机制
@Test
public void fanout() {
    // 这里属于广播,所以发送一次,三个客户端都能接收到
    // 结果
    // Sender : hi, fanout msg
    // FanoutReceiverB : hi, fanout msg
    // FanoutReceiverC : hi, fanout msg
    // FanoutReceiverA : hi, fanout msg
    fanoutSender.send1();
}

参考链接

http://www.ityouknow.com/springboot/2016/11/30/spring-boot-rabbitMQ.html

联系方式

项目代码路径码云:https://gitee.com/lizhaoandroid/Springboot-Learning-lz

联系方式:QQ3060507060

查看下一篇或者其他文章,可点击目录或者专栏查看