springboot集成kafka

1、引入依赖

bash 复制代码
      <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.8.6</version>
        </dependency>

2、配置

bash 复制代码
server:
  port: 9099
spring:
  kafka:
    bootstrap-servers: 192.168.157.101:9092
    consumer:
      group-id: test-consumer-group
      max-poll-records: 10
      concurrency: 10
      #Kafka中没有初始偏移或如果当前偏移在服务器上不再存在时,默认区最新 ,有三个选项 【latest, earliest, none】
      auto-offset-reset: earliest
      #是否开启自动提交
      enable-auto-commit: false
      ack-mode: MANUAL_IMMEDIATE
      #自动提交的时间间隔
      auto-commit-interval: 1000
      #key的解码方式
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      #value的解码方式
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      properties:
        batch-listener: true

    producer:
      batch-size: 4096
      buffer-memory: 40960
      retries: 1
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
    listener:
      #创建多少个consumer,值必须小于等于Kafk Topic的分区数。
      ack-mode: MANUAL_IMMEDIATE
      concurrency: 1  #推荐设置为topic的分区数

3、测试

java 复制代码
    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;
    @Test
    void contextLoads() {
        for (int i = 0; i < 100; i++) {
            kafkaTemplate.send("hello","hello"+i);
        }

    }

如日志打印:Connection to node 0 (/127.0.0.1:9092)

bash 复制代码
#进入kafka安装目录
/usr/local/kafka_2.12-3.5.1/config
#编辑配置文件
vim server.properties
    
# 允许外部端口连接                                            
listeners=PLAINTEXT://0.0.0.0:9092  
# 外部代理地址                                                
advertised.listeners=PLAINTEXT://192.168.157.101:9092  

重启kafka服务

相关推荐
记得开心一点嘛18 小时前
手搓Springboot
java·spring boot·spring
free1 天前
基于librdkafa C++客户端生产者发送数据失败问题处理#2
c++·kafka
不吃饭的猪1 天前
kafka启动小脚本
分布式·kafka
shallwe小威1 天前
SpringBoot集成ElasticSearch
数据库·spring boot·elasticsearch
Q_Q19632884751 天前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
AD钙奶-lalala1 天前
Spring Initializr(或者 IDEA 里新建 Spring Boot 项目)时 Dependencies 的选择
spring boot·spring·intellij-idea
风槐啊1 天前
邪修实战系列(3)
java·ide·spring boot·spring·tomcat
毕设源码-朱学姐1 天前
【开题答辩全过程】以 _基于SpringBoot技术的“树洞”心理咨询服务平台的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端