Nginx健康检查

本文介绍了Nginx的自带健康检查机制及其缺陷,着重讲解了淘宝Tengine的nginx_upstream_check_module模块如何实现主动式健康检查,包括模块下载、配置过程以及使用场景,以及如何通过检查数据库连接保证应用服务的稳定性。
摘要由CSDN通过智能技术生成

Nginx健康检查nginx_upstream_check_module

nginx健康检查介绍:

​ 主动健康检查,nignx定时主动地去ping后端的服务列表,当发现某服务出现异常时,把该服务从健康列表中移除,当发现某服务恢复时,又能够将该服务加回健康列表中。nginx自带的upstream轮询可以实现业务接口切换, nginx有一个开源的实现nginx_upstream_check_module模块能更加平滑的进行业务切换

nginx自带健康检查的缺陷:

  1. Nginx只有当有访问时后,才发起对后端节点探测。
  2. 如果本次请求中,节点正好出现故障,Nginx依然将请求转交给故障的节点,然后再转交给健康的节点处理。所以不会影响到这次请求的正常进行。但是会影响效率,因为多了一次转发
  3. 自带模块无法做到预警
  4. 被动健康检查, 应用服务器没挂,但是数据库连接池不够导致应用假死, Nginx任然认为它是一个正常节点, 继续把请求打在这台服务器上

使用第三访模块nginx_upstream_check_module:

  1. 区别于nginx自带的非主动式的心跳检测,淘宝开发的tengine自带了一个提供主动式后端服务器心跳检测模块
  2. 若健康检查包类型为http,在开启健康检查功能后,nginx会根据设置的间隔向指定的后端服务器端口发送健康检查包,并根据期望的HTTP回复状态码来判断服务是否健康。
  3. 后端真实节点不可用,则请求不会转发到故障节点
  4. 故障节点恢复后,请求正常转发

介绍nginx_upstream_check_module模块针对nginx1.20+

一、模块下载解压

nginx下载地址:https://nginx.org/en/download.html

nginx_upstream_check_module模块下载:

github地址: https://github.com/yaoweibin/nginx_upstream_check_module

taobao官网:http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

下载

https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

在这里插入图片描述

上传到服务器并解压

tar -zxvf nginx-1.22.1.tar.gz
unzip nginx_upstream_check_module-master.zip

在这里插入图片描述

二、官方步骤

安装

在这里插入图片描述

配置

在这里插入图片描述

三、自己安装步骤跟着官网来

安装patch

yum install -y patch

#(重点: 已安装nginx一定是停止运行状态的)
# 注意check_1.20.1+.patch 版本要跟你的nginx版本对应上
#(一定要执行这行命令, 要不然健康检查会报错: http upstream check module can not find any check server, make sure you've added the check servers, client: xxx.xxx.xxx.xxx, server: localhost, request: "GET /status HTTP/1.1", host: "xxx.xxx.xxx.xxx:8087")

# 执行patch命令一定要在(nginx源码目录)
patch -p1 < /home/app/nginx_upstream_check_module-master/check_1.20.1+.patch

在这里插入图片描述

安装nginx和模块nginx_upstream_check_module-master

# 进入nginx安装包目录, 执行下面命令, 根据自己的实际情况安装对应参数值改成自己的 (重点: nginx一定是停止运行状态的)
./configure --user=app --group=app --with-http_ssl_module --with-threads --with-file-aio --with-http_stub_status_module --add-module=/opt/app/nginx_upstream_check_module-master/ --prefix=/opt/app/nginx

make && make install

–prefix: nginx主目录
–user: nginx启动用户
–group: nginx启动组
–add-module: 添加模块
–with-http_ssl_model: 启动ssl模块

配置nginx

Nginx会每隔3000毫秒(3秒)向每个服务器发送一个GET请求/dist-app-ytgz-approve/health_check接口ip是对应的server后面的ip端口。如果服务器连续两次(rise=2)返回2xx或3xx状态码,那么Nginx就认为这个服务器是健康的。如果服务器连续五次(fall=5)没有响应或者返回非2xx或3xx的状态码,那么Nginx就认为这个服务器是不健康的,会自动将其剔除。

为什么请求接口带/dist-app-ytgz-approve那是因为我后台application.yaml里面配置的server.servlet.context-path: /dist-app-ytgz-approve

http {
    upstream approve {
      server xxx.xxx.xxx.xxx:9010;
      server xxx.xxx.xxx.xxx:9090;
      ip_hash;
        
        
      check interval=3000 rise=2 fall=5 timeout=5000 type=http;   
      check_http_send "GET /dist-app-ytgz-approve/health_check HTTP/1.0\r\n\r\n";    
      check_http_expect_alive http_2xx http_3xx;
      # Nginx会每隔3000毫秒(3秒)向每个服务器发送一个GET请求/dist-app-ytgz-approve/health_check接口ip是对应的server后面的ip端口。如果服务器连续两次(rise=2)返回2xx或3xx状态码,那么Nginx就认为这个服务器是健康的。如果服务器连续五次(fall=5)没有响应或者返回非2xx或3xx的状态码,那么Nginx就认为这个服务器是不健康的,会自动将其剔除。
	}

	server {
        listen       8087;
        server_name  localhost;

        location /dist-app-ytgz-approve {

          proxy_pass http://approve;
          proxy_set_header        Host $http_host;
          proxy_set_header        X-Real-IP $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header        X-Forwarded-Port $server_port;
          proxy_set_header        X-Forwarded-Proto $scheme;

          add_header X-Route-Ip $upstream_addr;
          add_header X-Route-Status $upstream_status;
        }

        location /status {
          check_status;

          access_log off;
        }

    }
}

后台健康检查接口

package com.dist.ytgz.approve.controller;

import com.dist.common.util.LogUtil;
import com.dist.ytgz.approve.service.HealthCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author <a href="mailto:zhangxiao@dist.com.cn">Zhang Xiao</a>
 * @since
 */
@RequestMapping
@RestController
public class HealthCheckController {

    @Autowired
    private HealthCheckService healthCheckService;

    @GetMapping("/health_check")
    public ResponseEntity<String> healthCheck() {
        // 这里可以添加检查数据库连接数的逻辑
        // 如果数据库连接数正常,返回200状态码
        // 如果数据库连接数耗尽,返回503状态码
        Long startTime = System.currentTimeMillis();
        boolean isDatabaseHealthy = healthCheckService.checkDatabaseHealth();

        if (isDatabaseHealthy) {
            Long endTime = System.currentTimeMillis();
            LogUtil.error("健康检查总共耗时: " + (endTime-startTime) / 1000.0);
            return ResponseEntity.ok("Healthy");
        } else {
            Long endTime = System.currentTimeMillis();
            LogUtil.error("不健康检查总共耗时: " + (endTime-startTime) / 1000.0);
            return ResponseEntity.status(503).body("Unhealthy");
        }
    }

}





package com.dist.ytgz.approve.service.impl;

import com.dist.ytgz.approve.service.HealthCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * TODO
 *
 * @author <a href="mailto:zhangxiao@dist.com.cn">Zhang Xiao</a>
 * @since
 */
@Service
public class HealthCheckServiceImpl implements HealthCheckService {

    @Autowired
    private EntityManager em;

    @Override
    public boolean checkDatabaseHealth() {
        try {
            // 这里的SQL语句应该替换为适合您的数据库的语句
            Query query = em.createNativeQuery("SELECT count(1) from dual");
            query.getSingleResult();
            return true;
        } catch (Exception e) {
            // 如果查询失败,那么可能是数据库连接数耗尽,或者数据库服务器出现了其他问题
            return false;
        }
    }
}

四、启动nginx

nginx健康检查的日志信息

在这里插入图片描述

nginx服务器状态

在这里插入图片描述
server number: 是后端服务器的数量
generation: 是Nginx reload的次数
Index: 是服务器的索引
Upstream: 是在配置中upstream的名称
Name: 是服务器IP:PORT
Status: 是服务器的状态
Rise counts: 是服务器连续检查成功的次数
Fall counts: 是连续检查失败的次数
Check type: 是检查的方式
Check port: 是后端专门为健康检查设置的端口

  • 14
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
nginx健康检查可以使用nginx_upstream_check_module进行。这个模块相对于nginx自带的check模块来说更为精细和高级。nginx_upstream_check_module的特点如下: 1. 它可以主动地对后端服务列表进行健康检查,通过定时去ping后端服务来判断它们的状态。 2. 当发现某个服务出现异常时,该模块会将这个服务从健康列表中移除。 3. 当发现某个服务恢复正常时,它又会将该服务加回健康列表中。 这个过程可以有日志记录。 但是需要注意的是,为了使用nginx_upstream_check_module,你需要进行源码编译,因为目前的rpm包并没有自带这个模块。你可以按照以下步骤进行编译和安装: 1. 下载nginx的源码并进入源码目录。 2. 应用nginx_upstream_check_module的补丁文件,命令如下:patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12.patch。 3. 运行configure命令,指定相关的编译参数,例如:--with-http_ssl_module、--with-openssl等。 4. 运行make命令进行编译,编译参数需要和之前的一样。 5. 备份原来的nginx可执行文件,将编译生成的新的nginx可执行文件复制到对应的目录。 6. 使用nginx -t命令检查配置文件是否有问题。 7. 最后,使用kill命令向nginx进程发送USR2信号,以重新加载配置文件。 这样,你就可以使用nginx_upstream_check_module来进行nginx健康检查了。参考资料[3]可以提供更详细的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只因为你温柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值