我用这个教程尝试了基本的SpringBootKafka应用程序。
我的项目层次结构如下所示:
springbootkafkaapplication.java文件
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootKafkaApplication{
public static void main(String[] args) {
SpringApplication.run(SpringBootKafkaApplication.class, args);
}
}
Kafka控制器.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.temp.kafka_springboot_pocs.services.Producer;
@RestController
@RequestMapping(value = "/kafka")
public class KafkaController {
private final Producer producer;
@Autowired
public KafkaController(Producer producer) {
this.producer = producer;
}
@PostMapping(value = "/publish")
public void sendMessageToKafkaTopic(@RequestParam("message") String message) {
this.producer.sendMessage(message);
}
}
消费者.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class Consumer {
private final Logger logger = LoggerFactory.getLogger(Consumer.class);
@KafkaListener(topics = "kafka_poc_3", groupId = "group_id")
public void consume(String message) {
logger.info(String.format("$$ -> Consumed Message -> %s", message));
}
}
生产者.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
@Service
public class Producer {
private static final Logger logger = LoggerFactory.getLogger(Producer.class);
private static final String TOPIC = "kafka_poc_3";
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void sendMessage(String message) {
logger.info(String.format("$$ -> Producing message --> %s",message));
this.kafkaTemplate.send(TOPIC, message);
}
}
应用程序属性
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=group-1
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserialize
spring.kafka.producer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.value-deserializer=org.apache.kafka.common.serialization.StringDeserialize
spring启动应用程序无法启动,错误描述如下:
说明:
Failed to bind properties under 'spring.kafka.consumer.value-deserializer' to java.lang.Class<?>:
Property: spring.kafka.consumer.value-deserializer
Value: org.apache.kafka.common.serialization.StringDeserialize
Origin: class path resource [application.properties]:12:42
Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<?>]
我错过了什么?
附言:我在 Spring 和 Spring 工作的时间不长。所以我会喜欢有人给我好的方向/联系的例子或确切的答案。
1条答案
按热度按时间ghhkc1vu1#
试试这个属性
Kafka制作人设置
spring.kafka.producer.key serializer=org.apache.kafka.common.serialization.stringserializer spring.kafka.producer.value serializer=org.apache.kafka.common.serialization.stringserializer
Kafka消费环境
spring.kafka.consumer.key deserializer=org.apache.kafka.common.serialization.stringdeserializer spring.kafka.consumer.value deserializer=org.apache.kafka.common.serialization.stringdeserializer spring.kafka.consumer.auto offset reset=earliest