RabbitMQ - SpringBoot 案例 - fanout 模式

HuDu發表於2021-05-21

整體核心

RabbitMQ - SpringBoot 案例 - fanout 模式

生產者模組

建立 springboot-rabbitmq-producer 的 springboot 專案

專案結構如下

RabbitMQ - SpringBoot 案例 - fanout 模式

web.xml 配置

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.yml 配置

server:
  port: 9900
  servlet:
    context-path: /producer
spring:
  application:
    name: springboot-rabbitmq-producer
  rabbitmq:
    host: xxx.xxx.xxx.xxx
    port: 5672
    virtual-host: /
    username: xxx
    password: xxx

服務層程式碼

@Service
public class OrderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 模擬使用者下單
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrder(String userId,String productId,int num) {
        // 1:根據id查詢商品是否充足
        // 2:儲存訂單
        String orderId = UUID.randomUUID().toString();
        System.out.println("訂單生成成功:"+orderId);
        // 3:通過 MQ 來完成訊息的分發
        // 交換機,路由 key/queue 佇列名稱,訊息內容
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }
}

config 配置類

@Configuration
public class RabbitMqConfiguration {

    // 1;宣告註冊 fanout 模式的交換機
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout_order_exchange",true,false);
    }
    // 2:宣告佇列 sms.fanout.queue email.fanout.queue weChat.fanout.queue
    @Bean
    public Queue smsQueue () {
        return new Queue("sms.fanout.queue",true);
    }

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

    @Bean
    public Queue weChatQueue () {
        return new Queue("weChat.fanout.queue",true);
    }
    // 3;完成繫結關係(佇列和交換機完成繫結關係)
    @Bean
    public Binding smsBinding() {
        return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
    }

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

    @Bean
    public Binding weChatBinding() {
        return BindingBuilder.bind(weChatQueue()).to(fanoutExchange());
    }
}

測試類

@SpringBootTest
class SpringbootRabbitmqProducerApplicationTests {

    @Autowired
    private OrderService orderService;

    @Test
    void contextLoads() {
        orderService.makeOrder("1","1",12);
    }

}

消費者模組

專案結構如下

RabbitMQ - SpringBoot 案例 - fanout 模式

和消費者模組區別在於修改一下埠

服務層程式碼如下

@Service
// 定義監聽的佇列
@RabbitListener(queues = {"email.fanout.queue"})
public class EmailConsumer {
    @RabbitHandler
    public void receiveMessage(String message) {
        System.out.println("email fanout--接收到的訂單資訊是:->" + message);
    }
}

@Service
@RabbitListener(queues = {"sms.fanout.queue"})
public class SMSConsumer {
  @RabbitHandler
  public void receiveMessage(String message) {
  System.out.println("sms fanout--接收到的訂單資訊是:->" + message);
  }
}

@Service
@RabbitListener(queues = {"weChat.fanout.queue"})
public class WeChatConsumer {
  @RabbitHandler
  public void receiveMessage(String message) {
  System.out.println("weChat fanout--接收到的訂單資訊是:->" + message);
  }
}

啟動消費者模組,再啟動生產者模組,效果如下

RabbitMQ - SpringBoot 案例 - fanout 模式

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章