导航

 

通常我们会使用Nginx的ngx_http_upstream_module模块来配置服务器组,示例如下

 upstream springboot {
 server 10.3.73.223:8080 max_fails=2 fail_timeout=30s;
 server 10.3.73.223:8090 max_fails=2 fail_timeout=30s;
 }
 server {
 listen 80;
 server_name localhost;
 location /test {
 proxy_pass http://springboot;
 }
 }

在30s内(fail_timeout,默认值为10s),与服务端通讯失败2次(max_fails,默认值为1,设置为0则认为服务端一直可用),则认为服务器不可用

不可用服务器在30s内与服务端通讯成功2次,则认为服务器恢复

特别需要注意的是,何为与服务端通讯失败是由upstream的使用方定义的(ngx_http_proxy_module、proxy_next_upstream、fastcgi_next_upstream和memcached_next_upstream)

以proxy_next_upstream为例:

与服务端建立连接、向服务端发送请求或者解析服务端响应头时,发生异常或超时将被认为是通讯失败

服务端返回的响应为空或不合法将被认为是通讯失败

如果配置了http_500,http_502,http_503,http_504和http_429,服务端返回这些状态码将被认为是通讯失败

服务端返回http_403和http_404永远不会被认为通讯失败

当upstream中的一台服务器响应失败时, Nginx会将请求转发给下一台服务器,直到所有的服务器都发送过该请求,如果此时依然无法获得成功的响应,客户端将收到最后一台服务器返回的响应结果

使用上面的配置进行测试:

package com.sean.test;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* Created by seanzou on 2018/8/20.
*/
@org.springframework.stereotype.Controller
public class Controller {
@RequestMapping("/test")
@ResponseBody
// @ResponseStatus(code= HttpStatus.NOT_FOUND)
@ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR)
public String test(){
System.out.println("test");
// throw new RuntimeException();
return "test";
}
}

即便服务端响应404、500状态码,Nginx依然认为通讯成功,除非停掉当前服务

upstream存在如下一些问题:

1 无法主动感知服务器状态

2 配置不灵活,无法自定义通讯失败判断条件(仅提供少数定义好的状态码可供使用)

1,ngx_http_upstream_hc_module

ngx_http_upstream_hc_module允许周期性的对服务器组中的服务器进行健康检查,前提条件是服务器组中的服务器必须使用共享内存模式(共享内存用来保存服务器组的配置信息以及运行时状态,Nginx的woker进程将共享该配置和状态),示例如下:

  1.  
    upstream dynamic {
  2.  
    # 共享内存
  3.  
    zone upstream_dynamic 64k;
  4.  server backend1.example.com;
  5.  
    server backend2.example.com;
  6.  
    }
  7.  http {
  8.  
    server {
  9.  
    ...
  10.  
    # Nginx每5s(默认值)就会向dynamic中的每一个服务器发送“/”请求
  11.  
    # location / {
  12.  
    # proxy_pass http://dynamic;
  13.  
    # health_check;
  14.  
    # }
  15.  # hc可以通过自定义的校验规则判断服务器状态
  16.  
    location / {
  17.  
    proxy_pass http://dynamic;
  18.  
    health_check match=welcome;
  19.  
    }
  20.  
    }
  21.  # 如果配置了多个条件,所有条件均满足服务器状态才被认为是正常的
  22.  
    # 响应状态码为200,且响应body中包含"Welcome to nginx!"服务器状态才被认为是正常的
  23.  
    # Nginx仅检查响应body中的前256k数据
  24.  
    match welcome {
  25.  
    status 200;
  26.  
    header Content-Type = text/html;
  27.  
    body ~ "Welcome to nginx!";
  28.  
    }
  29.  
    }

功能十分强大,遗憾的是只有Nginx商业版才包含该模块

 

2,nginx_upstream_check_module

这个模块是由淘宝团队开发的,并且是完全开源的:nginx_upstream_check_module

淘宝Tengine自带该模块,如果我们没有使用Tengine,可以通过打补丁的方式把该模块加到我们自己的Nginx中

  1.  
    sean@ubuntu:~$ unzip nginx_upstream_check_module-master.zip
  2.  
    sean@ubuntu:~$ cd nginx-1.14.0/
  3.  
    # 打补丁
  4.  
    sean@ubuntu:~/nginx-1.14.0$ patch -p1 < ../nginx_upstream_check_module-master/check_1.12.1+.patch
  5.  
    # 添加心跳检测模块后重新编译
  6.  
    sean@ubuntu:~/nginx-1.14.0$ sudo ./configure --add-module=../nginx_upstream_check_module-master/
  7.  
    sean@ubuntu:~/nginx-1.14.0$ sudo make
  8.  
    # 备份旧文件
  9.  
    sean@ubuntu:~/nginx-1.14.0$ sudo mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  10.  
    # 使用新文件
  11.  
    sean@ubuntu:~/nginx-1.14.0$ sudo cp ./objs/nginx /usr/local/nginx/sbin/
  12.  
    sean@ubuntu:~/nginx-1.14.0$ sudo /usr/local/nginx/sbin/nginx -t
  13.  
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  14.  
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

修改Nginx配置,官方文档参考:ngx_http_upstream_check_module document

  1.  
    http {
  2.  
    check_shm_size 1M;
  3.   upstream springboot {
  4.  
    server 10.3.73.223:8080;
  5.  
    server 10.3.73.223:8090;
  6.  
    check interval=3000 rise=2 fall=5 timeout=1000 type=http;
  7.  
    check_keepalive_requests 1;
  8.  
    check_http_send "GET /test HTTP/1.0\r\n\r\n";
  9.  
    check_http_expect_alive http_2xx;
  10.  
    }
  11.  server {
  12.  
    listen 80;
  13.  
    server_name localhost;
  14.   location /test {
  15.  
    proxy_pass http://springboot;
  16.  
    }
  17.  location /status {
  18.  
    check_status;
  19.  
    access_log off;
  20.  
    }
  21.  
    }
  22.  
    }

后端服务器健康检查状态都存于共享内存中,该指令可以设置共享内存的大小,如果服务器数量较大,需要注意该设置是否够用

语法:check_shm_size size

默认值:1M

上下文:http

 

健康检查规则配置

语法:check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true | false]

[type=tcp | http | ssl_hello | mysql | ajp] [port=check_port]

默认值:check interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp

上下文:upstream

interval:向后端发送的健康检查的间隔时间

fall(fall_count): 连续失败次数达到fall_count,服务器被认为是down状态

rise(rise_count): 连续成功次数达到rise_count,服务器被认为是up状态

timeout: 健康检查请求超时时间

default_down: 设定初始时服务器的状态,如果是true,服务器默认是down状态,如果是false,服务器默认是up状态,默认值是true,也就是一开始服务器被认为不可用,要等健康检查请求达到一定成功次数以后才会被认为是健康的

type:健康检查请求协议类型,支持tcp,http,ssl_hello,mysql和ajp

port:健康检查请求发送端口,可以与后端服务器服务端口不同

 

一个连接可发送的请求数,默认值为1,表示完成1次请求后即关闭连接

语法:check_keepalive_requests request_num

默认值:1

上下文:upstream

 

HTTP接口健康检查发送的请求内容,为了减少传输数据量,推荐采用HEAD方法

语法:check_http_send http_packet

默认值:"GET / HTTP/1.0\r\n\r\n"

上下文:upstream

 

HTTP接口健康检查成功状态码

语法:check_http_expect_alive [http_2xx | http_3xx | http_4xx | http_5xx]

默认值:http_2xx | http_3xx

上下文:upstream

 

后端服务器状态查询页面,提供三种展示方式

语法:check_status [html | csv | json]

默认值:check_status html

上下文:location

 

------------2018-11-29------------

线上环境检测的worker机使用tomcat作为容器,check_http_send需要配置Host(仅需配置即可,值是否正确不重要),否则会一直发送心跳检测,但是一直判定检测失败,示例如下:

  1.  
    upstream bj{
  2.  
    server 1.2.3.4:80;
  3.  
    server 5.6.7.8:80;
  4.  
    check interval=5000 rise=2 fall=2 timeout=1000 type=http;
  5.  
    check_http_send "GET /admin/health_check.htm HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n";
  6.  
    check_http_expect_alive http_2xx;
  7.  
posted on 2019-06-26 11:26  slqt  阅读(3582)  评论(0编辑  收藏  举报