Skip to content

Commit 8e4b8a8

Browse files
committed
Merge branch 'gh-34657'
Closes gh-34657
2 parents c0f59a1 + 8721c0e commit 8e4b8a8

File tree

81 files changed

+4271
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+4271
-518
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
18+
19+
/**
20+
* Adapts {@link ZipkinProperties} to {@link ZipkinConnectionDetails}.
21+
*
22+
* @author Moritz Halbritter
23+
* @author Andy Wilkinson
24+
* @author Phillip Webb
25+
*/
26+
class PropertiesZipkinConnectionDetails implements ZipkinConnectionDetails {
27+
28+
private final ZipkinProperties properties;
29+
30+
PropertiesZipkinConnectionDetails(ZipkinProperties properties) {
31+
this.properties = properties;
32+
}
33+
34+
@Override
35+
public String getSpanEndpoint() {
36+
return this.properties.getEndpoint();
37+
}
38+
39+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurations.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ static class UrlConnectionSenderConfiguration {
5959

6060
@Bean
6161
@ConditionalOnMissingBean(Sender.class)
62-
URLConnectionSender urlConnectionSender(ZipkinProperties properties) {
62+
URLConnectionSender urlConnectionSender(ZipkinProperties properties,
63+
ObjectProvider<ZipkinConnectionDetails> connectionDetailsProvider) {
64+
ZipkinConnectionDetails connectionDetails = connectionDetailsProvider
65+
.getIfAvailable(() -> new PropertiesZipkinConnectionDetails(properties));
6366
URLConnectionSender.Builder builder = URLConnectionSender.newBuilder();
6467
builder.connectTimeout((int) properties.getConnectTimeout().toMillis());
6568
builder.readTimeout((int) properties.getReadTimeout().toMillis());
66-
builder.endpoint(properties.getEndpoint());
69+
builder.endpoint(connectionDetails.getSpanEndpoint());
6770
return builder.build();
6871
}
6972

@@ -77,12 +80,15 @@ static class RestTemplateSenderConfiguration {
7780
@Bean
7881
@ConditionalOnMissingBean(Sender.class)
7982
ZipkinRestTemplateSender restTemplateSender(ZipkinProperties properties,
80-
ObjectProvider<ZipkinRestTemplateBuilderCustomizer> customizers) {
83+
ObjectProvider<ZipkinRestTemplateBuilderCustomizer> customizers,
84+
ObjectProvider<ZipkinConnectionDetails> connectionDetailsProvider) {
85+
ZipkinConnectionDetails connectionDetails = connectionDetailsProvider
86+
.getIfAvailable(() -> new PropertiesZipkinConnectionDetails(properties));
8187
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder()
8288
.setConnectTimeout(properties.getConnectTimeout())
8389
.setReadTimeout(properties.getReadTimeout());
8490
restTemplateBuilder = applyCustomizers(restTemplateBuilder, customizers);
85-
return new ZipkinRestTemplateSender(properties.getEndpoint(), restTemplateBuilder.build());
91+
return new ZipkinRestTemplateSender(connectionDetails.getSpanEndpoint(), restTemplateBuilder.build());
8692
}
8793

8894
private RestTemplateBuilder applyCustomizers(RestTemplateBuilder restTemplateBuilder,
@@ -106,10 +112,13 @@ static class WebClientSenderConfiguration {
106112
@Bean
107113
@ConditionalOnMissingBean(Sender.class)
108114
ZipkinWebClientSender webClientSender(ZipkinProperties properties,
109-
ObjectProvider<ZipkinWebClientBuilderCustomizer> customizers) {
115+
ObjectProvider<ZipkinWebClientBuilderCustomizer> customizers,
116+
ObjectProvider<ZipkinConnectionDetails> connectionDetailsProvider) {
117+
ZipkinConnectionDetails connectionDetails = connectionDetailsProvider
118+
.getIfAvailable(() -> new PropertiesZipkinConnectionDetails(properties));
110119
WebClient.Builder builder = WebClient.builder();
111120
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
112-
return new ZipkinWebClientSender(properties.getEndpoint(), builder.build());
121+
return new ZipkinWebClientSender(connectionDetails.getSpanEndpoint(), builder.build());
113122
}
114123

115124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
18+
19+
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
20+
21+
/**
22+
* Details required to establish a connection to a Zipkin server.
23+
*
24+
* @author Moritz Halbritter
25+
* @since 3.1.0
26+
*/
27+
public interface ZipkinConnectionDetails extends ConnectionDetails {
28+
29+
/**
30+
* The endpoint for the span reporting.
31+
* @return the endpoint
32+
*/
33+
String getSpanEndpoint();
34+
35+
}

spring-boot-project/spring-boot-autoconfigure/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ dependencies {
236236
testImplementation("org.junit.jupiter:junit-jupiter")
237237
testImplementation("org.mockito:mockito-core")
238238
testImplementation("org.mockito:mockito-junit-jupiter")
239+
testImplementation("org.postgresql:postgresql")
240+
testImplementation("org.postgresql:r2dbc-postgresql")
239241
testImplementation("org.skyscreamer:jsonassert")
240242
testImplementation("org.springframework:spring-test")
241243
testImplementation("org.springframework:spring-core-test")

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/AbstractConnectionFactoryConfigurer.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.amqp;
1818

19+
import java.util.stream.Collectors;
20+
1921
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
2022
import org.springframework.amqp.rabbit.connection.ConnectionNameStrategy;
2123
import org.springframework.boot.context.properties.PropertyMapper;
@@ -27,6 +29,9 @@
2729
*
2830
* @param <T> the connection factory type.
2931
* @author Chris Bono
32+
* @author Moritz Halbritter
33+
* @author Andy Wilkinson
34+
* @author Phillip Webb
3035
* @since 2.6.0
3136
*/
3237
public abstract class AbstractConnectionFactoryConfigurer<T extends AbstractConnectionFactory> {
@@ -35,9 +40,31 @@ public abstract class AbstractConnectionFactoryConfigurer<T extends AbstractConn
3540

3641
private ConnectionNameStrategy connectionNameStrategy;
3742

43+
private final RabbitConnectionDetails connectionDetails;
44+
45+
/**
46+
* Creates a new configurer that will configure the connection factory using the given
47+
* {@code properties}.
48+
* @param properties the properties to use to configure the connection factory
49+
*/
3850
protected AbstractConnectionFactoryConfigurer(RabbitProperties properties) {
39-
Assert.notNull(properties, "RabbitProperties must not be null");
51+
this(properties, new PropertiesRabbitConnectionDetails(properties));
52+
}
53+
54+
/**
55+
* Creates a new configurer that will configure the connection factory using the given
56+
* {@code properties} and {@code connectionDetails}, with the latter taking priority.
57+
* @param properties the properties to use to configure the connection factory
58+
* @param connectionDetails the connection details to use to configure the connection
59+
* factory
60+
* @since 3.1.0
61+
*/
62+
protected AbstractConnectionFactoryConfigurer(RabbitProperties properties,
63+
RabbitConnectionDetails connectionDetails) {
64+
Assert.notNull(properties, "Properties must not be null");
65+
Assert.notNull(connectionDetails, "ConnectionDetails must not be null");
4066
this.rabbitProperties = properties;
67+
this.connectionDetails = connectionDetails;
4168
}
4269

4370
protected final ConnectionNameStrategy getConnectionNameStrategy() {
@@ -55,7 +82,11 @@ public final void setConnectionNameStrategy(ConnectionNameStrategy connectionNam
5582
public final void configure(T connectionFactory) {
5683
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
5784
PropertyMapper map = PropertyMapper.get();
58-
map.from(this.rabbitProperties::determineAddresses).to(connectionFactory::setAddresses);
85+
String addresses = this.connectionDetails.getAddresses()
86+
.stream()
87+
.map((address) -> address.host() + ":" + address.port())
88+
.collect(Collectors.joining(","));
89+
map.from(addresses).to(connectionFactory::setAddresses);
5990
map.from(this.rabbitProperties::getAddressShuffleMode)
6091
.whenNonNull()
6192
.to(connectionFactory::setAddressShuffleMode);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/CachingConnectionFactoryConfigurer.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,32 @@
2525
* Configures Rabbit {@link CachingConnectionFactory} with sensible defaults.
2626
*
2727
* @author Chris Bono
28+
* @author Moritz Halbritter
29+
* @author Andy Wilkinson
30+
* @author Phillip Webb
2831
* @since 2.6.0
2932
*/
3033
public class CachingConnectionFactoryConfigurer extends AbstractConnectionFactoryConfigurer<CachingConnectionFactory> {
3134

35+
/**
36+
* Creates a new configurer that will configure the connection factory using the given
37+
* {@code properties}.
38+
* @param properties the properties to use to configure the connection factory
39+
*/
3240
public CachingConnectionFactoryConfigurer(RabbitProperties properties) {
33-
super(properties);
41+
this(properties, new PropertiesRabbitConnectionDetails(properties));
42+
}
43+
44+
/**
45+
* Creates a new configurer that will configure the connection factory using the given
46+
* {@code properties} and {@code connectionDetails}, with the latter taking priority.
47+
* @param properties the properties to use to configure the connection factory
48+
* @param connectionDetails the connection details to use to configure the connection
49+
* factory
50+
* @since 3.1.0
51+
*/
52+
public CachingConnectionFactoryConfigurer(RabbitProperties properties, RabbitConnectionDetails connectionDetails) {
53+
super(properties, connectionDetails);
3454
}
3555

3656
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.amqp;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
/**
23+
* Adapts {@link RabbitProperties} to {@link RabbitConnectionDetails}.
24+
*
25+
* @author Moritz Halbritter
26+
* @author Andy Wilkinson
27+
* @author Phillip Webb
28+
* @since 3.1.0
29+
*/
30+
public class PropertiesRabbitConnectionDetails implements RabbitConnectionDetails {
31+
32+
private final RabbitProperties properties;
33+
34+
public PropertiesRabbitConnectionDetails(RabbitProperties properties) {
35+
this.properties = properties;
36+
}
37+
38+
@Override
39+
public String getUsername() {
40+
return this.properties.determineUsername();
41+
}
42+
43+
@Override
44+
public String getPassword() {
45+
return this.properties.determinePassword();
46+
}
47+
48+
@Override
49+
public String getVirtualHost() {
50+
return this.properties.determineVirtualHost();
51+
}
52+
53+
@Override
54+
public List<Address> getAddresses() {
55+
List<Address> addresses = new ArrayList<>();
56+
for (String address : this.properties.determineAddresses().split(",")) {
57+
String[] components = address.split(":");
58+
addresses.add(new Address(components[0], Integer.parseInt(components[1])));
59+
}
60+
return addresses;
61+
}
62+
63+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,6 @@
5959
* <li>{@link org.springframework.amqp.core.AmqpAdmin } instance as long as
6060
* {@literal spring.rabbitmq.dynamic=true}.</li>
6161
* </ul>
62-
* <p>
63-
* The {@link org.springframework.amqp.rabbit.connection.CachingConnectionFactory} honors
64-
* the following properties:
65-
* <ul>
66-
* <li>{@literal spring.rabbitmq.port} is used to specify the port to which the client
67-
* should connect, and defaults to 5672.</li>
68-
* <li>{@literal spring.rabbitmq.username} is used to specify the (optional) username.
69-
* </li>
70-
* <li>{@literal spring.rabbitmq.password} is used to specify the (optional) password.
71-
* </li>
72-
* <li>{@literal spring.rabbitmq.host} is used to specify the host, and defaults to
73-
* {@literal localhost}.</li>
74-
* <li>{@literal spring.rabbitmq.virtualHost} is used to specify the (optional) virtual
75-
* host to which the client should connect.</li>
76-
* </ul>
7762
*
7863
* @author Greg Turnquist
7964
* @author Josh Long
@@ -82,6 +67,8 @@
8267
* @author Phillip Webb
8368
* @author Artsiom Yudovin
8469
* @author Chris Bono
70+
* @author Moritz Halbritter
71+
* @author Andy Wilkinson
8572
* @since 1.0.0
8673
*/
8774
@AutoConfiguration
@@ -93,23 +80,35 @@ public class RabbitAutoConfiguration {
9380
@Configuration(proxyBeanMethods = false)
9481
protected static class RabbitConnectionFactoryCreator {
9582

83+
private final RabbitProperties properties;
84+
85+
private final RabbitConnectionDetails connectionDetails;
86+
87+
protected RabbitConnectionFactoryCreator(RabbitProperties properties,
88+
ObjectProvider<RabbitConnectionDetails> connectionDetails) {
89+
this.properties = properties;
90+
this.connectionDetails = connectionDetails
91+
.getIfAvailable(() -> new PropertiesRabbitConnectionDetails(properties));
92+
}
93+
9694
@Bean
9795
@ConditionalOnMissingBean
98-
RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer(RabbitProperties properties,
99-
ResourceLoader resourceLoader, ObjectProvider<CredentialsProvider> credentialsProvider,
96+
RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer(ResourceLoader resourceLoader,
97+
ObjectProvider<CredentialsProvider> credentialsProvider,
10098
ObjectProvider<CredentialsRefreshService> credentialsRefreshService) {
10199
RabbitConnectionFactoryBeanConfigurer configurer = new RabbitConnectionFactoryBeanConfigurer(resourceLoader,
102-
properties);
100+
this.properties, this.connectionDetails);
103101
configurer.setCredentialsProvider(credentialsProvider.getIfUnique());
104102
configurer.setCredentialsRefreshService(credentialsRefreshService.getIfUnique());
105103
return configurer;
106104
}
107105

108106
@Bean
109107
@ConditionalOnMissingBean
110-
CachingConnectionFactoryConfigurer rabbitConnectionFactoryConfigurer(RabbitProperties rabbitProperties,
108+
CachingConnectionFactoryConfigurer rabbitConnectionFactoryConfigurer(
111109
ObjectProvider<ConnectionNameStrategy> connectionNameStrategy) {
112-
CachingConnectionFactoryConfigurer configurer = new CachingConnectionFactoryConfigurer(rabbitProperties);
110+
CachingConnectionFactoryConfigurer configurer = new CachingConnectionFactoryConfigurer(this.properties,
111+
this.connectionDetails);
113112
configurer.setConnectionNameStrategy(connectionNameStrategy.getIfUnique());
114113
return configurer;
115114
}

0 commit comments

Comments
 (0)