停服业务需要,服务器更新升级时,有时候需要维护一段时间,这段时间内,为了保证用户体验,通常都会在app端展示维护信息,和维护时间等等,
下面是一种app停服方案:
Nginx中编码,提供一个接口,app客户端每次登录前都会先请求这个接口,这个接口如果返回服务器已经停服,那么就根据返回值中的维护信息进行展示,
接口如下图:
server {
server_name 192.168.19.218;
listen 80;
# 导入停服配置
include maintenance/serviceInfo.conf;
# 服务状态接口
location /service_info {
default_type "application/json;charset=UTF-8";
if ($service_status = "NORMAL") {
return 200 '{"service_status":"NORMAL","maintenance_begin_time":"","maintenance_end_time":"","maintenance_intro":""}';
}
if ($service_status = "WAIT_STOP") {
return 200 '{"service_status":"WAIT_STOP","maintenance_begin_time":"$maintenance_begin_time","maintenance_end_time":"$maintenance_end_time","maintenance_intro":"$maintenance_intro"}';
}
if ($service_status = "STOP") {
return 200 '{"service_status":"STOP","maintenance_begin_time":"$maintenance_begin_time","maintenance_end_time":"$maintenance_end_time","maintenance_intro":"$maintenance_intro"}';
}
}
# 访问后端接口
location /
{
# 若停服,则直接以503的错误码返回
if ($service_status = "STOP") {
return 503 '{"code": "503", "message": "停服更新中!"}';
}
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Credentials "true";
add_header Cache-Control no-cache,private;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
add_header Access-Control-Allow-Headers Origin,X-Requested-With,Content-Type,Accept,authorization,bysyskey;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://apiserver;
#proxy_http_version 1.1;
#proxy_set_header Connection "";
}
}
maintenance/serviceInfo.conf 配置停服信息
# 服务状态,NORMAL:正常,WAIT_STOP:待停服,STOP:停服
set $service_status NORMAL;
# 维护开始时间
set $maintenance_begin_time 1627530630000;
# 维护结束时间
set $maintenance_end_time 1627530630010;
# 维护内容:
set $maintenance_intro '<h3><span class=\\"ql-size-large\\">停服维护公告</span></h3><p>1、修复BUG</p><p>2、<span style=\\"color: rgb(230, 0, 0);\\">增加关卡</span></p><p><br></p>';