Kafka-Timestamp-and-Nginx-Udp-Proxy

昨天遇到一个kafka lib的问题, 明明kafka版本是1.0, 发消息的时候也带上时间戳了, 但接收的时候就是看不到时间戳, timestamp为-1.

如果你也是用的sarama, 记着要在配置的时候制定kafka版本,不然就默认用最小的版本了, 而最小版本(0.8)是没有时间戳功能的, 所以即使后台的kafka是最新的1.0, 那也收不到timestamp, 因为sarama发的时候就没带, 协议版本里没有.

解决方法是, 指定kafka的版本:

config := sarama.NewConfig()
config.Version = sarama.V1_0_0_0 // HERE!
// sarama.WaitForLocal sarama.WaitForAll sarama.NoResponse
config.Producer.RequiredAcks = conf.AckMode

还需要注意的一点是:

// Timestamp is the timestamp assigned to the message by the broker. This
// is only guaranteed to be defined if the message was successfully
// delivered, RequiredAcks is not NoResponse, and the Kafka broker is at
// least version 0.10.0.
Timestamp time.Time

ProducerMessage.Timestamp

所以, config.Producer.RequiredAcks 不能是 NoResponse.

切记切记.


另外还有一个Nginx UDP代理的问题, 一并记录在此.

现象是, 发的UDP太猛了, nginx处理不过来, 解决方法这个大哥也说了:

http://blog.iany.me/zh/2017/08/nginx-udp-load-balance/

核心有两点:

  1. 关闭UDP proxy的response, 减少端口占用. 不然的话每来一个UDP包都会开一个新的端口去处理(tcpdump抓到的)
  2. 增加worker连接数. 在我这边测的是, 40000个连接占用了411M的内存, 大约一个连接10k
  3. 提高 worker_rlimit_nofile 限制, 增加每个worker进程可以打开的最大文件数量.
server{
	listen 10001 udp;
	proxy_pass syslog;
	proxy_responses 0;
}
worker_rlimit_nofile 1000000;

events {
    worker_connections 40000;
}

一些说明: http://nginx.org/en/docs/ngx_core_module.html

https://www.nginx.com/blog/tcp-load-balancing-udp-load-balancing-nginx-tips-tricks/#udpLB

comments powered by Disqus