MQ介绍:消息队列中间件 (MQ中间件)
    作用:异步处理,应用解耦,流量削峰,消息通知
    1.Spring整合rabbitmq
    生产者;
    1. 引入依赖spring-boot-start-amqp
    2. 配置rabbitmq连接参数
    3. 注入RabbitTemplate发送消息
    消费者:
    1.引入依赖spring-boot-start-amqp
    2. 配置rabbitmq连接参数
    3. 配置监听 @RabbitListener 注解
    2.队列交换机绑定
    1@Configuration + @Bean
    java
    @Configuration
    public class FanoutConfig {
    /
    声明交换机
    @return Fanout类型交换机
    */
    @Bean
    public FanoutExchange fanoutExchange(){
    return new FanoutExchange(“itcast.fanout”);
    }
    /

    第1个队列
    /
    @Bean
    public Queue fanoutQueue1(){
    return new Queue(“fanout.queue1”);
    }
    /
    绑定队列和交换机
    /
    @Bean
    public Binding bindingQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange){
    return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }
    /

    第2个队列
    /
    @Bean
    public Queue fanoutQueue2(){
    return new Queue(“fanout.queue2”);
    }
    /
    绑定队列和交换机
    /
    @Bean
    public Binding bindingQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange){
    return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }
    }
    2.@RabbitListener中通过注解声明java
    @RabbitListener(bindings = @QueueBinding(
    value = @Queue(name = “topic.queue1”),
    exchange = @Exchange(name = “itcast.topic”, type = ExchangeTypes.TOPIC),
    key = “china.#”
    ))
    public void listenTopicQueue1(String msg){
    System.out.println(“消费者接收到topic.queue1的消息:【” + msg + “】”);
    }

    @RabbitListener(bindings = @QueueBinding(
    value = @Queue(name = “topic.queue2”),
    exchange = @Exchange(name = “itcast.topic”, type = ExchangeTypes.TOPIC),
    key = “#.news”
    ))
    public void listenTopicQueue2(String msg){
    System.out.println(“消费者接收到topic.queue2的消息:【” + msg + “】”);
    }
    3.消息转换器
    Spring通过MessageConverter实现消息的序列化反序列化
    java
    @Bean
    public MessageConverter jsonMessageConverter(){
    return new Jackson2JsonMessageConverter();
    }
    体积大,性能差 默认使用JDK序列化
    4.4种交换器类型
    Direct Exchange Fanout Exchange Topic Exchange Headers Exchange(性能差,不常用)