地址:http://wiki.nginx.org/HttpUpstreamConsistentHash

首先声明一个命令:

static ngx_command_t  ngx_http_upstream_consistent_hash_commands[] = {

{   ngx_string("consistent_hash"),
        NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
        ngx_http_upstream_consistent_hash,
        0,
        0,
        NULL },

ngx_null_command
};

看下命令的处理函数:ngx_http_upstream_consistent_hash

ngx_str_t                                    *value;
    ngx_http_script_compile_t                     sc;
    ngx_http_upstream_srv_conf_t                 *uscf;
    ngx_http_upstream_consistent_hash_srv_conf_t *uchscf;

value = cf->args->elts;

uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
    uchscf = ngx_http_conf_upstream_srv_conf(uscf,
                                          ngx_http_upstream_consistent_hash_module);

ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));

sc.cf = cf;
    sc.source = &value[1];
    sc.lengths = &uchscf->lengths;
    sc.values = &uchscf->values;
    sc.complete_lengths = 1;
    sc.complete_values = 1;

if (ngx_http_script_compile(&sc) != NGX_OK) {
        return NGX_CONF_ERROR;
    }

    uscf->peer.init_upstream = ngx_http_upstream_init_consistent_hash;

uscf->flags = NGX_HTTP_UPSTREAM_CREATE
        |NGX_HTTP_UPSTREAM_WEIGHT;

这个模块获取upstream模块的配置,设置了peer.init_upstream为函数ngx_http_upstream_init_consistent_hash,这个函数用来初始化upstream的,ngx_http_upstream_init_consistent_hash(ngx_conf_t *cf,
函数会根据服务器的IP和端口号进行一致性哈希计算,

ngx_int_t
ngx_http_upstream_init_consistent_hash(ngx_conf_t *cf,
        ngx_http_upstream_srv_conf_t *us)
{

……

us->peer.init = ngx_http_upstream_init_consistent_hash_peer;

……

for (i = 0; i < us->servers->nelts; i++) {
        for (j = 0; j < server[i].naddrs; j++) {
            for (k = 0; k < ((MMC_CONSISTENT_POINTS * server[i].weight) / server[i].naddrs); k++) {
                ngx_snprintf(hash_data, HASH_DATA_LENGTH, "%V-%ui%Z", &server[i].addrs[j].name, k);
                continuum->nodes[continuum->nnodes].sockaddr = server[i].addrs[j].sockaddr;
                continuum->nodes[continuum->nnodes].socklen = server[i].addrs[j].socklen;
                continuum->nodes[continuum->nnodes].name = server[i].addrs[j].name;
                continuum->nodes[continuum->nnodes].name.data[server[i].addrs[j].name.len] = 0;
                continuum->nodes[continuum->nnodes].point = ngx_crc32_long(hash_data, ngx_strlen(hash_data));
                continuum->nnodes++;
            }
        }
    }

  //排序
    qsort(continuum->nodes, continuum->nnodes,
            sizeof(ngx_http_upstream_consistent_hash_node),
            (const void*) ngx_http_upstream_consistent_hash_compare_continuum_nodes);

……

}

可以看到,是根据服务器的IP和端口号及索引做ngx_crc32_long计算;

这里也指定了ngx_http_upstream_init_consistent_hash_peer函数为peer的init函数,这个在每次连接的时候都会执行,作用是根据key来查找服务器,

static ngx_int_t
ngx_http_upstream_init_consistent_hash_peer(ngx_http_request_t *r,
        ngx_http_upstream_srv_conf_t *us)
{

……

uchscf = ngx_http_conf_upstream_srv_conf(us,
                                          ngx_http_upstream_consistent_hash_module);

uchpd = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_consistent_hash_peer_data_t));

r->upstream->peer.data = uchpd->peers;
    uchpd->peers = us->peer.data;
  //编译变量
    if (ngx_http_script_run(r, &evaluated_key_to_hash,
                uchscf->lengths->elts, 0, uchscf->values->elts) == NULL)
    {
        return NGX_ERROR;
    }
 //根据key计算hash值
    uchpd->point =
        ngx_crc32_long(evaluated_key_to_hash.data, evaluated_key_to_hash.len);

r->upstream->peer.free = ngx_http_upstream_free_consistent_hash_peer;

//获取哪个服务器作计算节点
    r->upstream->peer.get = ngx_http_upstream_get_consistent_hash_peer;

……

}

nginx upstream一致性哈希的实现的更多相关文章

  1. Nginx一致性哈希模块的Lua实现

    Nginx一致性哈希模块的Lua重新实现 技术背景: 最近在工作中使用了nginx+redis 的架构,redis在后台做分布式存储,每个redis都存放不同的数据,这些数据都是某门户网站通过Hado ...

  2. Nginx的负载均衡 - 一致性哈希 (Consistent Hash)

    Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 当后端是缓存服务器时,经常使用一致性哈希算法来进行负载均衡. 使用一致性哈希的好处在于,增减 ...

  3. Nginx 第三方模块的安装以及一致性哈希算法的使用

    Nginx 第三方模块的安装以及一致性哈希算法的使用 第三方模块安装方法总结: 以ngx_http_php_memcache_standard_balancer-master为例 1:解压 到 pat ...

  4. Nginx网络架构实战学习笔记(四):nginx连接memcached、第三方模块编译及一致性哈希应用

    文章目录 nginx连接memcached 第三方模块编译及一致性哈希应用 总结 nginx连接memcached 首先确保nginx能正常连接php location ~ \.php$ { root ...

  5. nginx系列12:一致性哈希算法

    前面一节的hash算法存在一个问题,当上游的应用服务器因某一台down掉导致服务器数量发生变化时,会导致大量的请求路由策略失效,一致性哈希算法可以缓解这个问题. 一致性哈希算法 1,hash算法存在的 ...

  6. Nginx+Memcache+一致性hash算法 实现页面分布式缓存(转)

    网站响应速度优化包括集群架构中很多方面的瓶颈因素,这里所说的将页面静态化.实现分布式高速缓存就是其中的一个很好的解决方案... 1)先来看看Nginx负载均衡 Nginx负载均衡依赖自带的 ngx_h ...

  7. Nginx+upstream针对后端服务器容错的运维笔记

    熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ...

  8. nginx upstream 容错机制

    熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ...

  9. nginx upstream和轮询策略

    upstream nginx upstream语法配置 upstream 后面跟服务名 其中包含了,域名,端口 以及权重,可以看到他既支持http协议也支持socket协议的类型,backup意味着该 ...

随机推荐

  1. 顶级技术盛会KubeCon 2020,网易轻舟布道多云环境云原生应用交付

    在日前的KubeCon 2020中国线上峰会上,VMware中国研发中心架构师.Harbor项目创始人和维护者张海宁,和网易数帆轻舟事业部架构师.Harbor维护者裴明明,共同分享了如何在多云和多集群 ...

  2. Vue学习(十三)模版引擎算是预处理器吗?

    前言 今天在看vue-loader预处理器配置相关的内容,突然看到了Pug,然后有了一个疑问:模版引擎原来是预处理器吗? 答案是:YES 说明 这里重点讨论使用不同的js模板引擎作为预处理器, 下面示 ...

  3. Linux环境下没有权限操作文件或目录

    linux下有超级用户(root)和普通用户,普通用户不能直接操作没有权限的目录,如果出现了没有权限的提示,可以尝试用su命令解决. 比如: #mkdir aaa 我要创建一个aaa的文件夹,没有操作 ...

  4. C++数的表示

    二进制B 八进制O 十进制D 十六进制H / 0x十六进制 十进制数转换成R进制数:整数部分除基取余,上右下左:小数部分乘基取整,上左下右.   浮点数的阶用一种称为移码的编码表示方法,方便对阶.阶的 ...

  5. 计算机网络-应用层(2)FTP协议

    文件传输协议(FTP,File Transfer Protocol)是Internet上使用最广泛的文件传送协议.FTP提供交互式的访问,允许客户指明文件的类型与格式,并允许文件具有存取权限.它屏蔽了 ...

  6. count.exe 个人项目

    Github项目地址:https://github.com/bravedreamer/wordCount/tree/master/wc 一.题目描述: Word Count1. 实现一个简单而完整的软 ...

  7. Java数据结构——红黑树

    红黑树介绍红黑树(Red-Black Tree),它一种特殊的二叉查找树.执行查找.插入.删除等操作的时间复杂度为O(logn). 红黑树是特殊的二叉查找树,意味着它满足二叉查找树的特征:任意一个节点 ...

  8. 封装Vue Element的upload上传组件

    本来昨天就想分享封装的这个upload组件,结果刚写了两句话,就被边上的同事给偷窥上了,于是在我全神贯注地写分享的时候他就神不知鬼不觉地突然移动到我身边,腆着脸问我在干啥呢.卧槽你妈,当场就把我吓了一 ...

  9. 10、Entity Framework Core 3.1入门教程-执行原生SQL

    本文章是根据 微软MVP solenovex(杨旭)老师的视频教程编写而来,再加上自己的一些理解. 视频教程地址:https://www.bilibili.com/video/BV1xa4y1v7rR ...

  10. Clock()函数简单使用(C库函数)

    Clock()函数简单使用(转) 存在于标准库<time.h> 描述 C 库函数 clock_t clock(void) 返回程序执行起(一般为程序的开头),处理器时钟所使用的时间.为了获 ...