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服务

相关推荐
程序员张32 小时前
Mybatis条件判断某属性是否等于指定字符串
java·spring boot·mybatis
invicinble3 小时前
从逻辑层面理解Shiro在JVM中是如何工作的
jvm·spring boot
好好研究6 小时前
SpringBoot注解的作用
java·spring boot·spring
Libby博仙7 小时前
Spring Boot 条件化注解深度解析
java·spring boot·后端
子非鱼9217 小时前
SpringBoot快速上手
java·spring boot·后端
我爱娃哈哈8 小时前
SpringBoot + XXL-JOB + Quartz:任务调度双引擎选型与高可用调度平台搭建
java·spring boot·后端
Coder_Boy_8 小时前
基于SpringAI的在线考试系统-AI智能化拓展
java·大数据·人工智能·spring boot
内存不泄露8 小时前
二手物品交易平台
spring boot·小程序·django
n***33358 小时前
TCP/IP协议栈深度解析技术文章大纲
java·spring boot