gpt4 book ai didi

spring - common.KafkaException : com. fastxml.jackson.databind.ser.std.StringSerializer 不是 org.apache.kafka.common.serialization.Serializer 的实例

转载 作者:行者123 更新时间:2023-12-04 09:43:58 28 4
gpt4 key购买 nike

我正在开发 Spring Boot + Apache Kafka例子。发送时 TOPIC卡夫卡出现错误。

错误:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.demo.kafka.SpringBootKafkaJsonApplication.main(SpringBootKafkaJsonApplication.java:17) [classes/:na]
Caused by: org.apache.kafka.common.KafkaException: Failed to construct kafka producer
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:457) ~[kafka-clients-2.0.1.jar:na]
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:289) ~[kafka-clients-2.0.1.jar:na]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createKafkaProducer(DefaultKafkaProducerFactory.java:318) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createProducer(DefaultKafkaProducerFactory.java:305) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.getTheProducer(KafkaTemplate.java:437) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:367) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:184) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at com.demo.kafka.config.producer.Sender.send(Sender.java:28) ~[classes/:na]
at com.demo.kafka.SpringBootKafkaJsonApplication.run(SpringBootKafkaJsonApplication.java:22) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
... 5 common frames omitted
Caused by: org.apache.kafka.common.KafkaException: com.fasterxml.jackson.databind.ser.std.StringSerializer is not an instance of org.apache.kafka.common.serialization.Serializer
at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:304) ~[kafka-clients-2.0.1.jar:na]
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:362) ~[kafka-clients-2.0.1.jar:na]
... 14 common frames omitted

发件人.java
@Data
@NoArgsConstructor
public class Sender {

private static final Logger LOGGER = LoggerFactory.getLogger(Sender.class);

@Value("${kafka.topic-json}")
private String jsonTopic;

@Autowired
private KafkaTemplate<String, Car> kafkaTemplate;

public void send(Car car) {
LOGGER.info("sending car='{}'", car.toString());
kafkaTemplate.send(jsonTopic, car);
}
}

发件人配置文件
@Configuration
public class SenderConfig {
@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;

@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

return props;
}

@Bean
public ProducerFactory<String, Car> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}

@Bean
public KafkaTemplate<String, Car> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}

@Bean
public Sender sender() {
return new Sender();
}
}

接收器.java
public class Receiver {

private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

private CountDownLatch latch = new CountDownLatch(1);

public CountDownLatch getLatch() {
return latch;
}

@Value("${kafka.topic-json}")
private String jsonTopic;

@KafkaListener(topics = "jsont")
public void receive(Car car) {
LOGGER.info("received car='{}'", car.toString());
latch.countDown();
}
}

接收者配置文件
@Configuration
@EnableKafka
public class ReceiverConfig {

@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;

@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "json");

return props;
}

@Bean
public ConsumerFactory<String, Car> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
new JsonDeserializer<>(Car.class));
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, Car> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Car> factory =new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}

@Bean
public Receiver receiver() {
return new Receiver();
}
}

主Java
@SpringBootApplication
public class SpringBootKafkaJsonApplication implements CommandLineRunner {
@Autowired
private Sender sender;

public static void main(String[] args) {
SpringApplication.run(SpringBootKafkaJsonApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
sender.send(Car.builder().id("1").make("ALTO").manufacturer("Maruti Suzuki").build());
}
}

应用程序.yml
kafka:
topic-json: jsont
bootstrap-servers: localhost:9092

最佳答案

尝试使用

org.apache.kafka.common.serialization.StringSerializer





org.apache.kafka:kafka-clients jar



代替

com.fasterxml.jackson.databind.ser.std.StringSerializer

关于spring - common.KafkaException : com. fastxml.jackson.databind.ser.std.StringSerializer 不是 org.apache.kafka.common.serialization.Serializer 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53801565/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com