1、依赖

  1. springBoot整合(生产消费依赖相同)
    1. 核心包 spring-boot-starter-amqp
      1. <dependencies>
      2. <dependency>
      3. <groupId>org.springframework.boot</groupId>
      4. <artifactId>spring-boot-starter-amqp</artifactId>
      5. </dependency>
      6. <dependency>
      7. <groupId>org.springframework.boot</groupId>
      8. <artifactId>spring-boot-starter-test</artifactId>
      9. </dependency>
      10. <dependency>
      11. <groupId>org.springframework.boot</groupId>
      12. <artifactId>spring-boot-starter-logging</artifactId>
      13. </dependency>
      14. </dependencies>

2、SpringBoot + RabbitMQ

  1. 按照2步骤的依赖分区引入
  2. 启动类 ```java package com.lixin.rabbitMq;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

  • “生产者” rabbitMQ启动类 *
  • @author EX-lixin028
  • @date 2021-04-30 17:10 */ @SpringBootApplication public class RabbitMqProducerApplication {

    public static void main(String[] args) {

    1. SpringApplication.run(RabbitMqProducerApplication.class);

    } } ```

  1. 配置文件

    1. server:
    2. port: 44000
    3. spring:
    4. application:
    5. name: rabbitMq-producer
    6. rabbitmq:
    7. host: localhost
    8. port: 5672
    9. username: guest
    10. password: guest
    11. virtual-host: /
  2. 配置类 ```java package com.lixin.rabbitMq.config;

import com.lixin.constant.RabbitMqConstant; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

/**

  • 测试topic模式 *
  • @author EX-lixin028
  • @date 2021-04-30 17:16 */ @Configuration public class RabbitMqConfig {

    /**

    • email队列名称 / private static final String TOPIC_EMAIL = “topic_email”; /*
    • sms队列名称 / private static final String TOPIC_SMS = “topic_sms”; /*
    • 交换机 / private static final String EXCHANGE_TOPIC = “exchange_topic”; /*
    • 路由_sms / private static final String ROUTER_SMS = “topic.#.sms.#”; /*
    • 路由_email */ private static final String ROUTER_EMAIL = “topic.#.email.#”;

      /**

    • 交换机 *
    • @return */ @Bean(EXCHANGE_TOPIC) public Exchange exchangeTopic() { return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC).durable(true).build(); }

      /**

    • 队列_email *
    • @return */ @Bean(TOPIC_EMAIL) public Queue topicEmailQueue() { return new Queue(TOPIC_EMAIL, true, false, false, null); }

      /**

    • 队列_sms *
    • @return */ @Bean(TOPIC_SMS) public Queue topicSmsQueue() { return new Queue(TOPIC_SMS, true, false, false, null); }

      /**

    • 绑定队列_email到交换机上 *
    • @param topicEmail email
    • @param exchangeTopic 交换机
    • @return */ @Bean public Binding bindingRouterEmail(@Qualifier(value = TOPIC_EMAIL) Queue topicEmail,

      1. @Qualifier(value = EXCHANGE_TOPIC) Exchange exchangeTopic) {

      return BindingBuilder.bind(topicEmail).to(exchangeTopic).with(ROUTER_EMAIL).noargs(); }

      /**

    • 绑定队列_sms到交换机上 *
    • @param topicSms sms
    • @param exchangeTopic 交换机
    • @return */ @Bean public Binding bindingRouterSms(@Qualifier(value = TOPIC_SMS) Queue topicSms,
      1. @Qualifier(value = EXCHANGE_TOPIC) Exchange exchangeTopic) {
      return BindingBuilder.bind(topicSms).to(exchangeTopic).with(ROUTER_SMS).noargs(); } } ```