nginx upstream ip-hash负载均衡的实现(请求IP hash)

nginx 请求ip hash负载均衡   ip hash简单的原理就是根据请求端的IP来生成跟IP信息相关的hash值;对于不同的请求端,由于请求IP不会相同,因此生成的hash值也会不同。   以下是ngx_http_upstream_ip_hash_module的主要结构。typedef struct { /* the round robin data must be f...
摘要由CSDN通过智能技术生成

nginx 请求ip hash负载均衡
  ip hash简单的原理就是根据请求端的IP来生成跟IP信息相关的hash值;对于不同的请求端,由于请求IP不会相同,因此生成的hash值也会不同。
  以下是ngx_http_upstream_ip_hash_module的主要结构。

typedef struct {
    /* the round robin data must be first */

    //轮询负载的数据
    ngx_http_upstream_rr_peer_data_t   rrp;
    //计算得到的hash值
    ngx_uint_t                         hash;
   //请求IP地址的长度(这里包含了ipv4和ipv6)
    u_char                             addrlen;
    //请求地址
    u_char                            *addr;
    //尝试的次数
    u_char                             tries;
    //默认的获取upstream server的函数指针
    ngx_event_get_peer_pt              get_rr_peer;
} ngx_http_upstream_ip_hash_peer_data_t

ip hash的处理流程
1.ip hash处理配置初始化

static char *
ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_upstream_srv_conf_t  *uscf;
    //得到upstream server的配置信息
    uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);

    if (uscf->peer.init_upstream) {
     /*这里判断初始化指针已经被设置过了*/
        ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                           "load balancing method redefined");
    }
   /*设置配置upstream server 的初始化函数*/
    uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash;
   /*
   设置判断的标志位信息
   */
    uscf->flags = NGX_HTTP_UPSTREAM_CREATE
                  |NGX_HTTP_UPSTREAM_WEIGHT
                  |NGX_HTTP_UPSTREAM_MAX_CONNS
                  |NGX_HTTP_UPSTREAM_MAX_FAILS
                  |NGX_HTTP_UPSTREAM_FAI
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值