nginx健康检查模块

本文所说的nginx健康检查模块是指nginx_upstream_check_module模块。nginx_upstream_check_module模块是Taobao定制的用于健康检查的模块。

其主要作用是根据配置,对upstream中的每个server进行定期健康检查,在其超时或连续多次失败后,将其置为down状态。

其基本用法如下:

upstream cluster {

    # simple round-robin
server 192.168.0.1:80;
server 192.168.0.2:80; check interval=5000 rise=1 fall=3 timeout=4000;
}

更多的用法可以参看README

模块源码分析

分析的版本是TAG:v0.3.0

模块定义

ngx_http_upstream_check_module模块的定义在这里。

ngx_module_t  ngx_http_upstream_check_module = {
NGX_MODULE_V1,
&ngx_http_upstream_check_module_ctx, /* module context */
ngx_http_upstream_check_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_upstream_check_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};

ngx_http_upstream_check_module_ctx用于描述模块的一些上下文。

static ngx_http_module_t  ngx_http_upstream_check_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */ ngx_http_upstream_check_create_main_conf,/* create main configuration */
ngx_http_upstream_check_init_main_conf, /* init main configuration */ ngx_http_upstream_check_create_srv_conf, /* create server configuration */
NULL, /* merge server configuration */ ngx_http_upstream_check_create_loc_conf, /* create location configuration */
ngx_http_upstream_check_merge_loc_conf /* merge location configuration */
};

ngx_http_upstream_check_module_ctx中比较需要关注的是ngx_http_upstream_check_create_srv_conf,用于产生本模块server级别的配置。因为check的配置时在upstream里,属于server级别。所有ngx_http_upstream_check_create_srv_conf会为每个upstream产生一个ngx_http_upstream_check_srv_conf_t配置。

配置分解

ngx_http_upstream_check_srv_conf_tngx_http_upstream_check_srv_conf_s的结构如下:

struct ngx_http_upstream_check_srv_conf_s {
ngx_uint_t port;
ngx_uint_t fall_count;
ngx_uint_t rise_count;
ngx_msec_t check_interval;
ngx_msec_t check_timeout;
ngx_uint_t check_keepalive_requests; ngx_check_conf_t *check_type_conf;
ngx_str_t send; union {
ngx_uint_t return_code;
ngx_uint_t status_alive;
} code; ngx_array_t *fastcgi_params; ngx_uint_t default_down;
};

ngx_http_upstream_check_commands用于分解相关的check命令。

static ngx_command_t  ngx_http_upstream_check_commands[] = {

    { ngx_string("check"),
NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
ngx_http_upstream_check,
0,
0,
NULL },
....
};

以上代码用于分解以"check"开头的命令行。在解析该行时,将会调用 ngx_http_upstream_check 函数。

具体对于ngx_command_t的含义可以参考Nginx模块开发入门

ngx_http_upstream_check函数的主要作用就是将诸如check interval=5000 rise=1 fall=3 timeout=4000的指令分解,并赋值给ngx_http_upstream_check_srv_conf_t

模块运行

运行的入口函数在ngx_http_upstream_check_init_process

static ngx_int_t
ngx_http_upstream_check_init_process(ngx_cycle_t *cycle)
{
return ngx_http_upstream_check_add_timers(cycle);
} static ngx_int_t
ngx_http_upstream_check_add_timers(ngx_cycle_t *cycle)
{
... for (i = 0; i < peers->peers.nelts; i++) { //这里对每个server进行循环
peer[i].shm = &peer_shm[i]; peer[i].check_ev.handler = ngx_http_upstream_check_begin_handler; //配置check的钩子函数
... peer[i].check_timeout_ev.handler =
ngx_http_upstream_check_timeout_handler; //配置超时的钩子函数
... /*
* We add a random start time here, since we don't want to trigger
* the check events too close to each other at the beginning.
*/
delay = ucscf->check_interval > 1000 ? ucscf->check_interval : 1000;
t = ngx_random() % delay; //这里的设计比较特别,在一个随机的实际开始检查,以免所有的检查在一起撞车 ngx_add_timer(&peer[i].check_ev, t);
} return NGX_OK;
}

至于具体是如何运行check检查的,可以参看ngx_http_upstream_check_begin_handler函数。

nginx健康检查模块源码分析的更多相关文章

  1. Spark Scheduler模块源码分析之TaskScheduler和SchedulerBackend

    本文是Scheduler模块源码分析的第二篇,第一篇Spark Scheduler模块源码分析之DAGScheduler主要分析了DAGScheduler.本文接下来结合Spark-1.6.0的源码继 ...

  2. Spark Scheduler模块源码分析之DAGScheduler

    本文主要结合Spark-1.6.0的源码,对Spark中任务调度模块的执行过程进行分析.Spark Application在遇到Action操作时才会真正的提交任务并进行计算.这时Spark会根据Ac ...

  3. Zepto事件模块源码分析

    Zepto事件模块源码分析 一.保存事件数据的handlers 我们知道js原生api中要移除事件,需要传入绑定时的回调函数.而Zepto则可以不传入回调函数,直接移除对应类型的所有事件.原因就在于Z ...

  4. Django(51)drf渲染模块源码分析

    前言 渲染模块的原理和解析模块是一样,drf默认的渲染有2种方式,一种是json格式,另一种是模板方式. 渲染模块源码入口 入口:APIView类中dispatch方法中的:self.response ...

  5. Fabric2.2中的Raft共识模块源码分析

    引言 Hyperledger Fabric是当前比较流行的一种联盟链系统,它隶属于Linux基金会在2015年创建的超级账本项目且是这个项目最重要的一个子项目.目前,与Hyperledger的另外几个 ...

  6. Nginx学习笔记4 源码分析

    Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...

  7. Django(48)drf请求模块源码分析

    前言 APIView中的dispatch是整个请求生命过程的核心方法,包含了请求模块,权限验证,异常模块和响应模块,我们先来介绍请求模块 请求模块:request对象 源码入口 APIView类中di ...

  8. Django(49)drf解析模块源码分析

    前言 上一篇分析了请求模块的源码,如下: def initialize_request(self, request, *args, **kwargs): """ Retu ...

  9. Django(50)drf异常模块源码分析

    异常模块源码入口 APIView类中dispatch方法中的:response = self.handle_exception(exc) 源码分析 我们点击handle_exception跳转,查看该 ...

随机推荐

  1. 顶级jQuery树插件

    顶级jQuery树插件 顶级jQuery树插件 2013-03-05 17:20 139人阅读 评论(0) 收藏 举报 jsTree JsTree是一个基于jQuery的Tree控件.支持HTML.J ...

  2. WEB浏览器与服务器通讯过程

    以访问网页www.baidu.com为例,下面是使用Wireshark捕捉到的数据: 浏览器先发起一个TCP连接,然后发送GET报文给服务器,服务器之后返回一个Response报文. 从服务器端返回时 ...

  3. 游戏开发常用JS

    游戏插件:cocos2d,createjs,webGl(3d),three.js(3d插件) web插件:Bootstrap插件.less尽量写在服务端. chart.js:精巧的js图标绘制工具库

  4. 告别alert,拥抱console

    记得学习javascript的第一个demo就是alert("Hello  World");可是学习接触javascript这么长时间了还是在alert,因为javascript调 ...

  5. Objective C多态

    面向对象的封装的三个基本特征是.继承和多态. 包是一组简单的数据结构和定义相关的操作在上面的其合并成一个类,继承1种亲子关系,子类能够拥有父类定的成员变量.属性以及方法. 多态就是指父类中定义的成员变 ...

  6. mysql存储过程及常用函数

    原文:mysql存储过程及常用函数 一.函数 1.数学函数 CEIL()进一取整 SELECT CEIL(1.2);2 FLOOR()舍一取整 SELECT FLOOR(1.9);9 MOD取余数(取 ...

  7. DDD(领域驱动设计)理论结合实践

    DDD(领域驱动设计)理论结合实践   写在前面 插一句:本人超爱落网-<平凡的世界>这一期,分享给大家. 阅读目录: 关于DDD 前期分析 框架搭建 代码实现 开源-发布 后记 第一次听 ...

  8. Windows 下让 Python 多个版本共存(支持 pip)

    转载自 http://blog.kgzx.net/index.php/archives/40/ 因为类库兼容性的关系,写实际项目时都是用 Python 2,但自己试验性的写点小东西还是喜欢用 Pyth ...

  9. 利用XCode来进行IOS的程序开发

    利用XCode来进行IOS的程序开发 本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换 ...

  10. 一、SOAP简单对象访问协议讲解

    一.SOAP简单对象访问协议讲解 今天给大家讲讲SOAP的基本知识.下节给大家演示创建基于SOAP的Web Service. 更多SOA文章请查看我的个人博客. 首先,让我来简单一下入门SOAP所需的 ...