[转]Nginx主动式后端服务器健康检查配置
2022/2/14 7:19:54
本文主要是介绍[转]Nginx主动式后端服务器健康检查配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
环境:
SpringCloud微服务(eureka注册中心);
nginx作为负载均衡;
场景:
Nginx -> A服务
当流量高峰期时,kill A服务
A服务还没有挂掉,但是注册中心状态为OUT_OF_SERVICE,但是服务在Nginx的upstream中。
Nginx的流量还是会到达A服务,但是A服务Fegin接口调用其他服务时,会出现异常情况。
解决方案:
Nginx主动式调用服务器端的接口(自定义的监控检查接口)。
自定义检查接口:查询Eureka的状态,若状态不是up状态,那么返回500异常。
Nginx收到500异常后,自动将ip:port在upstream中摘除。
代码实现:
@RestController @Slf4j public class HealthController { @Autowired private EurekaClient eurekaClient; @RequestMapping(value = "/health/check/status", method = {RequestMethod.HEAD, RequestMethod.GET}) public boolean checkStatus(HttpServletResponse response) { try { InstanceInfo.InstanceStatus instanceRemoteStatus = eurekaClient.getInstanceRemoteStatus(); boolean up = InstanceInfo.InstanceStatus.UP.equals(instanceRemoteStatus); //设置http的响应码 if (!up) { response.setStatus(500); } return up; } catch (Exception e) { return true; } } }
Nginx配置:
upstream student-service-api { server 172.26.34.101:9050; check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD /health/check/status HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; }
- interval表示每隔3000毫秒向后端发送健康检查包;
- rise表示如果连续成功次数达到2 服务器就被认为是up;
- fail表示如果连续失败次数达到5 服务器就被认为是down;
- timeout表示后端健康请求的超时时间是1000毫秒;
- type表示发送的健康检查包是http请求;
- check_http_send 表示http健康检查包发送的请求内容。为了减少传输数据量,推荐采用“head”方法;
- check_http_expect_alive 指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的;
配置location,查看服务器的健康状态
location ~* /status { check_status; access_log off; }
原文:https://www.jianshu.com/p/beb9d8d26464
参考:Nginx实战系列之功能篇----后端节点健康检查
这篇关于[转]Nginx主动式后端服务器健康检查配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29fail2ban nginx
- 2024-03-18install nginx proxy manager
- 2024-01-2444从零开始用Rust编写nginx,命令行参数的设计与解析及说明
- 2024-01-22比Nginx更好用的Gateway!
- 2024-01-17nginx daemon off
- 2023-12-30Nginx Vhost Traffic Status 是什么意思-icode9专业技术文章分享
- 2023-12-26使用nginx搭建creates.io镜像(稀疏索引)
- 2023-11-18centos7编译安装Nginx教程。
- 2023-11-02nginx启动命令和停止命令
- 2023-10-25Centos7系统Nginx负载均衡如何安装和配置?