最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
spring boot 与kafka集成的示例代码
时间:2018-04-26 编辑:猪哥 来源:一聚教程网
新建spring boot项目
这里使用intellij IDEA
添加kafka集成maven
4.0.0 com.example demo 0.0.1-SNAPSHOT jar demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.kafka spring-kafka org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
项目中application.properties 添加
spring.kafka.bootstrap-servers=vm208:9092,vm:9092,vm50:9092 spring.kafka.consumer.auto-offset-reset=latest spring.kafka.consumer.group-id=local_test spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.acks=1
新建KafkaConsumer消费类
package com.example.demo.consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class KafkaConsumer { private Logger logger = LoggerFactory.getLogger(this.getClass()); @KafkaListener(topics = {"test"}) public void listen(ConsumerRecord, ?> record) { System.out.printf("offset = %d,key =%s,value=%sn", record.offset(), record.key(), record.value()); } }
启动spring-boot程序,在kafka集群,模拟发送topic,检验接收
编写producer代码
package com.example.demo.producer; import org.apache.kafka.clients.producer.ProducerRecord; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; @Component public class KafkaProducer { @Autowired private KafkaTemplate kafkaTemplate; String topic="test"; public void sendMessage(String key,String data){ kafkaTemplate.send(new ProducerRecord(topic,key,data)); } }
建立一个restful模拟发送( //http://localhost:8080/kafka/send.do?key=2&data=allen-test-message)
package com.example.demo.controller; import com.example.demo.producer.KafkaProducer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class ProducerController { @Autowired private KafkaProducer kafkaProducer; @RequestMapping(value = "/kafka/send.do", method = RequestMethod.GET) public String sendMessage(@RequestParam(value = "key") String key, @RequestParam(value = "data") String data) { kafkaProducer.sendMessage(key, data); return "sucess"; } }
可以发现 spring-kafka大大减少了代码工作量.
官方文档: https://docs.spring.io/spring-kafka/docs/1.2.2.RELEASE/reference/html/
-
上一个: 详解vue 数据传递的方法
相关文章
- js实现音乐播放器代码展示 10-12
- js实现图片查看器代码展示 10-12
- JS中switch的四种写法介绍 10-12
- js实现新闻轮播效果教程 10-11
- JS实现简单的图片切换功能教程 10-11
- js实现下拉刷新和上拉加载解析 09-28