0
点赞
收藏
分享

微信扫一扫

黑马JVM总结(十五)

伢赞 2023-09-21 阅读 32

生产者

  • 1.创建生产者SpringBoot工程
  • 2.引入依赖坐标
  • <dependency>
    • <groupld>org.springframework.boot</groupld>
    • <artifactld> spring-boot-starter-amqp</artifactld>
  • </dependency>
  • 3.编写yml配置,基本信息配置
  • 4.定义交换机,队列以及绑定关系的配置类
  • 5.注入RabbitTemplate,调用方法,完成消息发送

1.创建生产者工程

 

 2.导入依赖

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

 3.编写yml配置

spring:
rabbitmq:
host: 43.143.246.208
port: 5672
username: root
password: root
virtual-host: /itcast

 4.定义交换机,队列以及绑定关系的配置类

@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(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
//2.Queue 队列
@Bean("bootQueue")
public Queue bootQueue(){
return (Queue) QueueBuilder.durable(QUEUE_NAME).build();
}
//3. 队列和交互机绑定关系 Binding
/*
1. 知道哪个队列
2. 知道哪个交换机
3. routing key
*/

@Bean
public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}

5.注入RabbitTemplate,调用方法,完成消息发送

@SpringBootTest
class MqsendApplicationTests {
//1.注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSend(){
rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
}
}

6.运行结果 

 

消费者 

  • 1.创建消费者SpringBoot工程2.引入start,依赖坐标
  • <dependency>
    • <groupld> org.springframework.boot</groupld>
    • <artifactld> spring-boot-starter-amqp</artifactld>
  • </dependency>
  • 3.编写yml配置,基本信息配置
  • 4.定义监听类,使用@RabbitListener注解完成队列监听。
     

1.2.3同上 

4.定义监听类,使用@RabbitListener注解完成队列监听

@Component
public class RabbimtMQListener {
@RabbitListener(queues = "boot_queue")
public void ListenerQueue(Message message){
//System.out.println(message);
System.out.println(new String(message.getBody()));
}
}

启动 

 

运行结果 

小结

  • SpringBoot提供了快速整合RabbitMQ的方式
  • 基本信息在yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
  • 生产端直接注入RabbitTemplate完成消息发送
  • 消费端直接使用@RabbitListener完成消息接收
举报

相关推荐

0 条评论