18
18
19
19
import java .io .IOException ;
20
20
import java .time .Duration ;
21
+ import java .util .List ;
22
+ import java .util .Map ;
23
+
24
+ import org .apache .kafka .clients .CommonClientConfigs ;
25
+ import org .apache .kafka .clients .consumer .ConsumerConfig ;
26
+ import org .apache .kafka .clients .producer .ProducerConfig ;
21
27
22
28
import org .springframework .beans .factory .ObjectProvider ;
23
29
import org .springframework .boot .autoconfigure .AutoConfiguration ;
26
32
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
27
33
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
28
34
import org .springframework .boot .autoconfigure .condition .ConditionalOnSingleCandidate ;
35
+ import org .springframework .boot .autoconfigure .kafka .KafkaConnectionDetails .Node ;
29
36
import org .springframework .boot .autoconfigure .kafka .KafkaProperties .Jaas ;
30
37
import org .springframework .boot .autoconfigure .kafka .KafkaProperties .Retry .Topic ;
31
38
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
56
63
* @author Eddú Meléndez
57
64
* @author Nakul Mishra
58
65
* @author Tomaz Fernandes
66
+ * @author Moritz Halbritter
67
+ * @author Andy Wilkinson
68
+ * @author Phillip Webb
59
69
* @since 1.5.0
60
70
*/
61
71
@ AutoConfiguration
@@ -66,8 +76,12 @@ public class KafkaAutoConfiguration {
66
76
67
77
private final KafkaProperties properties ;
68
78
69
- public KafkaAutoConfiguration (KafkaProperties properties ) {
79
+ private final KafkaConnectionDetails connectionDetails ;
80
+
81
+ KafkaAutoConfiguration (KafkaProperties properties , ObjectProvider <KafkaConnectionDetails > connectionDetails ) {
70
82
this .properties = properties ;
83
+ this .connectionDetails = connectionDetails
84
+ .getIfAvailable (() -> new PropertiesKafkaConnectionDetails (properties ));
71
85
}
72
86
73
87
@ Bean
@@ -94,8 +108,9 @@ public LoggingProducerListener<Object, Object> kafkaProducerListener() {
94
108
@ ConditionalOnMissingBean (ConsumerFactory .class )
95
109
public DefaultKafkaConsumerFactory <?, ?> kafkaConsumerFactory (
96
110
ObjectProvider <DefaultKafkaConsumerFactoryCustomizer > customizers ) {
97
- DefaultKafkaConsumerFactory <Object , Object > factory = new DefaultKafkaConsumerFactory <>(
98
- this .properties .buildConsumerProperties ());
111
+ Map <String , Object > properties = this .properties .buildConsumerProperties ();
112
+ applyKafkaConnectionDetailsForConsumer (properties );
113
+ DefaultKafkaConsumerFactory <Object , Object > factory = new DefaultKafkaConsumerFactory <>(properties );
99
114
customizers .orderedStream ().forEach ((customizer ) -> customizer .customize (factory ));
100
115
return factory ;
101
116
}
@@ -104,8 +119,9 @@ public LoggingProducerListener<Object, Object> kafkaProducerListener() {
104
119
@ ConditionalOnMissingBean (ProducerFactory .class )
105
120
public DefaultKafkaProducerFactory <?, ?> kafkaProducerFactory (
106
121
ObjectProvider <DefaultKafkaProducerFactoryCustomizer > customizers ) {
107
- DefaultKafkaProducerFactory <?, ?> factory = new DefaultKafkaProducerFactory <>(
108
- this .properties .buildProducerProperties ());
122
+ Map <String , Object > properties = this .properties .buildProducerProperties ();
123
+ applyKafkaConnectionDetailsForProducer (properties );
124
+ DefaultKafkaProducerFactory <?, ?> factory = new DefaultKafkaProducerFactory <>(properties );
109
125
String transactionIdPrefix = this .properties .getProducer ().getTransactionIdPrefix ();
110
126
if (transactionIdPrefix != null ) {
111
127
factory .setTransactionIdPrefix (transactionIdPrefix );
@@ -140,7 +156,9 @@ public KafkaJaasLoginModuleInitializer kafkaJaasInitializer() throws IOException
140
156
@ Bean
141
157
@ ConditionalOnMissingBean
142
158
public KafkaAdmin kafkaAdmin () {
143
- KafkaAdmin kafkaAdmin = new KafkaAdmin (this .properties .buildAdminProperties ());
159
+ Map <String , Object > properties = this .properties .buildAdminProperties ();
160
+ applyKafkaConnectionDetailsForAdmin (properties );
161
+ KafkaAdmin kafkaAdmin = new KafkaAdmin (properties );
144
162
KafkaProperties .Admin admin = this .properties .getAdmin ();
145
163
if (admin .getCloseTimeout () != null ) {
146
164
kafkaAdmin .setCloseTimeout ((int ) admin .getCloseTimeout ().getSeconds ());
@@ -168,6 +186,34 @@ public RetryTopicConfiguration kafkaRetryTopicConfiguration(KafkaTemplate<?, ?>
168
186
return builder .create (kafkaTemplate );
169
187
}
170
188
189
+ private void applyKafkaConnectionDetailsForConsumer (Map <String , Object > properties ) {
190
+ properties .put (ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG ,
191
+ nodesToStringList (this .connectionDetails .getConsumerBootstrapNodes ()));
192
+ if (!(this .connectionDetails instanceof PropertiesKafkaConnectionDetails )) {
193
+ properties .put (CommonClientConfigs .SECURITY_PROTOCOL_CONFIG , "PLAINTEXT" );
194
+ }
195
+ }
196
+
197
+ private void applyKafkaConnectionDetailsForProducer (Map <String , Object > properties ) {
198
+ properties .put (ProducerConfig .BOOTSTRAP_SERVERS_CONFIG ,
199
+ nodesToStringList (this .connectionDetails .getProducerBootstrapNodes ()));
200
+ if (!(this .connectionDetails instanceof PropertiesKafkaConnectionDetails )) {
201
+ properties .put (CommonClientConfigs .SECURITY_PROTOCOL_CONFIG , "PLAINTEXT" );
202
+ }
203
+ }
204
+
205
+ private void applyKafkaConnectionDetailsForAdmin (Map <String , Object > properties ) {
206
+ properties .put (CommonClientConfigs .BOOTSTRAP_SERVERS_CONFIG ,
207
+ nodesToStringList (this .connectionDetails .getAdminBootstrapNodes ()));
208
+ if (!(this .connectionDetails instanceof PropertiesKafkaConnectionDetails )) {
209
+ properties .put (CommonClientConfigs .SECURITY_PROTOCOL_CONFIG , "PLAINTEXT" );
210
+ }
211
+ }
212
+
213
+ private List <String > nodesToStringList (List <Node > nodes ) {
214
+ return nodes .stream ().map ((node ) -> node .host () + ":" + node .port ()).toList ();
215
+ }
216
+
171
217
private static void setBackOffPolicy (RetryTopicConfigurationBuilder builder , Topic retryTopic ) {
172
218
long delay = (retryTopic .getDelay () != null ) ? retryTopic .getDelay ().toMillis () : 0 ;
173
219
if (delay > 0 ) {
0 commit comments