问题设想:
刚开始能通过客户端消费者消费到一些条目的数据,如果这时候日志过期时限到了,之后再通过消息代理生成了新的数据,那客户端消费者得到数据中的offset还会是在原来的基础上递增吗???
问题实践:
本来想通过改日志时间改成三分钟,去看效果的,可是kafka的server.properties文件中的log.retention.hours
貌似只能是整数
不然就是如下:
所以后来只能修改为1小时去尝试,之后等着干别的活或者修改linux的系统时间。
# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=1
//默认是168的,也就是24小时*7天,一周
问题结果/现象:
var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.KafkaClient({kafkaHost: '192.168.247.10:9092,192.168.247.11:9092,192.168.247.12:9092'}),
consumer = new Consumer(
client,
[
{ topic: 'Processing', partition: 0 }
],
{
autoCommit: false
}
);
consumer.on('message', function (message) {
console.log(message);
});
consumer.on('error', function (err) {
console.log(err);
});
consumer.on('message', function (message) {});
这里没有获取到任何信息。。。前面还以为是出了什么错误
问题分析:
1. 这是日志没过期之前
2. 这是日志过期之后,新增了两条之后日志文件的情况
所以能看出一些端倪,我人为设置过期之前,数据是有37条的,但是offset是36,offset是从0开始的。所以按照前面的从头获取消费信息,也就是从offset为0的时候开始获取,因为已经过期了,所以什么也获取不到。当改成如下代码:
var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.KafkaClient({kafkaHost: '192.168.247.10:9092,192.168.247.11:9092,192.168.247.12:9092'}),
consumer = new Consumer(
client,
[
{ topic: 'Processing', partition: 0 , offset: 37}
],
{
autoCommit: false,
fromOffset:true
}
);
consumer.on('message', function (message) {
console.log(message);
});
consumer.on('error', function (err) {
console.log(err);
});
/** 打印了如下信息 **/
{ topic: 'Processing',
value: '{"action":"message_publish","device_id":"1103262d-ff94-4609-b578-8018721e740c1553647524324","username":"mosl","topic":"/mosl","payload":"我是xxx hello hello hello 201903281353","ts":1553787263}',
offset: 37,
partition: 0,
highWaterOffset: 39,
key: null }
{ topic: 'Processing',
value: '{"action":"message_publish","device_id":"1103262d-ff94-4609-b578-8018721e740c1553647524324","username":"mosl","topic":"/mosl","payload":"我是xxx hello hello hello 201903281353","ts":1553788203}',
offset: 38,
partition: 0,
highWaterOffset: 39,
key: null }
就是说从offset为37的时候开始消费就能取到消费信息,也就是37是一个节点,区分过期前和过期后的一个节点。对应的几个日志文件的名字也体现了这点。猜想下,到了一个过期时间的到来,日志文件的名字会再次改变生成新的,对应的数字也是offset的区别过期前与后的。