案例:
1.先在虚拟机上启动zookeeper、Kafka。并在Kafka目录下创建一个Topic(test)
2.Idea进行项目构建(创建Springboot项目)
为了更加体现实际开发需求,一般生产者都是在调用某些接口的服务处理完逻辑之后然后往kafka里面扔数据,然后有一个消费者不停的监控这个Topic,然后处理数据,所以这里把生产者作为一个接口,消费者放到kafka这个目录下。
3.具体实现代码:
1.pom.xml 所需要手动添加的依赖
dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.SpringBoot配置文件application.yml
spring:
kafka:
bootstrap-servers: hdp-1:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
consumer:
group-id: testt
enable-auto-commit: true
auto-commit-interval: 1000
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
3.producer(生产者)
package com.zpark.kafkaboot.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//RestController 作用:是返回的内容就是return内容。不能返回jsp或者html
@RestController
@RequestMapping("/kafka")
public class TestKafkaProducerController {
//@Autowired 作用:相当于new了这个对象
@Autowired
private KafkaTemplate<String,String> kafkaTemplate;
//{msg} 可在里面随便填值,所填的值会传到consumer,相当于Topic里面的数据
@RequestMapping("/send/{msg}")
public String send(@PathVariable String msg){
/**
* 在spring boot中 kafkaTemplate.send相当于maven工程里的KafkaConsumer.send
* 也就是获取topic值
*/
kafkaTemplate.send("test",msg);
return "scccess";
}
}
4.consumer(消费者)
这里的消费者会监听这个主题,有消息就会执行,不需要进行while(true)
package com.zpark.kafkaboot.kafka;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
//@Component 作用:可以扫描到 @KafkaListener注解
@Component
public class TestConsumer {
@KafkaListener(topics = "test")
//record 就是从Kafka读到了数据放入了record
public void listen(ConsumerRecord<?,?> record){
System.out.printf("topic=%s,offset=%d,value=%s \n",record.topic(),record.offset(),record.value());
}
}
5.项目启动类
package com.zpark.kafkaboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KafkabootApplication {
public static void main(String[] args) {
SpringApplication.run(KafkabootApplication.class,args);
}
}
6.进行测试
运行项目,执行localhost:8080/kafka/send/hello
控制台输出:topic = first_kafka, offset = 19, value = hello
为了体现消费者不止执行一次就结束,再调用一次接口:
localhost:8080/kafka/send/kafka
topic = first_kafka, offset = 20, value = kafka
所以可以看到这里消费者实际上是不停的poll Topic数据的。