获取 Magic v1 在生成消息时不支持记录标题,在我的代码下方,
KafkaProducerConfig:
@Configuration
public class KafkaProducerConfig {
@Value(value = "${kafka.bootstrap-servers}")
private String bootstrapAddress;
@Bean
public ProducerFactory<String, Event> producerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
config.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return new DefaultKafkaProducerFactory<>(config);
}
@Bean
public KafkaTemplate<String, Event> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
}
KafkaConsumerConfig:
@EnableKafka
@Configuration
public class KafkaConsumerConfig {
@Value(value = "${spring.kafka.bootstrap-servers}")
private String bootstrapAddress;
@Value(value = "${spring.kafka.consumer.group-id}")
private String groupId;
@Value(value = "${kafka.consumer.enable.auto.commit}")
private String autoCommit;
@Value(value = "${kafka.consumer.auto.commit.interval.ms}")
private String autoCommitInterval;
@Value(value = "${kafka.consumer.auto.offset.reset}")
private String autoOffsetReset;
@Value(value = "${kafka.consumer.session.timeout.ms}")
private String sessionTimeout;
@Value(value = "${kafka.consumer.concurrency}")
private String concurrency;
@Value(value = "${kafka.consumer.pollTimeout}")
private String pollTimeout;
@Bean
public ConsumerFactory<String, Event> consumerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
config.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, autoCommit);
config.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
config.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
return new DefaultKafkaConsumerFactory<>(config, new StringDeserializer(),
new JsonDeserializer<>(Event.class));
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Event> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Event> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setConcurrency(Integer.parseInt(concurrency));
factory.getContainerProperties().setPollTimeout(Integer.parseInt(pollTimeout));
return factory;
}
}
KafkaTopicConfig:
@Configuration
public class KafkaTopicConfig {
@Value(value = "${spring.kafka.bootstrap-servers}")
private String bootstrapAddress;
@Value(value = "${kafka.topicName}")
private String topicName;
@Value(value = "${kafka.topic.partitions}")
private String partitions;
@Value(value = "${kafka.topic.replicationFactor}")
private String replicationFactor;
@Bean
public KafkaAdmin kafkaAdmin() {
Map<String, Object> configs = new HashMap<>();
configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
return new KafkaAdmin(configs);
}
@Bean
public NewTopic ClientTopic() {
return new NewTopic(topicName, Integer.parseInt(partitions), (short) Short.parseShort(replicationFactor));
}
}
产生消息:产生消息时出错
kafkaTemplate.send(topicName, event);
消费信息:
@KafkaListener(topics = "someTopicName", groupId = "somegroupId")
public void consume(Event event) {
// here Business logic
}
我正在使用的Gradle依存关系
implementation ('org.springframework.kafka:spring-kafka')
implementation('com.fasterxml.jackson.core:jackson-databind:2.9.4')
我正在使用的Spring Boot版本:
springBootVersion = '2.0.3.RELEASE'
请让我知道我在做什么错
我尝试在生产者工厂中添加,但是没有用,
config.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);