依赖
可以用spring-kafka-1.1.8.RELEASE版本的
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
Java代码
public class Constant {
public static final String TOPIC_STARTUP = "topic_startup";
public static final String TOPIC_EVENT="topic_event";
}
@PostMapping("/log")
public String logger(@RequestParam("log") String log) {
//System.out.println(log);
//写到kafka 先创建kafak生产者 再调生产者send方法
sendToKafka(log);
return "666";
}
@Autowired
private KafkaTemplate template;
private void sendToKafka(String log) {
String topic = Constant.TOPIC_STARTUP;
if (log.contains("event")) {
topic = Constant.TOPIC_EVENT;
}
template.send(topic, log);
}
application.properties配置文件
#kafka的配置 想联系到kafka kafka集群地址 生产者序列化器 key-v
spring.kafka.bootstrap-servers=zjj101:9092,zjj102:9092,zjj103:9092
# key value的序列化器
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
开始测试
这里面就能推送数据了