doujiang24 / lua-resty-kafka

Lua kafka client driver for the Openresty based on the cosocket API
BSD 3-Clause "New" or "Revised" License
804 stars 275 forks source link

How to make it work with proxy_pass #13

Closed githans closed 8 years ago

githans commented 8 years ago

Hi

Can this driver run on the phases other than 'content_by_lua'? If it can only run on 'content_by_lua', this will cause a conflict with proxy_pass which is also run on the 'content phase' of nginx. How to make this kafka producer work well when I have an upstream configuration? My main purpose is to output upstream_response_time to kafka.

Thank you

doujiang24 commented 8 years ago

Hello

Yes, I used the driver run on log_by_lua phase. And, seems log_by_lua is good for you too:)

githans commented 8 years ago

Hi, thank you for the reply. But there is an error when I put it in the 'log_by_lua' phase:


2015/11/04 17:40:40 [error] 10444#0: _759 failed to run log_bylua: /usr/local/openresty/lualib/resty/kafka/broker.lua:26: API disabled in the context of log_by_lua* stack traceback: [C]: in function 'tcp' /usr/local/openresty/lualib/resty/kafka/broker.lua:26: in function 'send_receive' /usr/local/openresty/lualib/resty/kafka/client.lua:139: in function '_fetch_metadata' /usr/local/openresty/lualib/resty/kafka/client.lua:200: in function 'fetch_metadata' /usr/local/openresty/lualib/resty/kafka/producer.lua:105: in function 'choose_partition' /usr/local/openresty/lualib/resty/kafka/producer.lua:347: in function 'send'


My config file is as follows:


upstream testupstream { server localhost:8080; } server { listen 80; server_name test_server; location /xxxx { proxy_pass http://testupstream; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; log_by_lua ' local cjson = require "cjson" local client = require "resty.kafka.client" local producer = require "resty.kafka.producer" local broker_list = { { host = "127.0.0.1", port = 9092 }, } -- sync producer_type local p = producer:new(broker_list) ............................ local offset, err = p:send("test", "test","test") if not offset then return end '; } } }

doujiang24 commented 8 years ago

Sorry, I forgot one thing, I used it in log_by_lua with producet_type = "async" (it is the recommended way, but not the default one) https://github.com/doujiang24/lua-resty-kafka#new-1

If you want to use sync, then you need to work with 0 delay timer, to put producer.send in to timer phase https://github.com/openresty/lua-nginx-module#ngxtimerat

githans commented 8 years ago

Thank you for the answer, issue closed.