Hello!
2013/4/12 曾鋆:
>
> 我是您openresty的一名粉丝。使用很久了,感觉特别好!
谢谢支持!
> 想跟您请教一个问题:
> 我们最近业务上有需求,想nginx进程启动的时候,去zk上建立一个ZOO_EPHEMERAL类型的节点。
> 经过阅读tengine的教程和您博客上的教程,我写了一个。基本定义如下:
>
> typedef struct
> {
> ngx_str_t ecdata;
> }ngx_zookeeper_main_conf_t;
>
> static ngx_command_t ngx_zookeeper_commands[] = {
> { ngx_string("zk_server"),
> NGX_ANY_CONF|NGX_CONF_TAKE1,
> ngx_zookeeper_readconf,
> NGX_HTTP_MAIN_CONF_OFFSET,
> offsetof(ngx_zookeeper_main_conf_t, ecdata),
> NULL },
> ngx_null_command
> };
>
> static ngx_http_module_t ngx_zookeeper_module_ctx = {
> NULL, /* preconfiguration */
> NULL, /* postconfiguration */
>
> ngx_zookeeper_create_main_conf,/* create main configuration */
> ngx_zookeeper_init_main_conf, /* init main configuration */
>
> NULL, /* create server configuration */
> NULL, /* merge server configuration */
>
> NULL, /* create location configuration */
> NULL
> };
>
>
> ngx_module_t ngx_module_zookeeper = {
> NGX_MODULE_V1,
> &ngx_zookeeper_module_ctx, /* module context */
> ngx_zookeeper_commands, /* module directives */
> NGX_HTTP_MODULE, /* module type */
> NULL, /* init master */
> NULL, /* init module */
> NULL, /* init process */
> NULL, /* init thread */
> NULL, /* exit thread */
> NULL, /* exit process */
> NULL, /* exit master */
> NGX_MODULE_V1_PADDING
> };
>
> 我将zk_server 10.9.18.14:2181; 放在了http块中,发现起效果然又退出了。
> 经分析,貌似是因为nginx重新fork的原因,如果我在nginx.conf中设置为
>
你的 zookeeper 连接究竟用于什么目的?你在 init_main_conf 回调里创建这个连接其实是在 master 进程里创建连接。
我觉得你应该在你的 ngx_module_zookeeper 全局变量中注册自己的 init_process
handler,并在那里建立你的后端连接。这个回调会在 worker 进程创建时调用。
在 nginx HUP reload 的时候,旧的 worker 进程都会退出,新的 worker 进程会重新 fork 出来。
> master_process off;
> daemon off;
>
> 就可以一直保持连接。
>
这种配置只是用于开发方便,你肯定不会希望在生产上使用的。没有 master 自然就没有多个 worker 进程了,更没有 fork 了,同时
HUP reload 也会有问题。
同时抄送给 openresty 中文邮件列表:https://groups.google.com/group/openresty
我建议你加入此邮件并从此在那里交流这样的问题,谢谢合作!
Best regards,
-agentzh