1.引入库
<!-- kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.8.2</version>
</dependency>
如果启动报错:Error creating bean with name ‘kafkaTemplate‘ defined in class path resource 那就是springboot版本和kafka不兼容 去找对应版本兼容
2.配置文件
代码如下(示例):
spring.kafka.bootstrap-servers=xxx:9092,xxx:9092,xxx:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
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.group-id=model
示例代码
@Component
public class KafkaTest {
@KafkaListener(topics = "model") //topic的名称
public void kafkaConsume(String content){
System.out.println("kafka消息消费:"+content);
}
}
|