当前位置:   article > 正文

使用注解的方式进行配置RabbitMQ

使用注解的方式进行配置RabbitMQ

引入依赖:

  1. <dependency>
  2. <groupId>org.springframework.amqp</groupId>
  3. <artifactId>spring-rabbit-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

配置application.yml 

  1. server:
  2. port: 8082
  3. spring:
  4. rabbitmq:
  5. host: 192.168.37.105
  6. port: 5672
  7. username: admin
  8. password: admin
  9. virtual-host: /
  10. shop:
  11. exchange: shop.exchange.direct
  12. queue: shop.queue
  13. routingKey: shop.routingKey

 编写配置类消息转化器

  1. @Configuration
  2. public class RabbitmqConfig {
  3. /**
  4. * 消息转化器
  5. * 将对象转化为 Json 字符串
  6. */
  7. @Bean
  8. public MessageConverter messageConverter(){
  9. return new Jackson2JsonMessageConverter();
  10. }
  11. }

创建生产者:

  1. @RestController
  2. @RequiredArgsConstructor
  3. @RequestMapping("producer03")
  4. public class Producer03Controller {
  5. private final RabbitTemplate rabbitTemplate;
  6. @GetMapping("/send01")
  7. public String sendMsgFanout(){
  8. rabbitTemplate.convertAndSend("exchange2.fanout","",new Student(2,"Alice","178",20));
  9. return "Producer02==>发送成功!";
  10. }
  11. @GetMapping("/send02")
  12. public String sendFanout(){
  13. rabbitTemplate.convertAndSend("exchange2.fanout","","Fanout发送消息!");
  14. return "Producer02==>发送成功!";
  15. }
  16. @GetMapping("/send03")
  17. public String sendMsgDirect(){
  18. rabbitTemplate.convertAndSend("exchange2.DIRECT","monian",new Student(2,"Alice","178",20));
  19. return "Producer02==>发送成功!";
  20. }
  21. @GetMapping("/send04")
  22. public String sendMsgDirectMsg(){
  23. rabbitTemplate.convertAndSend("exchange2.DIRECT","monian","发送的数据Direct!");
  24. return "Producer02==>发送成功!";
  25. }
  26. }

创建消费者:

  1. @Component
  2. public class MsgListener02 {
  3. /**
  4. * 使用注解的方式,对交换机和队列进行装配
  5. * 如果存在就不创建
  6. *
  7. * @param msg
  8. */
  9. @RabbitListener(bindings = @QueueBinding(
  10. value = @Queue(value = "queue02"),
  11. exchange = @Exchange(value = "exchange2.fanout", type = ExchangeTypes.FANOUT)
  12. ))
  13. public void readMsg(Student stu) {
  14. System.out.println("annotationListener::Fanout => :" + stu);
  15. }
  16. @RabbitListener(bindings = @QueueBinding(
  17. value = @Queue(value = "queue02"),
  18. exchange = @Exchange(value = "exchange2.fanout", type = ExchangeTypes.FANOUT)
  19. ))
  20. public void readMsg(String msg) {
  21. System.out.println("annotationListener::Fanout => :" + msg);
  22. }
  23. @RabbitListener(bindings = @QueueBinding(
  24. value = @Queue(value = "queue03"),
  25. exchange = @Exchange(value = "exchange2.DIRECT", type = ExchangeTypes.DIRECT),
  26. key = "monian"
  27. ))
  28. public void readMsgDirect(Student stu) {
  29. System.out.println("annotationListener::Direct => :" + stu);
  30. }
  31. @RabbitListener(bindings = @QueueBinding(
  32. value = @Queue(value = "queue03"),
  33. exchange = @Exchange(value = "exchange2.DIRECT", type = ExchangeTypes.DIRECT),
  34. key = "monian"
  35. ))
  36. public void readMsgDirect(String msg) {
  37. System.out.println("annotationListener::Direct => :" + msg);
  38. }
  39. }

编写Student:

  1. @AllArgsConstructor
  2. @NoArgsConstructor
  3. @Data
  4. public class Student implements Serializable {
  5. private Integer id;
  6. private String name;
  7. private String phone;
  8. private Integer age;
  9. }

测试结果: 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/590793
推荐阅读
相关标签
  

闽ICP备14008679号