当前位置:   article > 正文

springboot配置多个rabbitMQ_springboot rabbitmq 需要多个rabbittemplate

springboot rabbitmq 需要多个rabbittemplate
  1. 本文链接:https://blog.csdn.net/qq_37608766/article/details/80912370
  2. https://blog.csdn.net/qq_37608766/article/details/80912370
  3. 1.application.yml
  4. spring:
  5.     rabbitmq:
  6.       hospSync:
  7.         host: localhost
  8.         port: 5672
  9.         username: test
  10.         password: test
  11.         virtual-host: test
  12.       pay:
  13.         host: localhost
  14.         port: 5672
  15.         username: demo
  16.         password: demo
  17.         virtual-host: demo
  18. 2.配置mq
  19. package com.medrd.hpay.config;
  20. import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
  21. import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  22. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  23. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  24. import org.springframework.beans.factory.annotation.Qualifier;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
  27. import org.springframework.context.annotation.Bean;
  28. import org.springframework.context.annotation.Configuration;
  29. import org.springframework.context.annotation.Primary;
  30. @Configuration
  31. public class RabbitMqConfiguration{
  32.     @Bean(name="hospSyncConnectionFactory")
  33.     @Primary
  34.     public ConnectionFactory hospSyncConnectionFactory(
  35.             @Value("${spring.rabbitmq.hospSync.host}") String host,
  36.             @Value("${spring.rabbitmq.hospSync.port}") int port,
  37.             @Value("${spring.rabbitmq.hospSync.username}") String username,
  38.             @Value("${spring.rabbitmq.hospSync.password}") String password,
  39.             @Value("${spring.rabbitmq.hospSync.virtual-host}") String virtualHost){
  40.         CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
  41.         connectionFactory.setHost(host);
  42.         connectionFactory.setPort(port);
  43.         connectionFactory.setUsername(username);
  44.         connectionFactory.setPassword(password);
  45.         connectionFactory.setVirtualHost(virtualHost);
  46.         return connectionFactory;
  47.     }
  48.     @Bean(name="hPayConnectionFactory")
  49.     public ConnectionFactory hPayConnectionFactory(
  50.             @Value("${spring.rabbitmq.pay.host}") String host,
  51.             @Value("${spring.rabbitmq.pay.port}") int port,
  52.             @Value("${spring.rabbitmq.pay.username}") String username,
  53.             @Value("${spring.rabbitmq.pay.password}") String password,
  54.             @Value("${spring.rabbitmq.pay.virtual-host}") String virtualHost){
  55.         CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
  56.         connectionFactory.setHost(host);
  57.         connectionFactory.setPort(port);
  58.         connectionFactory.setUsername(username);
  59.         connectionFactory.setPassword(password);
  60.         connectionFactory.setVirtualHost(virtualHost);
  61.         return connectionFactory;
  62.     }
  63.     @Bean(name="hospSyncRabbitTemplate")
  64. //    @Primary
  65.     public RabbitTemplate firstRabbitTemplate(
  66.             @Qualifier("hospSyncConnectionFactory") ConnectionFactory connectionFactory
  67.     ){
  68.         RabbitTemplate hospSyncRabbitTemplate = new RabbitTemplate(connectionFactory);
  69.         //使用外部事物
  70.         //ydtRabbitTemplate.setChannelTransacted(true);
  71.         return hospSyncRabbitTemplate;
  72.     }
  73.     @Bean(name="hPayRabbitTemplate")
  74.     @Primary
  75.     public RabbitTemplate secondRabbitTemplate(
  76.             @Qualifier("hPayConnectionFactory") ConnectionFactory connectionFactory
  77.     ){
  78.         RabbitTemplate hPayRabbitTemplate = new RabbitTemplate(connectionFactory);
  79.         //使用外部事物
  80.         //lpzRabbitTemplate.setChannelTransacted(true);
  81.         return hPayRabbitTemplate;
  82.     }
  83.         @Bean(name="hospSyncContainerFactory")
  84.     public SimpleRabbitListenerContainerFactory hospSyncFactory(
  85.             SimpleRabbitListenerContainerFactoryConfigurer configurer,
  86.             @Qualifier("hospSyncConnectionFactory") ConnectionFactory connectionFactory
  87.     ) {
  88.         SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
  89.         configurer.configure(factory, connectionFactory);
  90.         return factory;
  91.     }
  92.     @Bean(name="hPayContainerFactory")
  93.     public SimpleRabbitListenerContainerFactory hPayFactory(
  94.             SimpleRabbitListenerContainerFactoryConfigurer configurer,
  95.             @Qualifier("hPayConnectionFactory") ConnectionFactory connectionFactory
  96.     ) {
  97.         SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
  98.         configurer.configure(factory, connectionFactory);
  99.         return factory;
  100.     }
  101. }
  102. 3.监听(消费方)
  103. package com.medrd.hpay.domain;
  104. import org.slf4j.Logger;
  105. import org.slf4j.LoggerFactory;
  106. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  107. import org.springframework.messaging.handler.annotation.Header;
  108. import org.springframework.stereotype.Component;
  109. @Component
  110. public class RabbitMqMessage {
  111.     private static final Logger logger = LoggerFactory.getLogger(RabbitMqMessage.class);
  112.     @RabbitListener(queues = {"test.hospital.add","test.hospital.update","test.hospital.delete"}, containerFactory="hospSyncContainerFactory")
  113.     public void hospitalAdd(@Header("amqp_receivedRoutingKey") String routeKey, Object data) {
  114.         logger.debug("Reciver routeKey: {}, data: {}", data);
  115.     }
  116.     @RabbitListener(queues = {"demo.add","demo.update"}, containerFactory="hPayContainerFactory")
  117.     public void hPayAdd(@Header("amqp_receivedRoutingKey") String routeKey, String data) {
  118.         System.out.println(data);
  119.         System.out.println();
  120.     }
  121. }
  122. 4.生产者(消息生产方
  123. package com.paas.springboot.demo01;
  124.  
  125. import java.util.Date;
  126. import javax.annotation.Resource;
  127. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  128. import org.springframework.stereotype.Component;
  129.  
  130. @Component
  131. public class Sender {
  132.  
  133.     @Resource(name="hospSyncRabbitTemplate")
  134.     private RabbitTemplate hospSyncRabbitTemplate;
  135.  
  136.     public void send1() {
  137.         String context = "hello1 " + new Date();
  138.         System.out.println("Sender : " + context);
  139.         this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
  140.     }
  141.  
  142.     public void send2() {
  143.         String context = "hello2 " + new Date();
  144.         System.out.println("Sender : " + context);
  145.         this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
  146.     }
  147.  
  148. }
  149. 5.配置文件
  150. <dependency>
  151.     <groupId>org.springframework.boot</groupId>
  152.     <artifactId>spring-boot-starter-amqp</artifactId>
  153. </dependency>

 

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

闽ICP备14008679号