From 4b77e2e205f46ef245cdd6f5e07ee03cd71258e0 Mon Sep 17 00:00:00 2001 From: Jonghan Kim Date: Wed, 26 Aug 2020 20:08:46 +0900 Subject: [PATCH 1/2] Add support for RabbitMQ's addressShuffleMode property See gh-23091 --- .../amqp/RabbitAutoConfiguration.java | 2 ++ .../boot/autoconfigure/amqp/RabbitProperties.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 6276bf358c..5e4e2f1e44 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -83,6 +83,7 @@ import org.springframework.context.annotation.Import; * @author Gary Russell * @author Phillip Webb * @author Artsiom Yudovin + * @author Jonghan Kim * @since 1.0.0 */ @Configuration(proxyBeanMethods = false) @@ -105,6 +106,7 @@ public class RabbitAutoConfiguration { .getObject()); PropertyMapper map = PropertyMapper.get(); map.from(properties::determineAddresses).to(factory::setAddresses); + map.from(properties::getAddressShuffleMode).whenNonNull().to(factory::setAddressShuffleMode); map.from(properties::isPublisherReturns).to(factory::setPublisherReturns); map.from(properties::getPublisherConfirmType).whenNonNull().to(factory::setPublisherConfirmType); RabbitProperties.Cache.Channel channel = properties.getCache().getChannel(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index f2c90aa585..c2388bae5f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Optional; import org.springframework.amqp.core.AcknowledgeMode; +import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.AddressShuffleMode; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.ConfirmType; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -41,6 +42,7 @@ import org.springframework.util.StringUtils; * @author Gary Russell * @author Artsiom Yudovin * @author Franjo Zilic + * @author Jonghan Kim * @since 1.0.0 */ @ConfigurationProperties(prefix = "spring.rabbitmq") @@ -87,6 +89,11 @@ public class RabbitProperties { */ private String addresses; + /** + * Shuffling mode for connecting host. + */ + private AddressShuffleMode addressShuffleMode; + /** * Requested heartbeat timeout; zero for none. If a duration suffix is not specified, * seconds will be used. @@ -282,6 +289,14 @@ public class RabbitProperties { this.virtualHost = "".equals(virtualHost) ? "/" : virtualHost; } + public AddressShuffleMode getAddressShuffleMode() { + return this.addressShuffleMode; + } + + public void setAddressShuffleMode(AddressShuffleMode addressShuffleMode) { + this.addressShuffleMode = addressShuffleMode; + } + public Duration getRequestedHeartbeat() { return this.requestedHeartbeat; } From 674ad85cdb8159000cf9b725574357d66271ca49 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 27 Aug 2020 14:26:58 +0200 Subject: [PATCH 2/2] Polish "Add support for RabbitMQ's addressShuffleMode property" See gh-23091 --- .../boot/autoconfigure/amqp/RabbitAutoConfiguration.java | 1 - .../boot/autoconfigure/amqp/RabbitProperties.java | 5 ++--- .../additional-spring-configuration-metadata.json | 4 ++++ .../autoconfigure/amqp/RabbitAutoConfigurationTests.java | 8 ++++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 5e4e2f1e44..3880bed03e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -83,7 +83,6 @@ import org.springframework.context.annotation.Import; * @author Gary Russell * @author Phillip Webb * @author Artsiom Yudovin - * @author Jonghan Kim * @since 1.0.0 */ @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index c2388bae5f..3699939f27 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -42,7 +42,6 @@ import org.springframework.util.StringUtils; * @author Gary Russell * @author Artsiom Yudovin * @author Franjo Zilic - * @author Jonghan Kim * @since 1.0.0 */ @ConfigurationProperties(prefix = "spring.rabbitmq") @@ -90,9 +89,9 @@ public class RabbitProperties { private String addresses; /** - * Shuffling mode for connecting host. + * Mode used to shuffle configured addresses. */ - private AddressShuffleMode addressShuffleMode; + private AddressShuffleMode addressShuffleMode = AddressShuffleMode.NONE; /** * Requested heartbeat timeout; zero for none. If a duration suffix is not specified, diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index e284a04425..bf9abe86dd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1538,6 +1538,10 @@ "name": "spring.r2dbc.pool.validation-depth", "defaultValue": "local" }, + { + "name": "spring.rabbitmq.address-shuffle-mode", + "defaultValue": "none" + }, { "name": "spring.rabbitmq.cache.connection.mode", "defaultValue": "channel" diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index 4888109471..0ccc5c34d3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -42,6 +42,7 @@ import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFac import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; +import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.AddressShuffleMode; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode; import org.springframework.amqp.rabbit.connection.ConnectionFactory; @@ -137,12 +138,15 @@ class RabbitAutoConfigurationTests { void testConnectionFactoryWithOverrides() { this.contextRunner.withUserConfiguration(TestConfiguration.class) .withPropertyValues("spring.rabbitmq.host:remote-server", "spring.rabbitmq.port:9000", - "spring.rabbitmq.username:alice", "spring.rabbitmq.password:secret", - "spring.rabbitmq.virtual_host:/vhost", "spring.rabbitmq.connection-timeout:123") + "spring.rabbitmq.address-shuffle-mode=random", "spring.rabbitmq.username:alice", + "spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost", + "spring.rabbitmq.connection-timeout:123") .run((context) -> { CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class); assertThat(connectionFactory.getHost()).isEqualTo("remote-server"); assertThat(connectionFactory.getPort()).isEqualTo(9000); + assertThat(connectionFactory).hasFieldOrPropertyWithValue("addressShuffleMode", + AddressShuffleMode.RANDOM); assertThat(connectionFactory.getVirtualHost()).isEqualTo("/vhost"); com.rabbitmq.client.ConnectionFactory rcf = connectionFactory.getRabbitConnectionFactory(); assertThat(rcf.getConnectionTimeout()).isEqualTo(123);