地址: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. Python定时任务轻量解决方案---Schedule

    写后端的同学们可能都知道,工作中可能需要周期性执行一些任务,俗称定时任务.Linux环境下,可以借助于系统自带的crontab完成定时任务.但是很多时候,开发的同学们可能并没有权限去操作crontab ...

  2. 强化学习模型实现RL-Adventure

    源代码:https://github.com/higgsfield/RL-Adventure 在Pytorch1.4.0上解决bug后的复现版本:https://github.com/lucifer2 ...

  3. 如何在Linux上使用scp命令进行服务器之间的文件/目录传输

    1. 本地上传文件到远程: scp [local_file_path] [username]@[server_ip]:[remote_dir] 2. 本地上传目录到远程: scp -r [local_ ...

  4. muduo源码解析2-AtomicIntegerT类

    AtomicIntegerT template<typename T> class atomicTntergerT:public noncopyable { }; 作用: 与std::ao ...

  5. NET Core Kestrel部署HTTPS 一个服务器绑一个证书 一个服务器绑多个证书

    .net core 3.0 网站发布到centos后,绑定ssl证书,一个服务器绑一个证书,一个服务器绑多个证书 开始之前:对于windows服务器不存在这个问题,在iis中绑定证书是非常简单的一件事 ...

  6. Android开发,java开发程序员常见基础面试题,更换两个变量的值,java逻辑代码

    public class abswitch{ public static void main (String args []){ //author:qq986945193 int a=1; int b ...

  7. 地图_SDK

    不仅仅是Google,您必须知道的全球十大地图API https://blog.csdn.net/u013068887/article/details/79322096

  8. War3模型导出

    Wiki https://en.wikipedia.org/wiki/.MDX 教程 http://www.ou99.com/article-25996-1.html http://blog.csdn ...

  9. DLX AlgorithmX

    AlgorithmX精确覆盖: https://en.wikipedia.org/wiki/Knuth's_Algorithm_X DLX的基础算法 https://zh.wikipedia.org/ ...

  10. [BUUOJ记录] [BSidesCF 2020]Had a bad day

    主要考察文件包含以及php://filter伪协议的一个小trick.题目很简单,但是因为不知道这个trick卡了很久,记录一下 进入题目看到有两个按钮,没有其他信息: 点击按钮显示出来一张图片,然后 ...