├── rabbit-multiple-spring-boot-starter ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── spring.factories │ │ └── java │ │ └── com │ │ └── kaikai │ │ └── starter │ │ ├── FirstRabbitMultipleProperties.java │ │ ├── SecondRabbitMultipleProperties.java │ │ ├── RetryTemplateFactory.java │ │ ├── RootRabbitMultipleProperties.java │ │ └── RabbitMultipleAutoConfiguration.java ├── pom.xml └── rabbit-multiple-spring-boot-starter.iml ├── Producer.java ├── FirstRabbitConfig.java ├── SecondRabbitConfig.java ├── README.md ├── RabbitUtil.java ├── RabbitOneConsumer.java └── RabbitSecondConsumer.java /rabbit-multiple-spring-boot-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.kaikai.starter.RabbitMultipleAutoConfiguration -------------------------------------------------------------------------------- /rabbit-multiple-spring-boot-starter/src/main/java/com/kaikai/starter/FirstRabbitMultipleProperties.java: -------------------------------------------------------------------------------- 1 | package com.kaikai.starter; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | * FirstRabbitMultipleProperties 7 | * 8 | * @author pu 9 | * @date 2020/12/25 10 | * time 10:01 11 | */ 12 | @ConfigurationProperties(prefix = "spring.rabbit.multiple.first") 13 | public class FirstRabbitMultipleProperties extends RootRabbitMultipleProperties { 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /rabbit-multiple-spring-boot-starter/src/main/java/com/kaikai/starter/SecondRabbitMultipleProperties.java: -------------------------------------------------------------------------------- 1 | package com.kaikai.starter; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | * FirstRabbitMultipleProperties 7 | * 8 | * @author pu 9 | * @date 2020/12/25 10 | * time 10:01 11 | */ 12 | @ConfigurationProperties(prefix = "spring.rabbit.multiple.second") 13 | public class SecondRabbitMultipleProperties extends RootRabbitMultipleProperties { 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Producer.java: -------------------------------------------------------------------------------- 1 | # rabbitmq-multiple-spring-boot-starter 2 | 用于解决springboot连接多个rabbitmq的starter 3 | 4 | 5 | @Component 6 | public class Producer{ 7 | /** 8 | * rabbit-one的发送对象 9 | */ 10 | @Autowired 11 | @Qualifier("firstRabbitTemplate") 12 | private RabbitTemplate firstRabbitTemplate; 13 | 14 | 15 | /** 16 | * rabbit-second的发送对象 17 | */ 18 | @Autowired 19 | @Qualifier("secondRabbitTemplate") 20 | private RabbitTemplate secondRabbitTemplate; 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /FirstRabbitConfig.java: -------------------------------------------------------------------------------- 1 | # rabbitmq-multiple-spring-boot-starter 2 | 用于解决springboot连接多个rabbitmq的starter 3 | /** 4 | * rabbit1配置 5 | * 6 | * @author kaikai 7 | */ 8 | @Configuration 9 | public class FirstRabbitConfig { 10 | 11 | @Autowired 12 | @Qualifier("firstAmqpAdmin") 13 | private AmqpAdmin amqpAdmin; 14 | 15 | 16 | @PostConstruct 17 | private void bind() { 18 | RabbitUtil.createQueueBindTopicExchange(amqpAdmin, RabbitConstant.TOPIC_EXCHANGE, RabbitConstant.SALARY_QUEUE_NAME, RabbitConstant.SALARY_ROUTING_KEY); 19 | RabbitUtil.createQueueBindTopicExchange(amqpAdmin, RabbitConstant.TOPIC_EXCHANGE, RabbitConstant.MONTH_SALARY_QUEUE_NAME, RabbitConstant.MONTH_SALARY_ROUTING_KEY); 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /SecondRabbitConfig.java: -------------------------------------------------------------------------------- 1 | # rabbitmq-multiple-spring-boot-starter 2 | 用于解决springboot连接多个rabbitmq的starter 3 | 4 | /** 5 | * rabbit2配置 6 | * 7 | * @author kaikai 8 | */ 9 | @Configuration 10 | public class SecondRabbitConfig { 11 | 12 | @Autowired 13 | @Qualifier("secondAmqpAdmin") 14 | private AmqpAdmin amqpAdmin; 15 | 16 | 17 | @PostConstruct 18 | private void bind() { 19 | //调用上面的工具类 20 | RabbitUtil.createQueueBindTopicExchange(amqpAdmin, RabbitConstant.TOPIC_EXCHANGE, RabbitConstant.SALARY_QUEUE_NAME, RabbitConstant.SALARY_ROUTING_KEY); 21 | RabbitUtil.createQueueBindTopicExchange(amqpAdmin, RabbitConstant.TOPIC_EXCHANGE, RabbitConstant.MONTH_SALARY_QUEUE_NAME, RabbitConstant.MONTH_SALARY_ROUTING_KEY); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rabbitmq-multiple-spring-boot-starter 2 | 用于解决springboot连接多个rabbitmq的starter 3 | 4 | ## 1.使用前先对项目进行打包 5 | 6 | ## 2.application.yaml的配置如下 7 | ``` 8 | spring: 9 | rabbit: 10 | multiple: 11 | rabbit1 12 | first: 13 | host: 192.168.0.12 14 | port: 5672 15 | username: rabbit1 16 | password: 123456 17 | virtual-host: / 18 | ackModel: MANUAL 19 | publisher-confirms: true 20 | publisher-returns: true 21 | rabbit2 22 | second: 23 | host: 192.168.0.12 24 | port: 5672 25 | username: rabbit2 26 | password: 123456 27 | virtual-host: / 28 | ackModel: MANUAL 29 | publisher-confirms: true 30 | publisher-returns: true 31 | ``` 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /RabbitUtil.java: -------------------------------------------------------------------------------- 1 | # rabbitmq-multiple-spring-boot-starter 2 | 用于解决springboot连接多个rabbitmq的starter 3 | /** 4 | * rabbit工具类 5 | * 6 | * @author pu 7 | * @date 2021/3/10 8 | * time 14:07 9 | */ 10 | public class RabbitUtil { 11 | /** 12 | * 13 | * @param amqpAdmin mq管理对象 14 | * @param topicName 交换机名 15 | * @param queueName 队列名 16 | * @param routingKey 路由key 17 | */ 18 | public static void createQueueBindTopicExchange(AmqpAdmin amqpAdmin,String topicName,String queueName,String routingKey){ 19 | TopicExchange topicExchange = new TopicExchange(topicName); 20 | amqpAdmin.declareExchange(topicExchange); 21 | Queue testQueue = new Queue(queueName,true); 22 | Binding bind = BindingBuilder.bind(testQueue).to(topicExchange).with(routingKey); 23 | amqpAdmin.declareQueue(testQueue); 24 | amqpAdmin.declareBinding(bind); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /rabbit-multiple-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 2.1.11.RELEASE 10 | 11 | 12 | com.kaikai.starter 13 | rabbit-multiple-spring-boot-starter 14 | 1.1.1 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-amqp 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /RabbitOneConsumer.java: -------------------------------------------------------------------------------- 1 | # rabbitmq-multiple-spring-boot-starter 2 | 用于解决springboot连接多个rabbitmq的starter 3 | 4 | package com.cdls.carp.open.consumer; 5 | 6 | import com.alibaba.fastjson.JSONObject; 7 | import com.cdls.carp.common.constant.RabbitConstant; 8 | import com.rabbitmq.client.Channel; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.amqp.core.Message; 11 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 12 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.beans.factory.annotation.Qualifier; 15 | import org.springframework.stereotype.Component; 16 | 17 | import java.io.IOException; 18 | 19 | /** 20 | * @author :pu 21 | * @description : 22 | * @create :2021-06-22 11:30:00 23 | */ 24 | @Component 25 | @Slf4j 26 | public class RabbitOneConsumer { 27 | 28 | /** 29 | * 监听中心mq的人员同步队列 30 | * 31 | * @param message 消息 32 | * @param channel 连接通道 33 | * @throws IOException 34 | //注意这里 35 | */ 36 | @RabbitListener(queues = "testQueue", containerFactory = "firstRabbitListenerContainerFactory") 37 | public void test(Message message, Channel channel) throws IOException { 38 | String json = new String(message.getBody(), "utf-8"); 39 | log.info("消息 json={}", json); 40 | //ack确认 41 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); 42 | 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /RabbitSecondConsumer.java: -------------------------------------------------------------------------------- 1 | # rabbitmq-multiple-spring-boot-starter 2 | 用于解决springboot连接多个rabbitmq的starter 3 | package com.cdls.carp.open.consumer; 4 | 5 | import com.alibaba.fastjson.JSONObject; 6 | import com.cdls.carp.common.constant.RabbitConstant; 7 | import com.rabbitmq.client.Channel; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.amqp.core.Message; 10 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 11 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.beans.factory.annotation.Qualifier; 14 | import org.springframework.stereotype.Component; 15 | 16 | import java.io.IOException; 17 | 18 | /** 19 | * @author :pu 20 | * @description : 21 | * @create :2021-06-22 11:30:00 22 | */ 23 | @Component 24 | @Slf4j 25 | public class RabbitSecondConsumer { 26 | 27 | /** 28 | * 监听中心mq的人员同步队列 29 | * 30 | * @param message 消息 31 | * @param channel 连接通道 32 | * @throws IOException 33 | //注意这里 34 | */ 35 | @RabbitListener(queues = "testQueue", containerFactory = "secondRabbitListenerContainerFactory") 36 | public void test(Message message, Channel channel) throws IOException { 37 | String json = new String(message.getBody(), "utf-8"); 38 | log.info("消息 json={}", json); 39 | //ack确认 40 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); 41 | 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /rabbit-multiple-spring-boot-starter/src/main/java/com/kaikai/starter/RetryTemplateFactory.java: -------------------------------------------------------------------------------- 1 | package com.kaikai.starter; 2 | 3 | import org.springframework.boot.autoconfigure.amqp.RabbitProperties; 4 | import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer; 5 | import org.springframework.boot.context.properties.PropertyMapper; 6 | import org.springframework.retry.backoff.ExponentialBackOffPolicy; 7 | import org.springframework.retry.policy.SimpleRetryPolicy; 8 | import org.springframework.retry.support.RetryTemplate; 9 | 10 | import java.time.Duration; 11 | import java.util.List; 12 | 13 | /** 14 | * RetryTemplateFactory 15 | * 16 | * @author pu 17 | * @date 2020/12/25 18 | * time 14:43 19 | */ 20 | class RetryTemplateFactory { 21 | 22 | private final List customizers; 23 | 24 | RetryTemplateFactory(List customizers) { 25 | this.customizers = customizers; 26 | } 27 | 28 | public RetryTemplate createRetryTemplate(RabbitProperties.Retry properties, 29 | RabbitRetryTemplateCustomizer.Target target) { 30 | PropertyMapper map = PropertyMapper.get(); 31 | RetryTemplate template = new RetryTemplate(); 32 | SimpleRetryPolicy policy = new SimpleRetryPolicy(); 33 | map.from(properties::getMaxAttempts).to(policy::setMaxAttempts); 34 | template.setRetryPolicy(policy); 35 | ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); 36 | map.from(properties::getInitialInterval).whenNonNull().as(Duration::toMillis) 37 | .to(backOffPolicy::setInitialInterval); 38 | map.from(properties::getMultiplier).to(backOffPolicy::setMultiplier); 39 | map.from(properties::getMaxInterval).whenNonNull().as(Duration::toMillis).to(backOffPolicy::setMaxInterval); 40 | template.setBackOffPolicy(backOffPolicy); 41 | if (this.customizers != null) { 42 | for (RabbitRetryTemplateCustomizer customizer : this.customizers) { 43 | customizer.customize(target, template); 44 | } 45 | } 46 | return template; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /rabbit-multiple-spring-boot-starter/rabbit-multiple-spring-boot-starter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /rabbit-multiple-spring-boot-starter/src/main/java/com/kaikai/starter/RootRabbitMultipleProperties.java: -------------------------------------------------------------------------------- 1 | package com.kaikai.starter; 2 | 3 | import org.springframework.amqp.core.AcknowledgeMode; 4 | import org.springframework.boot.autoconfigure.amqp.RabbitProperties; 5 | 6 | /** 7 | * RootRabbitMultipleProperties 8 | * 9 | * @author pu 10 | * @date 2020/12/25 11 | * time 9:57 12 | */ 13 | public class RootRabbitMultipleProperties { 14 | 15 | private String host; 16 | 17 | private Integer port; 18 | 19 | private String username; 20 | 21 | private String password; 22 | 23 | private String virtualHost; 24 | 25 | private Integer maxConsumerNumber=10; 26 | 27 | private Integer consumerNumber=5; 28 | 29 | private AcknowledgeMode ackModel=AcknowledgeMode.AUTO; 30 | 31 | /** 32 | * Whether to enable publisher confirms. 33 | */ 34 | private boolean publisherConfirms; 35 | 36 | /** 37 | * Whether to enable publisher returns. 38 | */ 39 | private boolean publisherReturns; 40 | 41 | private RabbitProperties.Cache cache = new RabbitProperties.Cache(); 42 | 43 | public RabbitProperties.Cache getCache() { 44 | return cache; 45 | } 46 | 47 | public void setCache(RabbitProperties.Cache cache) { 48 | this.cache = cache; 49 | } 50 | 51 | private RabbitProperties.Retry retry=new RabbitProperties.Retry(); 52 | 53 | public RabbitProperties.Retry getRetry() { 54 | return retry; 55 | } 56 | 57 | public void setRetry(RabbitProperties.Retry retry) { 58 | this.retry = retry; 59 | } 60 | 61 | public Integer getMaxConsumerNumber() { 62 | return maxConsumerNumber; 63 | } 64 | 65 | public void setMaxConsumerNumber(Integer maxConsumerNumber) { 66 | this.maxConsumerNumber = maxConsumerNumber; 67 | } 68 | 69 | public Integer getConsumerNumber() { 70 | return consumerNumber; 71 | } 72 | 73 | public void setConsumerNumber(Integer consumerNumber) { 74 | this.consumerNumber = consumerNumber; 75 | } 76 | 77 | public AcknowledgeMode getAckModel() { 78 | return ackModel; 79 | } 80 | 81 | public void setAckModel(AcknowledgeMode ackModel) { 82 | this.ackModel = ackModel; 83 | } 84 | 85 | public Integer getPrefetchCount() { 86 | return prefetchCount; 87 | } 88 | 89 | public void setPrefetchCount(Integer prefetchCount) { 90 | this.prefetchCount = prefetchCount; 91 | } 92 | 93 | private Integer prefetchCount=1; 94 | 95 | 96 | public String getHost() { 97 | return host; 98 | } 99 | 100 | public void setHost(String host) { 101 | this.host = host; 102 | } 103 | 104 | public Integer getPort() { 105 | return port; 106 | } 107 | 108 | public void setPort(Integer port) { 109 | this.port = port; 110 | } 111 | 112 | public String getUsername() { 113 | return username; 114 | } 115 | 116 | public void setUsername(String username) { 117 | this.username = username; 118 | } 119 | 120 | public String getPassword() { 121 | return password; 122 | } 123 | 124 | public void setPassword(String password) { 125 | this.password = password; 126 | } 127 | 128 | public String getVirtualHost() { 129 | return virtualHost; 130 | } 131 | 132 | public void setVirtualHost(String virtualHost) { 133 | this.virtualHost = virtualHost; 134 | } 135 | 136 | public boolean isPublisherConfirms() { 137 | return publisherConfirms; 138 | } 139 | 140 | public void setPublisherConfirms(boolean publisherConfirms) { 141 | this.publisherConfirms = publisherConfirms; 142 | } 143 | 144 | public boolean isPublisherReturns() { 145 | return publisherReturns; 146 | } 147 | 148 | public void setPublisherReturns(boolean publisherReturns) { 149 | this.publisherReturns = publisherReturns; 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /rabbit-multiple-spring-boot-starter/src/main/java/com/kaikai/starter/RabbitMultipleAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.kaikai.starter; 2 | 3 | import org.springframework.amqp.core.AcknowledgeMode; 4 | import org.springframework.amqp.core.AmqpAdmin; 5 | import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; 6 | import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 7 | import org.springframework.amqp.rabbit.connection.ConnectionFactory; 8 | import org.springframework.amqp.rabbit.connection.ConnectionNameStrategy; 9 | import org.springframework.amqp.rabbit.core.RabbitAdmin; 10 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 11 | import org.springframework.beans.factory.ObjectProvider; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.beans.factory.annotation.Qualifier; 14 | import org.springframework.boot.autoconfigure.amqp.RabbitProperties; 15 | import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer; 16 | import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer; 17 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 18 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 19 | import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; 20 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 21 | import org.springframework.boot.context.properties.PropertyMapper; 22 | import org.springframework.context.annotation.Bean; 23 | import org.springframework.context.annotation.Configuration; 24 | import org.springframework.context.annotation.Primary; 25 | import org.springframework.retry.support.RetryTemplate; 26 | 27 | import java.time.Duration; 28 | import java.util.stream.Collectors; 29 | 30 | /** 31 | * rabbitMultipleAutoConfiguration 32 | * 33 | * @author pu 34 | * @date 2020/12/25 35 | * time 9:56 36 | */ 37 | @Configuration 38 | @EnableConfigurationProperties({FirstRabbitMultipleProperties.class,SecondRabbitMultipleProperties.class}) 39 | @ConditionalOnMissingBean(ConnectionFactory.class) 40 | public class RabbitMultipleAutoConfiguration { 41 | 42 | 43 | 44 | @Bean(name="firstConnectionFactory") 45 | @Primary 46 | public ConnectionFactory hospSyncConnectionFactory(FirstRabbitMultipleProperties firstRabbitMultipleProperties,ObjectProvider connectionNameStrategy){ 47 | return getCachingConnectionFactory(firstRabbitMultipleProperties,connectionNameStrategy); 48 | } 49 | 50 | @Bean(name="secondConnectionFactory") 51 | public ConnectionFactory hPayConnectionFactory( SecondRabbitMultipleProperties secondRabbitMultipleProperties,ObjectProvider connectionNameStrategy){ 52 | return getCachingConnectionFactory(secondRabbitMultipleProperties,connectionNameStrategy); 53 | } 54 | @Bean(name="firstRabbitTemplate") 55 | @Primary 56 | public RabbitTemplate firstRabbitTemplate( 57 | @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory, 58 | ObjectProvider retryTemplateCustomizers, 59 | FirstRabbitMultipleProperties properties 60 | ){ 61 | RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); 62 | //使用外部事物 63 | //ydtRabbitTemplate.setChannelTransacted(true); 64 | if (properties.getRetry().isEnabled()) { 65 | rabbitTemplate.setRetryTemplate(new RetryTemplateFactory( 66 | retryTemplateCustomizers.orderedStream() 67 | .collect(Collectors.toList())).createRetryTemplate( 68 | properties.getRetry(), 69 | 70 | RabbitRetryTemplateCustomizer.Target.SENDER)); 71 | } 72 | return rabbitTemplate; 73 | } 74 | 75 | @Bean(name="secondRabbitTemplate") 76 | public RabbitTemplate secondRabbitTemplate( 77 | @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory, 78 | ObjectProvider retryTemplateCustomizers, 79 | SecondRabbitMultipleProperties properties 80 | ){ 81 | RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); 82 | //使用外部事物 83 | //lpzRabbitTemplate.setChannelTransacted(true); 84 | if (properties.getRetry().isEnabled()) { 85 | rabbitTemplate.setRetryTemplate(new RetryTemplateFactory( 86 | retryTemplateCustomizers.orderedStream() 87 | .collect(Collectors.toList())).createRetryTemplate( 88 | properties.getRetry(), 89 | RabbitRetryTemplateCustomizer.Target.SENDER)); 90 | } 91 | return rabbitTemplate; 92 | } 93 | 94 | @Bean(name="firstRabbitListenerContainerFactory") 95 | @Primary 96 | public SimpleRabbitListenerContainerFactory hospSyncFactory( 97 | SimpleRabbitListenerContainerFactoryConfigurer configurer, 98 | @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory, 99 | FirstRabbitMultipleProperties firstRabbitMultipleProperties 100 | ) { 101 | return getSimpleRabbitListenerContainerFactory(configurer,connectionFactory,firstRabbitMultipleProperties); 102 | } 103 | 104 | 105 | @Bean(name="secondRabbitListenerContainerFactory") 106 | public SimpleRabbitListenerContainerFactory hPayFactory( 107 | SimpleRabbitListenerContainerFactoryConfigurer configurer, 108 | @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory,SecondRabbitMultipleProperties secondRabbitMultipleProperties 109 | ) { 110 | return getSimpleRabbitListenerContainerFactory(configurer,connectionFactory,secondRabbitMultipleProperties); 111 | } 112 | @Bean("firstAmqpAdmin") 113 | @Primary 114 | // @ConditionalOnSingleCandidate(ConnectionFactory.class) 115 | @ConditionalOnProperty(prefix = "spring.rabbit.multiple.first", name = "dynamic", matchIfMissing = true) 116 | public AmqpAdmin firstAmqpAdmin(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) { 117 | return new RabbitAdmin(connectionFactory); 118 | } 119 | @Bean("secondAmqpAdmin") 120 | // @ConditionalOnSingleCandidate(ConnectionFactory.class) 121 | @ConditionalOnProperty(prefix = "spring.rabbit.multiple.second", name = "dynamic", matchIfMissing = true) 122 | public AmqpAdmin secondAmqpAdmin(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) { 123 | return new RabbitAdmin(connectionFactory); 124 | } 125 | 126 | private CachingConnectionFactory getCachingConnectionFactory(RootRabbitMultipleProperties rootRabbitMultipleProperties,ObjectProvider connectionNameStrategy){ 127 | CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); 128 | PropertyMapper map = PropertyMapper.get(); 129 | connectionFactory.setHost(rootRabbitMultipleProperties.getHost()); 130 | connectionFactory.setPort(rootRabbitMultipleProperties.getPort()); 131 | connectionFactory.setUsername(rootRabbitMultipleProperties.getUsername()); 132 | connectionFactory.setPassword(rootRabbitMultipleProperties.getPassword()); 133 | connectionFactory.setVirtualHost(rootRabbitMultipleProperties.getVirtualHost()); 134 | connectionFactory.setPublisherReturns(rootRabbitMultipleProperties.isPublisherReturns()); 135 | connectionFactory.setPublisherConfirms(rootRabbitMultipleProperties.isPublisherConfirms()); 136 | RabbitProperties.Cache.Channel channel = rootRabbitMultipleProperties.getCache().getChannel(); 137 | map.from(channel::getSize).whenNonNull().to(connectionFactory::setChannelCacheSize); 138 | map.from(channel::getCheckoutTimeout).whenNonNull().as(Duration::toMillis) 139 | .to(connectionFactory::setChannelCheckoutTimeout); 140 | RabbitProperties.Cache.Connection connection = rootRabbitMultipleProperties.getCache() 141 | .getConnection(); 142 | map.from(connection::getMode).whenNonNull().to(connectionFactory::setCacheMode); 143 | map.from(connection::getSize).whenNonNull() 144 | .to(connectionFactory::setConnectionCacheSize); 145 | map.from(connectionNameStrategy::getIfUnique).whenNonNull() 146 | .to(connectionFactory::setConnectionNameStrategy); 147 | return connectionFactory; 148 | } 149 | 150 | private SimpleRabbitListenerContainerFactory getSimpleRabbitListenerContainerFactory( 151 | SimpleRabbitListenerContainerFactoryConfigurer configurer, 152 | @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory,RootRabbitMultipleProperties rootRabbitMultipleProperties){ 153 | SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); 154 | configurer.configure(factory, connectionFactory); 155 | factory.setAcknowledgeMode(rootRabbitMultipleProperties.getAckModel()); 156 | factory.setConcurrentConsumers(rootRabbitMultipleProperties.getConsumerNumber()); 157 | // 最大消费者数量 158 | factory.setMaxConcurrentConsumers(rootRabbitMultipleProperties.getMaxConsumerNumber()); 159 | // 最大投递数 160 | factory.setPrefetchCount(rootRabbitMultipleProperties.getPrefetchCount()); 161 | return factory; 162 | } 163 | 164 | } 165 | --------------------------------------------------------------------------------