基于spring-boot集成rabbit项目搭建。

  1. 创建生产者工程,pom依赖引入。
    <!--
    1. 父工程依赖
    -->
    <parent>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
    	<version>2.2.2.RELEASE</version>
    </parent>
    
    <dependencies>
    	<!--2. rabbitmq-->
    	<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>
    </dependencies>
    
  2. application.yml配置整合
    spring:
      rabbitmq:
    	host: 192.168.137.130
    	port: 5672
    	username: admin
    	password: 123456
    	virtual-host: /
    
  3. 创建配置类
    @Configuration
    public class RabbitMQConfig {
    	public static final String EXCHANGE_NAME = "boot_topic_exchange";
    	public static final String QUEUE_NAME = "boot_queue";
    
    	// 1 交换机
    	@Bean("bootExchange")
    	public Exchange bootExchange(){
    		//代码连编   ctrl+alt+v:补全返回值格式。类似于.var和.castvar
    		return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    	}
    	//2.Queue 队列
    	@Bean("bootQueue")
    	public Queue bootQueue(){
    		return QueueBuilder.durable(QUEUE_NAME).build();
    	}
    	//3. 队列和交互机绑定关系 Binding
    	/*
    		1. 知道哪个队列
    		2. 知道哪个交换机
    		3. routing key
    		noargs():表示不指定参数
    	 */
    	@Bean
    	public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
    									 @Qualifier("bootExchange") Exchange exchange){
    		return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    	}
    }
    
  4. 创建入口类
    @SpringBootApplication
    public class ProducerApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(ProducerApplication.class);
    	}
    }
    
  5. 发送消息
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ProducerTest {
    
    	@Autowired
    	private RabbitTemplate rabbitTemplate;
    
    	/**
    	 * 第一个参数:交换机名字
    	 * 第二个参数:routingKey
    	 * 第三个参数:发送的消息
    	 */
    	@Test
    	public void testSend(){
    		rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","mq hello");
    	}
    }
    
  6. 搭建消费者工程,pom依赖引入
    <parent>
    	 <groupId>org.springframework.boot</groupId>
    	 <artifactId>spring-boot-starter-parent</artifactId>
    	 <version>2.2.2.RELEASE</version>
    </parent>
    <dependencies>
    	<!--RabbitMQ 启动依赖-->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-amqp</artifactId>
    	</dependency>
    </dependencies>
    
  7. application.yml配置整合
    spring:
      rabbitmq:
        host: 192.168.137.130
        port: 5672
    	username: admin
    	password: 123456
    	virtual-host: /
    
  8. 消息监听器
    @Component
    public class RabbimtMQListener {
    	@RabbitListener(queues = "boot_queue")
    	public void listenerQueue(Message message){
    		System.out.println(new String(message.getBody()));
    	}
    }
    
  9. 创建入口类
    @SpringBootApplication
    public class ConsumerSpringbootApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(ConsumerSpringbootApplication.class, args);
    	}
    }
    
  10. 小结:
    • SpringBoot提供了快速整合RabbitMQ的方式
    • 基本信息在yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
    • 生产端直接注入RabbitTemplate完成消息发送
    • 消费端直接使用@RabbitListener完成消息接收
原文地址:https://www.cnblogs.com/rbwbear/p/15557819.html