Maison >développement back-end >tutoriel php >nginx roundrobin 、keepalive、ip_hash模块分析
首先从以下结构体开始,他也被包含在ngx_http_upstream_srv_conf_s结构中
<code><span>typedef</span><span>struct</span> { <span>/*typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t*cf,ngx_http_upstream_srv_conf_t *us);*/</span> ngx_http_upstream_init_pt init_upstream; ngx_http_upstream_init_peer_pt init; <span>void</span> *data; } ngx_http_upstream_peer_t</code>
以下重点分析下两个成员变量init_upstream和init
init_upstream
每个upsteam配置块块执行一次;
可通过配置keepalive或者ip_hash功能配置,即碰到keepalive或者ip_hash配置被调用,如果没有配置默认实现为ngx_http_upstream_init_round_robin函数,在配置初始化函数ngx_http_upstream_init_main_conf被调用,默认设置为主要功能是有2个
init
init函数对于每一个到oringin的请求执行一次;
在ngx_http_upstream_init_request中被调用,用来配置r->upstream->peer结构体,对于keepalive和ip_hash功能将配置不同的get和free函数,默认实现是ngx_http_upstream_create_round_robin_peer
<code>struct ngx_peer_connection_s { ngx_connection_t <span>*</span>connection; struct sockaddr <span>*</span>sockaddr; socklen_t socklen; ngx_str_t <span>*</span>name; ngx_uint_t tries; ngx_msec_t start_time; <span>/*类型typedef ngx_int_t (*ngx_event_get_peer_pt)(ngx_peer_connection_t *pc, void *data);*/</span><span>/* 上面init的函数中被配置,如果配置了keepalive且从cache中找到了connection将返回NGX_DONE,*/</span><span>/* 且设置复 用已经存在的连接,如果没有配置keepalive或者,没有找到存在的connection,*/</span><span>/*但找到了下一跳地址,将返回NGX_OK,*/</span> ngx_event_get_peer_pt get; <span>/*,同样在init中被设置,连接释放时被调用*/</span> ngx_event_free_peer_pt free; <span>/* 在init中被设置,用以保存init_upstream中生成的us->peer.data数据,在get和free方法中被使用*/</span><span>void</span><span>*</span><span>data</span>; <span>...</span><span>...</span><span>...</span> };</code>
init函数初始化的数据将在为rr创建连接是被使用,调用过程如下:
ngx_http_upstream_init_request->ngx_http_upstream_connect->ngx_event_connect_peer->
rc = pc->get(pc, pc->data);
round_robin ,ip_hash,keepalive模块之间关系
round_robin和ip_hash模块都用于查找吓一跳ip,keepalive功能用于找到吓一跳ip地址之后,通过ip地址查找是否已经存在可用的connection,所以两个模块的get实现方式不同,ip_hash是现使用ip_hash查找下一下,查找失败后采用默认的round_robin功能,
而keepalive功能是在ip查找到之后才能使用,ip查找可以使用ip_hash的方法,也可以使用round_robin提供的方法,
如下
<code>static ngx_int_t ngx_http_upstream_init_keepalive_peer(ngx_http_request_t <span>*</span>r, ngx_http_upstream_srv_conf_t <span>*</span>us) { ngx_http_upstream_keepalive_peer_data_t <span>*</span>kp; ngx_http_upstream_keepalive_srv_conf_t <span>*</span>kcf; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r<span>-></span>connection<span>-></span><span>log</span>, <span>0</span>, <span>"init keepalive peer"</span>); kcf <span>=</span> ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_keepalive_module); kp <span>=</span> ngx_palloc(r<span>-></span>pool, sizeof(ngx_http_upstream_keepalive_peer_data_t)); <span>if</span> (kp <span>==</span><span>NULL</span>) { <span>return</span> NGX_ERROR; } <span>if</span> (kcf<span>-></span>original_init_peer(r, us) <span>!=</span> NGX_OK) { <span>return</span> NGX_ERROR; } kp<span>-></span>conf <span>=</span> kcf; kp<span>-></span>upstream <span>=</span> r<span>-></span>upstream; <span>/*保存默认的original_get_peer方法,仍将在新的get方法中被调用,r->upstream->peer.get;可以是round_robin,也可以是iphash模块提供的 */</span> kp<span>-></span>original_get_peer <span>=</span> r<span>-></span>upstream<span>-></span>peer<span>.</span>get; kp<span>-></span>original_free_peer <span>=</span> r<span>-></span>upstream<span>-></span>peer<span>.</span>free; <span>...</span><span>...</span><span>/*保存默认的在original_init_peer中初始化好的数据,get和free方法中需要使用*/</span> kp<span>-></span><span>data</span><span>=</span> r<span>-></span>upstream<span>-></span>peer<span>.</span><span>data</span>; <span>/*可以通过r->upstream->peer.data找到keepalive模块*/</span> r<span>-></span>upstream<span>-></span>peer<span>.</span><span>data</span><span>=</span> kp; <span>/*设置新的get和free钩子*/</span> r<span>-></span>upstream<span>-></span>peer<span>.</span>get <span>=</span> ngx_http_upstream_get_keepalive_peer; r<span>-></span>upstream<span>-></span>peer<span>.</span>free <span>=</span> ngx_http_upstream_free_keepalive_peer; <span>return</span> NGX_OK; } static ngx_int_t ngx_http_upstream_get_keepalive_peer(ngx_peer_connection_t <span>*</span>pc, <span>void</span><span>*</span><span>data</span>) { ngx_http_upstream_keepalive_peer_data_t <span>*</span>kp <span>=</span><span>data</span>; ngx_http_upstream_keepalive_cache_t <span>*</span>item; ngx_int_t rc; ngx_queue_t <span>*</span>q, <span>*</span><span>cache</span>; ngx_connection_t <span>*</span>c; <span>/* ask balancer 此源码注释应不准确*/</span><span>/*适用round_robin或者ip_hash等功能找到下一跳地址*/</span> rc <span>=</span> kp<span>-></span>original_get_peer(pc, kp<span>-></span><span>data</span>); <span>if</span> (rc <span>!=</span> NGX_OK) { <span>return</span> rc; } <span>/* search cache for suitable connection */</span><span>cache</span><span>=</span><span>&</span>kp<span>-></span>conf<span>-></span><span>cache</span>;</code>
可以看出keepalive类似于装饰器模型,即在下一跳ip上增加一个keepalive的功能,keepalive包含origin实现的函数指针
而ip_hash和round_robin 能类似于策略模型,即在init_upstream中设置对于request查找ip的不同策略。
而这三个模块整体关系,类似于建造者之间关系,即都必须实现且按顺序调用init_upstream,init,get,free等函数。
后记:
round_robin多提供了一个create函数,用于upstream中resolved成员被设置的情况,例如通过
proxy_pass http://$vhost;的方式设置到origin的地址。 此时init被create代替,且不能在具有keepalive和ip_hash功能。keepalive和iphash都只能用于upstream模块中。
以上就介绍了 nginx roundrobin 、keepalive、ip_hash模块分析,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。