Kafka暂停消费--consumer.pause()

遇到了需要暂停消费的场景,使用pause()方法暂停消费,resume()方法恢复消费,基于springboot的demo如下:

注意,如果需要暂停消费的话,需要consumer 订阅 topic 的方式必须是 Assign。

assign 和 subscribe 的区别 :assign方法由用户直接手动consumer实例消费哪些具体分区,assign的consumer不会拥有kafka的group management机制,也就是当group内消费者数量变化的时候不会有reblance行为发生。assign的方法不能和subscribe方法同时使用。

pom文件



    4.0.0
    com.yqs
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-aop
            1.5.21.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter
            1.5.21.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-web
            1.5.21.RELEASE
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter-test
            1.5.21.RELEASE
            test
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-autoconfigure
            1.5.21.RELEASE
        
        
            org.springframework.boot
            spring-boot
            1.5.21.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-log4j2
            1.5.21.RELEASE
        
        
            org.springframework.kafka
            spring-kafka
            1.3.9.RELEASE
        
        
            com.alibaba
            fastjson
            1.2.73
        
        
        
            log4j
            log4j
            1.2.14
        
        
        
            org.apache.commons
            commons-lang3
            3.4
        
    
    
        

            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.6.1
                
                    1.8
                    1.8
                
            
        
    


kafka消费者 :

通过switchOn变量来手动的控制暂停跟恢复

@Component
public class KafkaPauseTest implements ApplicationListener {
    static Logger log = Logger.getLogger(KafkaPauseTest.class);
    @Autowired
    private Consumer consumer;
    TopicPartition  partition0 = new TopicPartition("test_topic", 0);

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        new Thread(()->{
            try {
                consumer.assign(Arrays.asList(new TopicPartition[]{partition0}));
                while (true) {
                    log.info("获取开关变量="+ SwitchController.switchOn);
                    if (SwitchController.switchOn == 1){
                        log.info("暂停消费开始");
                        consumer.pause(Arrays.asList(new TopicPartition[]{partition0}));
                        log.info("暂停消费结束");
                    }else {
                        log.info("恢复消费开始");
                        consumer.resume(Collections.singletonList(partition0));
                        log.info("恢复消费结束");
                    }
                    ConsumerRecords records = consumer.poll(5000);
                    log.info("poll结果结束");
                    for (ConsumerRecord record : records) {
                        log.info("topic = " + record.topic() + ", partition = " + record.partition());
                        log.info("offset = " + record.offset());
                        log.info("value = " + record.value());
                        String msg = record.value();
                    }
                    consumer.commitSync();
                }
            } finally {
                consumer.close();
            }
        }).start();
    }
}

控制类:

@RestController
public class SwitchController {

    public static volatile int switchOn = 0;

    @RequestMapping(value = "/pause", method = RequestMethod.GET)
    public String pause() {
        switchOn = 1;
        return "暂停监听";
    }

    @RequestMapping(value = "/reuse", method = RequestMethod.GET)
    public String reuse() {
        switchOn = 0;
        return "恢复监听";
    }
}

你可能感兴趣的:(Kafka暂停消费--consumer.pause())