控制kafka consumer service topic热启停的一种思路

本文介绍了一种在Spring Boot项目中控制Kafka Consumer Service Topic热启停的方法,包括设置Gradle依赖、批量消费模式配置、根据配置文件启动指定Topic、以及通过接口动态启停Topic。项目使用了Spring Boot 2.3.3.RELEASE和Spring-Kafka 2.5.5.RELEASE。
摘要由CSDN通过智能技术生成

我们在编写完成kakfa consumer service,将其部署到线上后有时会有启停某个topic的需求,而toipc在我们编写消费入口时就已经指定启停状态,本文提供一种热启停思路。
包含:

  1. 服务启动时只启动指定的topic
  2. 服务在线上运行时通过调用接口启停全部或指定的topic(借鉴es restful 控制)

项目使用spring boot 2.3.3.RELEASE ,spring-kafka 2.5.5.RELEASE

gradle依赖如下

ext.springbootVersion = '2.3.3.RELEASE'

dependencies {
   
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springbootVersion
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: springbootVersion
    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.22'
    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.31'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.1.0.RELEASE'
    compile group: 'org.springframework.kafka', name: 'spring-kafka', version: '2.5.5.RELEASE'
    compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.1.3'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: springbootVersion
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
    compile group: 'com.alibaba', name: 'druid', version: '1.1.12'
    compile group: 'com.alibaba', name: 'druid-spring-boot-starter', version: '1.1.10'
    compile group: 'net.ipip', name: 'ipdb', version: '1.1.3'
    compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
    compile group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '4.1.2'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.0.RELEASE'
}

kafka使用批量消费模式,批量工厂配置类如下,注意其中 factory.setAutoStartup(false);设置为不随spring自动启动

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.listener.ContainerProperties;

/**
 * @Author geyunpeng
 * @create 2020/9/07 17:53
 * @description kafka批量工厂配置类
 */

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值