local begin = ngx.now()
local http = require "resty.http"
local httpc = http.new()
local delay = 5
local f
f = function (premature)
if premature then
return
end
local res, err = httpc:request_uri("http://172.16.7.2:8085/api/gateway/Heartbeat", {
method = "POST",
body = "requests=true",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
}
})
local ok, err = ngx.timer.at(delay, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
end
local ok, err = ngx.timer.at(delay, f )
if not ok then
ngx.log(ngx.ERR, "failed to create the timer: ", err)
return
end
ngx.say("registered timer")
目的是调用后每5秒进行一次http请求达到心跳,并获取从server端传过来的响应。
但是经过几次成功心跳后就会出现问题。
2017/08/17 17:28:24 [error] 1701#0: *26 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/http.lua:803: bad request
stack traceback:
coroutine 0:
[C]: in function 'connect'
/usr/local/openresty/lualib/resty/http.lua:803: in function 'request_uri'
content_by_lua(nginx.conf_bak9:100):11: in function <content_by_lua(nginx.conf_bak9:100):7>, context: ngx.timer, client: 172.16.7.2, server: 0.0.0.0:8003
听说只是创建了一个resty.http对象,每请求都应该使用自己的 cosocket,但如何进行有效针对呢?