Add support for arbitrary producer/consumer Kafka properties
PR #7672 Added support for arbitrary common properties. However, Kafka emits a warning if a producer configuration contains properties intended only for consumers, and vice versa. The documentation showed a sample of how to write code to configure arbitrary properties but this is inconvenient. Add arbitrary properties to the consumer and procucer configs. See gh-9775
This commit is contained in:
committed by
Stephane Nicoll
parent
43e1df7752
commit
191752d2c0
@@ -968,6 +968,7 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.kafka.consumer.heartbeat-interval= # Expected time in milliseconds between heartbeats to the consumer coordinator.
|
||||
spring.kafka.consumer.key-deserializer= # Deserializer class for keys.
|
||||
spring.kafka.consumer.max-poll-records= # Maximum number of records returned in a single call to poll().
|
||||
spring.kafka.consumer.properties.*= # Additional properties used to configure the client.
|
||||
spring.kafka.consumer.ssl.key-password= # Password of the private key in the key store file.
|
||||
spring.kafka.consumer.ssl.keystore-location= # Location of the key store file.
|
||||
spring.kafka.consumer.ssl.keystore-password= # Store password for the key store file.
|
||||
@@ -991,6 +992,7 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.kafka.producer.client-id= # Id to pass to the server when making requests; used for server-side logging.
|
||||
spring.kafka.producer.compression-type= # Compression type for all data generated by the producer.
|
||||
spring.kafka.producer.key-serializer= # Serializer class for keys.
|
||||
spring.kafka.producer.properties.*= # Additional properties used to configure the client.
|
||||
spring.kafka.producer.retries= # When greater than zero, enables retrying of failed sends.
|
||||
spring.kafka.producer.ssl.key-password= # Password of the private key in the key store file.
|
||||
spring.kafka.producer.ssl.keystore-location= # Location of the key store file.
|
||||
@@ -998,7 +1000,7 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.kafka.producer.ssl.truststore-location= # Location of the trust store file.
|
||||
spring.kafka.producer.ssl.truststore-password= # Store password for the trust store file.
|
||||
spring.kafka.producer.value-serializer= # Serializer class for values.
|
||||
spring.kafka.properties.*= # Additional properties used to configure the client.
|
||||
spring.kafka.properties.*= # Additional properties, common to producers and consumers, used to configure the client.
|
||||
spring.kafka.ssl.key-password= # Password of the private key in the key store file.
|
||||
spring.kafka.ssl.keystore-location= # Location of the key store file.
|
||||
spring.kafka.ssl.keystore-password= # Store password for the key store file.
|
||||
|
||||
@@ -5117,20 +5117,13 @@ are not directly supported, use the following:
|
||||
[source,properties,indent=0]
|
||||
----
|
||||
spring.kafka.properties.foo.bar=baz
|
||||
spring.kafka.consumer.properties.fiz.buz=qux
|
||||
spring,kafka.producer.properties.baz.qux=fiz
|
||||
----
|
||||
|
||||
This sets the common `foo.bar` Kafka property to `baz`.
|
||||
|
||||
These properties will be shared by both the consumer and producer factory beans.
|
||||
If you wish to customize these components with different properties, such as to use a
|
||||
different metrics reader for each, you can override the bean definitions, as follows:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
include::{code-examples}/kafka/KafkaSpecialProducerConsumerConfigExample.java[tag=configuration]
|
||||
----
|
||||
|
||||
This sets the common `foo.bar` Kafka property to `baz` (applies to both producers and consumers), the consumer `fiz.buz` property to `qux` and the `baz.qux` producer property to `fiz`.
|
||||
|
||||
IMPORTANT: Properties set in this way will override properties that are in the subset that boot explicitly supports.
|
||||
|
||||
[[boot-features-resttemplate]]
|
||||
== Calling REST services with '`RestTemplate`'
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.kafka;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.kafka.clients.CommonClientConfigs;
|
||||
import org.apache.kafka.common.metrics.KafkaMetric;
|
||||
import org.apache.kafka.common.metrics.MetricsReporter;
|
||||
|
||||
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.kafka.core.ConsumerFactory;
|
||||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
|
||||
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
||||
import org.springframework.kafka.core.ProducerFactory;
|
||||
|
||||
/**
|
||||
* Example custom kafka configuration beans used when the user wants to apply different
|
||||
* common properties to the producer and consumer.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 1.5
|
||||
*/
|
||||
public class KafkaSpecialProducerConsumerConfigExample {
|
||||
|
||||
// tag::configuration[]
|
||||
@Configuration
|
||||
public static class CustomKafkaBeans {
|
||||
|
||||
/**
|
||||
* Customized ProducerFactory bean.
|
||||
* @param properties the kafka properties.
|
||||
* @return the bean.
|
||||
*/
|
||||
@Bean
|
||||
public ProducerFactory<?, ?> kafkaProducerFactory(KafkaProperties properties) {
|
||||
Map<String, Object> producerProperties = properties.buildProducerProperties();
|
||||
producerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
|
||||
MyProducerMetricsReporter.class);
|
||||
return new DefaultKafkaProducerFactory<>(producerProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Customized ConsumerFactory bean.
|
||||
* @param properties the kafka properties.
|
||||
* @return the bean.
|
||||
*/
|
||||
@Bean
|
||||
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) {
|
||||
Map<String, Object> consumerProperties = properties.buildConsumerProperties();
|
||||
consumerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
|
||||
MyConsumerMetricsReporter.class);
|
||||
return new DefaultKafkaConsumerFactory<>(consumerProperties);
|
||||
}
|
||||
|
||||
}
|
||||
// end::configuration[]
|
||||
|
||||
public static class MyConsumerMetricsReporter implements MetricsReporter {
|
||||
|
||||
@Override
|
||||
public void configure(Map<String, ?> configs) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(List<KafkaMetric> metrics) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void metricChange(KafkaMetric metric) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void metricRemoval(KafkaMetric metric) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class MyProducerMetricsReporter implements MetricsReporter {
|
||||
|
||||
@Override
|
||||
public void configure(Map<String, ?> configs) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(List<KafkaMetric> metrics) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void metricChange(KafkaMetric metric) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void metricRemoval(KafkaMetric metric) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user