一个简单的java+kafka 消费者

上干货

1. pom

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
        </dependency>

2. runner

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class KafkaRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        new Thread(new OriginalConsumer("xxxx", "xxyy")).start();
    }
}

3. consumer

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

/**
 * 官方jar实现消费者
 */
public class OriginalConsumer implements Runnable {

    /**
     * 服务器列表,多个用 , 隔开
     */
    private static final String BROKER_LIST = "localhost:9092";

    /**
     * 客户端收到消息后,在处理消息前自动提交(即消息已消费),增加偏移量
     */
    private static final String ENABLE_AUTO_COMMIT_CONFIG = "true";

    /**
     * 自动提交的频率,1S
     */
    private static final String AUTO_COMMIT_INTERVAL_MS_CONFIG = "1000";
    private static final String TOPIC = "TEST_TOPIC";
    private KafkaConsumer<String, String> consumer;
    private String clientId;

    public OriginalConsumer(String groupId, String clientId) {
        this.clientId = clientId;
        Properties properties = new Properties();
        properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BROKER_LIST);
        properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        properties.setProperty(ConsumerConfig.CLIENT_ID_CONFIG, clientId);
        properties.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, ENABLE_AUTO_COMMIT_CONFIG);
        properties.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, AUTO_COMMIT_INTERVAL_MS_CONFIG);
        properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        consumer = new KafkaConsumer<String, String>(properties);
        consumer.subscribe(Arrays.asList(TOPIC));
    }

    private void consume() {
        // 获取消息,超时时间:1S
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(2L));
        records.forEach(record -> {
            System.out.println(clientId + "-消费消息:" + record.toString());
        });
    }

    @Override
    public void run() {
        try {
            while (true) {
                consume();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            consumer.close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值