栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

RabbitMQ-SpringBoot案例 -fanout模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RabbitMQ-SpringBoot案例 -fanout模式

1、创建生产者工程 1、在pom.xml中引入依赖

        
            org.springframework.boot
            spring-boot-starter-amqp
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.amqp
            spring-rabbit-test
            test
        
    
2、配置application.yml
server:
  port: 8080

spring:
  # RabbitMQ配置
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin
    virtual-host: /

3、RabbitTemplate 配置类
package com.sky.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {

    // 声明注册交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanout_order_exchange");
    }

    // 声明队列
    @Bean
    public Queue smsQueue(){
        return new Queue("sms.fanout.queue",true);
    }

    @Bean
    public Queue duanxinQueue(){
        return new Queue("duanxin.fanout.queue",true);
    }

    @Bean
    public Queue emailQueue(){
        return new Queue("email.fanout.queue",true);
    }

    // 交换机和队列绑定
    @Bean
    public Binding smsBinding(){
        return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding duanxinBinding(){
        return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding emailBinding(){
        return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
    }

}

4、发送订单
package com.sky.service.impl;

import com.sky.service.OrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
@Service
public class OrderServiceImpl implements OrderService {	
	@Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void makeOrder(String userId, String productId, int num) {
        // 1:根据商品id查询库存是否充足

        //2:保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功"+orderId);
        //3:通过MQ来完成消息的分发
        // 参数1 : 交换机 参数2:路由key / 队列名称  参数3 :消息内容
        String exchangeName = "fanout_order_exchange";
        String routeKey = "";

        rabbitTemplate.convertAndSend(exchangeName,routeKey,orderId);
    }	
}
2、创建消费者工程 1、导入pom依赖

        
            org.springframework.boot
            spring-boot-starter-amqp
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.amqp
            spring-rabbit-test
            test
        
    
2、配置application.yml
server:
  port: 8081

spring:
  # RabbitMQ配置
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin
    virtual-host: /

3、编写接收消息service

消费者短信服务:

package com.sky.service.fanout;

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


@Service
@RabbitListener(queues = {"duanxin.fanout.queue"})
public class FanoutDuanxinConsumer {
    @RabbitHandler
    public void receiveMessage(String message){
        System.out.println("duanxin fanout ---->"+message);
    }
}

消费者邮件服务:

package com.sky.service.fanout;

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


@Service
@RabbitListener(queues = {"email.fanout.queue"})
public class FanoutEmailConsumer {

    @RabbitHandler
    public void receiveMessage(String message){
        System.out.println("duanxin fanout ---->"+message);
    }

}

消费者短信息服务:

package com.sky.service.fanout;

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


@Service
@RabbitListener(queues = {"sms.fanout.queue"})
public class FanoutSMSConsumer {

    @RabbitHandler
    public void receiveMessage(String message){
        System.out.println("sms fanout ---->"+message);
    }
}

3、测试 1、先开启生产者通过exchange(交换机)发送消息

还有一条没截到。。。

2、开启消费者

测试通过!

小结

看视频时看到老师说到一个面试题:

问到交换机的创建和队列声明还有绑定应该在消费者中还是生成者中?

  • 老师给出的答案是消费者中,因为防止用户访问的时候队列没创建或者没绑定报错,应该丢到消费者中
  • 但是我个人觉得两边都可以,因为一般都是先开启服务,再提供给消费者。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885930.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号