通过nginx解决服务集群发布引发的问题

什么叫优雅关闭?

非优雅关闭会导致业务的损失或者影响业务数据,比如 kill -9 暴力停止导致正在进行的用户瞬间无法访问,而数据瞬间丢失或缺失,引发的一连串问题。而优雅关闭正是为了解决该问题而提了的一系列优化措施,指的是在系统在服务过程中,需要更新或者停机,通过一定的机制或者顺序按照业务或者相关技术的流程进行服务的无损或优雅的进行服务停止、重启等。

通过nginx防止服务关闭后请求超时

当nginx轮询请求集群的时候,若某个节点挂了,导致用户无法请求或一直等待,严重影响了用户体验。8081和8082然后关掉一个服务。

再次请求:

代码下载:https://gitee.com/hong99/spring/issues/I1N1DF

通过nginx新增配置如下:

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

该指令可以打开后端服务器的健康检查功能。指令后面的参数意义是:

  • interval:发送的健康检查包的间隔。

  • fall(fallcount): 如果连续失败次数达到fallcount,服务器就被认为是down。

  • rise(risecount): 如果连续成功次数达到risecount,服务器就被认为是up。

  • timeout: 后端健康请求的超时时间。

  • type:健康检查包的类型,现在支持以下多种类型

全配置

#user nobody;
worker_processes  1;


events {
    worker_connections  1024;
}


http {
   # 每秒不同超过1次
   limit_req_zone $binary_remote_addr zone=allips:10m rate=1r/s;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
   upstream tomcat_cluster{
       server localhost:8081;
       server localhost:8082;
       check interval=3000 rise=2 fall=5 timeout=1000 type=http;
   }

    server {
        listen       80;
        server_name  localhost;
        location / {
         #limit_conn one 1;
            limit_req zone=allips burst=1 nodelay;
            proxy_pass http://tomcat_cluster; 
            #设置代理
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
         #proxy_connect_timeout 1s;
            #proxy_read_timeout 36000s;
            #proxy_send_timeout 36000s; 
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
      error_page 500 502 503 504 /503;
      location = /503 {
         default_type application/json; 
         add_header Content-Type 'text/html; charset=utf-8';
         return 200 '{"code":0,"msg":"访问高峰期请重试..."}';
        }
      location /status { 
        stub_status on; #表示开启stubStatus的工作状态统计功能。
        access_log on; #access_log off; 关闭access_log 日志记录功能。
        #auth_basic "status"; #auth_basic 是nginx的一种认证机制。
        #auth_basic_user_file conf/htpasswd; #用来指定密码文件的位置。
      }
    }

}

重新Nginx

nginx -s reload

发现已成功请求。不会卡死,

全部转移到81这台了。

重新启动服务:

再次请求两次,发现服务已经可以用了。证明ng可以监听服务的状态再进行转发。

代码下载:https://gitee.com/hong99/spring/issues/I1N1DF

注:

nginx这个check需要安装模块而在docker非常麻烦,建议自行用linux系统自己去尝试。

参考文章:

https://cloud.tencent.com/developer/article/1027287

https://www.cnblogs.com/caozengling/p/10136232.html

https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/reference/htmlsingle/#production-ready-endpoints

https://jiyiren.github.io/2018/06/18/jvm-exit/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值