1、依赖
- springBoot整合(生产消费依赖相同)
- 核心包 spring-boot-starter-amqp
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></dependency></dependencies>
- 核心包 spring-boot-starter-amqp
2、SpringBoot + RabbitMQ
- 按照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) {
SpringApplication.run(RabbitMqProducerApplication.class);
} } ```
配置文件
server:port: 44000spring:application:name: rabbitMq-producerrabbitmq:host: localhostport: 5672username: guestpassword: guestvirtual-host: /
配置类 ```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,
@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,
return BindingBuilder.bind(topicSms).to(exchangeTopic).with(ROUTER_SMS).noargs(); } } ```@Qualifier(value = EXCHANGE_TOPIC) Exchange exchangeTopic) {
