Merge branch '2.1.x'

Closes gh-18885
This commit is contained in:
Stephane Nicoll
2019-11-05 14:17:35 +01:00
4 changed files with 65 additions and 3 deletions

View File

@@ -125,7 +125,7 @@ public class RabbitAutoConfiguration {
map.from(properties::getRequestedHeartbeat).whenNonNull().asInt(Duration::getSeconds)
.to(factory::setRequestedHeartbeat);
RabbitProperties.Ssl ssl = properties.getSsl();
if (ssl.isEnabled()) {
if (ssl.determineEnabled()) {
factory.setUseSSL(true);
map.from(ssl::getAlgorithm).whenNonNull().to(factory::setSslAlgorithm);
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);

View File

@@ -322,7 +322,7 @@ public class RabbitProperties {
return this.template;
}
public static class Ssl {
public class Ssl {
/**
* Whether to enable SSL support.
@@ -378,6 +378,21 @@ public class RabbitProperties {
return this.enabled;
}
/**
* Returns whether SSL is enabled from the first address, or the configured ssl
* enabled flag if no addresses have been set.
* @return whether ssl is enabled
* @see #setAddresses(String)
* @see #isEnabled()
*/
public boolean determineEnabled() {
if (CollectionUtils.isEmpty(RabbitProperties.this.parsedAddresses)) {
return isEnabled();
}
Address address = RabbitProperties.this.parsedAddresses.get(0);
return address.secureConnection;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@@ -960,6 +975,10 @@ public class RabbitProperties {
private static final int DEFAULT_PORT = 5672;
private static final String PREFIX_AMQP_SECURE = "amqps://";
private static final int DEFAULT_PORT_SECURE = 5671;
private String host;
private int port;
@@ -970,6 +989,8 @@ public class RabbitProperties {
private String virtualHost;
private boolean secureConnection;
private Address(String input) {
input = input.trim();
input = trimPrefix(input);
@@ -979,6 +1000,10 @@ public class RabbitProperties {
}
private String trimPrefix(String input) {
if (input.startsWith(PREFIX_AMQP_SECURE)) {
this.secureConnection = true;
return input.substring(PREFIX_AMQP_SECURE.length());
}
if (input.startsWith(PREFIX_AMQP)) {
input = input.substring(PREFIX_AMQP.length());
}
@@ -1015,7 +1040,7 @@ public class RabbitProperties {
int portIndex = input.indexOf(':');
if (portIndex == -1) {
this.host = input;
this.port = DEFAULT_PORT;
this.port = (this.secureConnection) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
}
else {
this.host = input.substring(0, portIndex);

View File

@@ -90,6 +90,18 @@ class RabbitPropertiesTests {
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
void determinePortUsingAmqpReturnsPortOfFirstAddress() {
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
void determinePortUsingAmqpsReturnsPortOfFirstAddress() {
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
assertThat(this.properties.determinePort()).isEqualTo(5671);
}
@Test
void virtualHostDefaultsToNull() {
assertThat(this.properties.getVirtualHost()).isNull();
@@ -223,6 +235,24 @@ class RabbitPropertiesTests {
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit.example.com:1234");
}
@Test
void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
void determineSslUsingAmqpReturnsStateOfFirstAddress() {
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
}
@Test
void determineSslReturnFlagPropertyWhenNoAddresses() {
this.properties.getSsl().setEnabled(true);
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
void simpleContainerUseConsistentDefaultValues() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

View File

@@ -5070,6 +5070,13 @@ For example, you might declare the following section in `application.properties`
spring.rabbitmq.password=secret
----
Alternatively, you could configure the same connection using the `addresses` attributes:
[source,properties,indent=0]
----
spring.rabbitmq.addresses=amqp://admin:secret@localhost
----
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`.
See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options.