kafka与springboot集成-2
一、加入依赖(springboot的其他依赖这里展示了,版本使用的是2.0.4.RELEASE)
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
二、加入配置
spring: kafka: # 地址 bootstrap-servers: 192.168.5.100:9092,192.168.5.100:9093 producer: # 0 发送出去即成功 1 发送出去等待leader落盘后,认为成功 -1 认为在ISR集群中leader和follower同步完成为成功 acks: 1 # 重试次数 retries: 3 # kafka一次拉取的缓存数据量默认16KB batch-size: 16384 # 本地缓存大小默认32M buffer-memory: 33554432 # 序列化 key-serializer: org.apache.kafka.common.serialization.BytesSerializer value-serializer: org.apache.kafka.common.serialization.BytesSerializer consumer: # 默认组 group-id: kafka # 自动提交一般关闭 enable-auto-commit: false # 一次性拉取数量 max-poll-records: 500 # 反序列化 key-deserializer: org.apache.kafka.common.serialization.BytesDeserializer value-deserializer: org.apache.kafka.common.serialization.BytesDeserializer listener: # 确认模式 ack-mode: manual_immediate
三、编码
1、topic配置
@Configuration public class TopicConfiguration { @Bean public NewTopic log() { return new NewTopic("log", 4, (short) 1); } @Bean public NewTopic data() { return new NewTopic("data", 4, (short) 1); } }
2、生产者
@Component public class Producer { @Autowired private KafkaTemplate<Bytes, Bytes> kafkaTemplate; private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Scheduled(cron = "0 0/1 * * * ?") public void send() { String now = formatter.format(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())); kafkaTemplate.send("log", Bytes.wrap("log".getBytes()), Bytes.wrap(now.getBytes())); kafkaTemplate.send("data", Bytes.wrap("data".getBytes()), Bytes.wrap(now.getBytes())); } }
3、消费者
@Component public class Consumer { @KafkaListener(topics = "log") public void log(ConsumerRecord<Bytes, Bytes> record, Acknowledgment ack) { System.out.println(MessageFormat.format("log key {0}, value {1}", record.key(), record.value())); ack.acknowledge(); } @KafkaListener(topics = "data") public void data(ConsumerRecord<Bytes, Bytes> record, Acknowledgment ack) { System.out.println(MessageFormat.format("data key {0}, value {1}", record.key(), record.value())); ack.acknowledge(); } }
4、测试: