nginx upstream一致性哈希的实现
地址: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一致性哈希的实现的更多相关文章
- Nginx一致性哈希模块的Lua实现
Nginx一致性哈希模块的Lua重新实现 技术背景: 最近在工作中使用了nginx+redis 的架构,redis在后台做分布式存储,每个redis都存放不同的数据,这些数据都是某门户网站通过Hado ...
- Nginx的负载均衡 - 一致性哈希 (Consistent Hash)
Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 当后端是缓存服务器时,经常使用一致性哈希算法来进行负载均衡. 使用一致性哈希的好处在于,增减 ...
- Nginx 第三方模块的安装以及一致性哈希算法的使用
Nginx 第三方模块的安装以及一致性哈希算法的使用 第三方模块安装方法总结: 以ngx_http_php_memcache_standard_balancer-master为例 1:解压 到 pat ...
- Nginx网络架构实战学习笔记(四):nginx连接memcached、第三方模块编译及一致性哈希应用
文章目录 nginx连接memcached 第三方模块编译及一致性哈希应用 总结 nginx连接memcached 首先确保nginx能正常连接php location ~ \.php$ { root ...
- nginx系列12:一致性哈希算法
前面一节的hash算法存在一个问题,当上游的应用服务器因某一台down掉导致服务器数量发生变化时,会导致大量的请求路由策略失效,一致性哈希算法可以缓解这个问题. 一致性哈希算法 1,hash算法存在的 ...
- Nginx+Memcache+一致性hash算法 实现页面分布式缓存(转)
网站响应速度优化包括集群架构中很多方面的瓶颈因素,这里所说的将页面静态化.实现分布式高速缓存就是其中的一个很好的解决方案... 1)先来看看Nginx负载均衡 Nginx负载均衡依赖自带的 ngx_h ...
- Nginx+upstream针对后端服务器容错的运维笔记
熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ...
- nginx upstream 容错机制
熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ...
- nginx upstream和轮询策略
upstream nginx upstream语法配置 upstream 后面跟服务名 其中包含了,域名,端口 以及权重,可以看到他既支持http协议也支持socket协议的类型,backup意味着该 ...
随机推荐
- unity探索者之iOS微信登录、分享
版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/8405700.html iOS接入微信的SDK相对于安卓要麻烦一点,除了核心功能代码 ...
- Linux上通过curl发送PUT和POST请求
通常而言,我们都使用curl发送get请求,但是还是可以使用它发送一些其他类型的请求的,如PUT/POST 只需要使用-X参数即可:
- centos AAVMF_CODE.fd 文件下载
centos: yum install AAVMF -y ubuntu: apt-get install qemu-efi*
- Python之运维
这几日一直研究运维监控的事情,有次看见有一个脚本写的还不错,如今已经找不到地址了 就只能用Python代替shell了 其中原理是 监控 /proc/下的各种文件,/proc/ 顾名思义其为进程的文件 ...
- JDK14-ZGC调研初探
原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 背景 公司ElasticSearch准备进行升级,而ElasticSearch7以上则是已 ...
- 基于 abp vNext 微服务开发的敏捷应用构建平台 - 设计构想
许多中小企业的管理模式都是在自身的发展过程中不断摸索,逐步建立起来的,每一家都有其独有的管理模式,而且随着企业的不断发展,管理模式也在不断变化中.企业在发展壮大的过程中离不开信息化系统的支撑,企业在构 ...
- Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)
题目内容 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the ...
- 牛客网PAT练兵场-锤子剪刀布
题目地址:https://www.nowcoder.com/questionTerminal/79db907555c24b15a9c73f7f7d0e2471 题解:无 /** * *作者:Ycute ...
- 牛客网数据库SQL实战解析(11-20题)
牛客网SQL刷题地址: https://www.nowcoder.com/ta/sql?page=0 牛客网数据库SQL实战解析(01-10题): https://blog.csdn.net/u010 ...
- 简述BFS与DFS
简述BFS与DFS 最近学习了数据结构课程以及应对蓝桥杯备考,所以花费了一点时间将比较重要的两个搜索BFS(宽度优先搜索)和DFS(深度优先搜索)大致思路以及代码整理出来,如有错误,还请各位大佬批评改 ...