Skip to content

Commit 3728281

Browse files
committed
GH-7646: Polishing
Add back custom config example, using include.
1 parent b0056bc commit 3728281

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4618,7 +4618,18 @@ Only a subset of the properties supported by Kafka are available via the `KafkaP
46184618
class. If you wish to configure the producer or consumer with additional properties that
46194619
are not directly supported, use the following:
46204620

4621-
`spring.kafka.properties.foo=bar`
4621+
`spring.kafka.properties.foo.bar=baz`
4622+
4623+
This sets the common `foo.bar` kafka property to `baz`.
4624+
4625+
These properties will be shared by both the consumer and producer factory beans.
4626+
If you wish to customize these components with different properties, such as to use a
4627+
different metrics reader for each, you can override the bean definitions, as follows:
4628+
4629+
[source,java,indent=0]
4630+
----
4631+
include::{code-examples}/kafka/KafkaSpecialProducerConsumerConfigExample.java[tag=configuration]
4632+
----
46224633

46234634
[[boot-features-restclient]]
46244635
== Calling REST services
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2016-2016 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+
* http://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.kafka;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import org.apache.kafka.clients.CommonClientConfigs;
23+
import org.apache.kafka.common.metrics.KafkaMetric;
24+
import org.apache.kafka.common.metrics.MetricsReporter;
25+
26+
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.kafka.core.ConsumerFactory;
30+
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
31+
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
32+
import org.springframework.kafka.core.ProducerFactory;
33+
34+
/**
35+
* Example custom kafka configuration beans used when the user wants to
36+
* apply different common properties to the producer and consumer.
37+
*
38+
* @author Gary Russell
39+
* @since 1.5
40+
*
41+
*/
42+
public class KafkaSpecialProducerConsumerConfigExample {
43+
44+
// tag::configuration[]
45+
@Configuration
46+
public static class CustomKafkaBeans {
47+
48+
/**
49+
* Customized ProducerFactory bean.
50+
* @param properties the kafka properties.
51+
* @return the bean.
52+
*/
53+
@Bean
54+
public ProducerFactory<?, ?> kafkaProducerFactory(KafkaProperties properties) {
55+
Map<String, Object> producerProperties = properties.buildProducerProperties();
56+
producerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
57+
MyProducerMetricsReporter.class);
58+
return new DefaultKafkaProducerFactory<Object, Object>(producerProperties);
59+
}
60+
61+
/**
62+
* Customized ConsumerFactory bean.
63+
* @param properties the kafka properties.
64+
* @return the bean.
65+
*/
66+
@Bean
67+
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) {
68+
Map<String, Object> consumererProperties = properties.buildConsumerProperties();
69+
consumererProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
70+
MyConsumerMetricsReporter.class);
71+
return new DefaultKafkaConsumerFactory<Object, Object>(consumererProperties);
72+
}
73+
74+
}
75+
// end::configuration[]
76+
77+
public static class MyConsumerMetricsReporter implements MetricsReporter {
78+
79+
@Override
80+
public void configure(Map<String, ?> configs) {
81+
}
82+
83+
@Override
84+
public void init(List<KafkaMetric> metrics) {
85+
}
86+
87+
@Override
88+
public void metricChange(KafkaMetric metric) {
89+
}
90+
91+
@Override
92+
public void metricRemoval(KafkaMetric metric) {
93+
}
94+
95+
@Override
96+
public void close() {
97+
}
98+
99+
}
100+
101+
public static class MyProducerMetricsReporter implements MetricsReporter {
102+
103+
@Override
104+
public void configure(Map<String, ?> configs) {
105+
}
106+
107+
@Override
108+
public void init(List<KafkaMetric> metrics) {
109+
}
110+
111+
@Override
112+
public void metricChange(KafkaMetric metric) {
113+
}
114+
115+
@Override
116+
public void metricRemoval(KafkaMetric metric) {
117+
}
118+
119+
@Override
120+
public void close() {
121+
}
122+
123+
}
124+
125+
}

0 commit comments

Comments
 (0)