Spring 引导自动配置加载失败 Spring Kafka 属性
Spring Boot Auto Configuration Failed Loading Spring Kafka Properties
Spring 引导加载属性失败。这是我通过 yaml 文件使用的属性。
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
auto-commit-interval: 100
enable-auto-commit: true
group-id: ********************
auto-offset-reset: earliest
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
producer:
batch-size: 16384
buffer-memory: 33554432
retries: 0
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
listener:
poll-timeout: 20000
我得到的异常是这个
原因:java.lang.IllegalAccessException:Class org.apache.kafka.common.utils.Utils 无法使用修饰符 [=23] 访问 class org.springframework.kafka.support.serializer.JsonDeserializer 的成员=]
我认为构造函数是受保护的。请提供一种实例化它的方法。
没错。参见:
protected JsonDeserializer() {
this((Class<T>) null);
}
protected JsonDeserializer(ObjectMapper objectMapper) {
this(null, objectMapper);
}
public JsonDeserializer(Class<T> targetType) {
this(targetType, new ObjectMapper());
this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
JsonDeserializer
未设计为由默认构造函数实例化,因为它需要知道 targetType
才能反序列化。
您可以将此 class 扩展到您的特定类型:
public class FooJsonDeserializer extends JsonDeserializer<Foo> { }
并已将此用作 class 值 value-deserializer
属性。
或者你可以考虑自定义DefaultKafkaConsumerFactory
:
@Bean
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) {
Map<String, Object> consumerProperties = properties.buildConsumerProperties();
consumerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
MyConsumerMetricsReporter.class);
DefaultKafkaConsumerFactory<Object, Object> consumerFactory =
new DefaultKafkaConsumerFactory<>(consumerProperties);
consumerFactory.setValueDeserializer(new JsonDeserializer<>(Foo.class));
return consumerFactory;
}
Spring 引导加载属性失败。这是我通过 yaml 文件使用的属性。
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
auto-commit-interval: 100
enable-auto-commit: true
group-id: ********************
auto-offset-reset: earliest
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
producer:
batch-size: 16384
buffer-memory: 33554432
retries: 0
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
listener:
poll-timeout: 20000
我得到的异常是这个
原因:java.lang.IllegalAccessException:Class org.apache.kafka.common.utils.Utils 无法使用修饰符 [=23] 访问 class org.springframework.kafka.support.serializer.JsonDeserializer 的成员=]
我认为构造函数是受保护的。请提供一种实例化它的方法。
没错。参见:
protected JsonDeserializer() {
this((Class<T>) null);
}
protected JsonDeserializer(ObjectMapper objectMapper) {
this(null, objectMapper);
}
public JsonDeserializer(Class<T> targetType) {
this(targetType, new ObjectMapper());
this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
JsonDeserializer
未设计为由默认构造函数实例化,因为它需要知道 targetType
才能反序列化。
您可以将此 class 扩展到您的特定类型:
public class FooJsonDeserializer extends JsonDeserializer<Foo> { }
并已将此用作 class 值 value-deserializer
属性。
或者你可以考虑自定义DefaultKafkaConsumerFactory
:
@Bean
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) {
Map<String, Object> consumerProperties = properties.buildConsumerProperties();
consumerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
MyConsumerMetricsReporter.class);
DefaultKafkaConsumerFactory<Object, Object> consumerFactory =
new DefaultKafkaConsumerFactory<>(consumerProperties);
consumerFactory.setValueDeserializer(new JsonDeserializer<>(Foo.class));
return consumerFactory;
}