使用lua watch consul并更新upstream列表

443 views
Skip to first unread message

Huabin Zheng

unread,
Dec 1, 2016, 12:05:17 AM12/1/16
to openresty
想用lua实现发送http请求watch consul上的服务注册信息,当注册信息有变化时watcher返回,使用根据最新的注册信息更新nginx upstream。

伪代码表示如下:

while true:
      r = send_http_request(url)
      # block until registry changed
     new_upstream = build_upstream(r)
     replace_upstream(new_upstream)

 lua-resty-http结合 lua-upstream-nginx-module看上去好像能搞定,但这个循环应该写在什么context下?

Huabin Zheng

unread,
Dec 2, 2016, 12:26:12 AM12/2/16
to openresty
我用resty.http在init_worker_by_lua中执行这个while loop,

结果报错说

resty/http.lua:100: API disabled in the context of init_worker_by_lua*

stack traceback:

        [C]: in function 'ngx_socket_tcp'

        lualib/resty/http.lua:100: in function 'new'

Kwanhur Huang

unread,
Dec 2, 2016, 10:07:28 AM12/2/16
to open...@googlegroups.com, Kwanhur Huang
https://github.com/openresty/lua-nginx-module#ngxsockettcp

context: rewrite_by_lua*, access_by_lua*, content_by_lua*, ngx.timer.*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*


-- 
-- 
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

DeJiang Zhu

unread,
Dec 3, 2016, 8:17:52 AM12/3/16
to open...@googlegroups.com
Hello

正确的做法是:
在 init_worker 创建 timer,在 timer 内使用 cosocket

在 2016年12月2日 下午1:26,Huabin Zheng <huabin...@gmail.com>写道:

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com

Huabin Zheng

unread,
Dec 4, 2016, 12:37:07 AM12/4/16
to openresty
谢谢

搞定了,参考的春哥的那个health check module。

这中间遇到了一个问题用jit 2.x后解决了。


On Saturday, December 3, 2016 at 9:17:52 PM UTC+8, doujiang wrote:
Hello

正确的做法是:
在 init_worker 创建 timer,在 timer 内使用 cosocket
在 2016年12月2日 下午1:26,Huabin Zheng <huabin...@gmail.com>写道:
我用resty.http在init_worker_by_lua中执行这个while loop,

结果报错说

resty/http.lua:100: API disabled in the context of init_worker_by_lua*

stack traceback:

        [C]: in function 'ngx_socket_tcp'

        lualib/resty/http.lua:100: in function 'new'


On Thursday, December 1, 2016 at 1:05:17 PM UTC+8, Huabin Zheng wrote:
想用lua实现发送http请求watch consul上的服务注册信息,当注册信息有变化时watcher返回,使用根据最新的注册信息更新nginx upstream。

伪代码表示如下:

while true:
      r = send_http_request(url)
      # block until registry changed
     new_upstream = build_upstream(r)
     replace_upstream(new_upstream)

 lua-resty-http结合 lua-upstream-nginx-module看上去好像能搞定,但这个循环应该写在什么context下?

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

Hadals Yang

unread,
Dec 28, 2016, 4:36:52 AM12/28/16
to openresty
你好,我也在做类似的事情,我想咨询一下你们的做法。

1.你们不同项目的不同upstream注册到consul的时候是怎么组织关系的啊?
比如一个server 一个upstream吗?

2.watch ,部分具体是怎么检测的。
比如,watch 某个server 吗?

谢谢你

在 2016年12月1日星期四 UTC+8下午1:05:17,Huabin Zheng写道:

Ming

unread,
Dec 28, 2016, 8:32:15 PM12/28/16
to open...@googlegroups.com
参考下又拍的这个开源项目,或者你可以直接使用它:https://github.com/upyun/slardar

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
Reply all
Reply to author
Forward
0 new messages