OpenRestry +Lua + Kafka 实战代码

下载OpenRestry 

OpenResty - OpenResty 1.21.4.2 Released

下载Lua和Kafka 集成插件

https://github.com/doujiang24/lua-resty-kafka

-- 导入依赖
local producer = require "resty.kafka.producer"

local broker_list = {
         { host = "192.168.x.xxx", port = 9000 },
         { host = "192.168.x.yyy", port = 9000 },

}
-- 分区发送策略
local function partitioner(key, num, correlation_id)
	return key % num
end
-- producer_type  async 表示异步批量发送,非async 是单条发送;
local CONNECT_PARAMS = { producer_type = "async", socket_timeout = 5000,flush_time = 50,producer_retry_times = 3,partitioner =partitioner }

--发送kafka类型 1正确
local function sendkafka(p,topic, key, r,t)
    if r ~= nil then
		local offset, err = p:send(topic, key, r)
		--print("offset"..err)
		if not offset then
            -- 收集错误日志
			print("send type "..t..",and send data is "..r.." and send err:", err)
			return
		end
	else
	    print("send data is nil and type is "..t)
	end

end
-- 开始接收请求body
ngx.req.read_body()
ngx.update_time()
local nows = ngx.now()
local times,count = string.gsub(nows, "%.", "")
local strlen = string.len(times)
if strlen == 10 then 
	   times = times.."000"
	end
if strlen == 11 then 
	   times = times.."00"
	end
if strlen == 12 then 
	   times = times.."0"
	end
local topic = "mytopic"
local key = string.sub(times,-3)
local body = ngx.var.request_body


if body then
	local p = producer:new(broker_list,CONNECT_PARAMS)
	sendkafka(p,topic, key, tostring(body),1)
end

你可能感兴趣的:(openrestry和Lua,kafka,大数据,lua,kafka,开发语言,openresty)