nginx负载均衡策略分析[一](round_robin模块概要)

来源:互联网 发布:单代号搭接网络 编辑:程序博客网 时间:2024/05/17 22:19

1.基本情况

模块名:round_robin

文件位置:ngx_http_upstream_round_robin.c

运行阶段:content_phase

RR策略做为默认策略

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
例如:
 upstream tomcats {
 server 10.1.1.107:88  max_fails=3 fail_timeout=3s weight=9;
 server 10.1.1.132:80  max_fails=3 fail_timeout=3s weight=9;
 }

上游调用:Ngx_http_upstream.c Ngx_http_upstream_ip_hash_module.c
功能:nginx在用作反向代理服务器时,对于后端的服务器可以采用两种分流策略:加权分流和ip hash。本模块主要完成加权分流功能。对于权重较高的机器,被选中的概率大,对于权重相同的机器,则采用轮询方式。
亮点:
1、在数据结构中设置了single字段,判断是否是只有一台后端服务器。如果是,则无需走分流策略模块,直接返回即可;
2、同时设置了失败次数和失败时间的上限。如果达到了失败次数上限,在一段时间内该server不参与分流。
3、backup服务器的使用。只有在现有server都无效时,才会请求backup服务器


2.关键数据结构

typedef struct {//基本socket信息    struct sockaddr                *sockaddr;    socklen_t                       socklen;    ngx_str_t                       name;//当前权重值和设定权重值    ngx_int_t                       current_weight;    ngx_int_t                       weight;//失败次数和访问时间    ngx_uint_t                      fails;    time_t                          accessed;//失败次数上限值和失败时间阈值    ngx_uint_t                      max_fails;    time_t                          fail_timeout;//服务器是否参与策略    ngx_uint_t                      down;          /* unsigned  down:1; *///ssl相关#if (NGX_HTTP_SSL)    ngx_ssl_session_t              *ssl_session;   /* local to a process */#endif} ngx_http_upstream_rr_peer_t; //后台服务器的具体信息//round robin后端服务器信息typedef struct ngx_http_upstream_rr_peers_s  ngx_http_upstream_rr_peers_t;struct ngx_http_upstream_rr_peers_s {    ngx_uint_t                      single;             //该群是否只有一台服务器    ngx_uint_t                      number;             //该群后端服务器数量    ngx_uint_t                      last_cached; /* ngx_mutex_t                    *mutex; */    ngx_connection_t              **cached;    ngx_str_t                      *name;    ngx_http_upstream_rr_peers_t   *next;               //下个upstream节点,即下一个服务器群的指针,next 一般指向backupserver(备份服务器群)    ngx_http_upstream_rr_peer_t     peer[1];            //服务器群中的第一个服务器,如果还有其它的服务器,则会连续申请其它的peer};typedef struct {    ngx_http_upstream_rr_peers_t   *peers;              //所有服务器群的指针    ngx_uint_t                      current;            //当前服务器    uintptr_t                      *tried;              //服务器位图指针,用于记录服务器当前的状态    uintptr_t                       data;               //tried位图实际存储位置} ngx_http_upstream_rr_peer_data_t;

3.函数功能说明

主要的函数列表如下

ngx_int_t   ngx_http_upstream_init_round_robin(ngx_conf_t *cf,    ngx_http_upstream_srv_conf_t *us)ngx_int_t   ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r,    ngx_http_upstream_srv_conf_t *us)ngx_int_t   ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,    ngx_http_upstream_resolved_t *ur)ngx_int_t   ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)static ngx_http_upstream_rr_peer_t *   ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)void   ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data,    ngx_uint_t state)






参考资料:

nginx round_robin模块剖析

开源中国ngx_http_upstream_round_robin.c



原创粉丝点击