PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

nginx roundrobin 、keepalive、ip_hash模块分析

原创
2016-07-28 08:26:55 1209浏览

首先从以下结构体开始,他也被包含在ngx_http_upstream_srv_conf_s结构中

typedefstruct {
/*typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t*cf,ngx_http_upstream_srv_conf_t *us);*/
    ngx_http_upstream_init_pt        init_upstream;

    ngx_http_upstream_init_peer_pt   init;
    void                            *data;                     
} ngx_http_upstream_peer_t

以下重点分析下两个成员变量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个

  1. us->servers数组中取得数据初始化ngx_http_upstream_rr_peers_t结构存房子us->peer.data中供init函数使用;
  2. 设置init函数的值,实现init可配置;

init

init函数对于每一个到oringin的请求执行一次;
在ngx_http_upstream_init_request中被调用,用来配置r->upstream->peer结构体,对于keepalive和ip_hash功能将配置不同的get和free函数,默认实现是ngx_http_upstream_create_round_robin_peer

struct ngx_peer_connection_s {

    ngx_connection_t                *connection;
    struct sockaddr                 *sockaddr;
    socklen_t                        socklen;
    ngx_str_t                       *name;

    ngx_uint_t                       tries;
    ngx_msec_t                       start_time;

    /*类型typedef ngx_int_t (*ngx_event_get_peer_pt)(ngx_peer_connection_t *pc, void     *data);*//* 上面init的函数中被配置,如果配置了keepalive且从cache中找到了connection将返回NGX_DONE,*//* 且设置复    用已经存在的连接,如果没有配置keepalive或者,没有找到存在的connection,*//*但找到了下一跳地址,将返回NGX_OK,*/
    ngx_event_get_peer_pt            get;

    /*,同样在init中被设置,连接释放时被调用*/
    ngx_event_free_peer_pt           free;
   /* 在init中被设置,用以保存init_upstream中生成的us->peer.data数据,在get和free方法中被使用*/void*data;
   .........
   };

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提供的方法,
如下

static ngx_int_t                                                                                                                                                         
ngx_http_upstream_init_keepalive_peer(ngx_http_request_t *r,                                                                                                             
    ngx_http_upstream_srv_conf_t *us)                                                                                                                                    
{                                                                                                                                                                        
    ngx_http_upstream_keepalive_peer_data_t  *kp;                                                                                                                        
    ngx_http_upstream_keepalive_srv_conf_t   *kcf;                                                                                                                       

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                                                                                                            
                   "init keepalive peer");                                                                                                                               

    kcf = ngx_http_conf_upstream_srv_conf(us,                                                                                                                            
                                          ngx_http_upstream_keepalive_module);                                                                                           

    kp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_keepalive_peer_data_t));                                                                                           
    if (kp ==NULL) {                                                                                                                                                    
        return NGX_ERROR;                                                                                                                                                
    }                                                                                                                                                                    

    if (kcf->original_init_peer(r, us) != NGX_OK) {                                                                                                                      
        return NGX_ERROR;                                                                                                                                                
    }                                                                                                                                                                    

    kp->conf = kcf;                                                                                                                                                      
    kp->upstream = r->upstream;                                                                                                                                          

    /*保存默认的original_get_peer方法,仍将在新的get方法中被调用,r->upstream->peer.get;可以是round_robin,也可以是iphash模块提供的 */    kp->original_get_peer = r->upstream->peer.get;                                                                                                                       
    kp->original_free_peer = r->upstream->peer.free;                                                                                                                     

    ....../*保存默认的在original_init_peer中初始化好的数据,get和free方法中需要使用*/
    kp->data= r->upstream->peer.data;

    /*可以通过r->upstream->peer.data找到keepalive模块*/
    r->upstream->peer.data= kp;   
     /*设置新的get和free钩子*/    r->upstream->peer.get = ngx_http_upstream_get_keepalive_peer;
    r->upstream->peer.free = ngx_http_upstream_free_keepalive_peer;                                                                                                                                                        
    return NGX_OK;                                                                                                                                                       
} 
static ngx_int_t
ngx_http_upstream_get_keepalive_peer(ngx_peer_connection_t *pc, void*data)
{
    ngx_http_upstream_keepalive_peer_data_t  *kp =data;
    ngx_http_upstream_keepalive_cache_t      *item;

    ngx_int_t          rc;
    ngx_queue_t       *q, *cache;
    ngx_connection_t  *c;
    /* ask balancer 此源码注释应不准确*//*适用round_robin或者ip_hash等功能找到下一跳地址*/
    rc = kp->original_get_peer(pc, kp->data);

    if (rc != NGX_OK) {
        return rc;
    }                                                                                                                                                                      

    /* search cache for suitable connection */cache=&kp->conf->cache;

可以看出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模块中。

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 nginx roundrobin 、keepalive、ip_hash模块分析,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。