-
Notifications
You must be signed in to change notification settings - Fork 231
microservice graceful up down
服务注册时,容器可能还未就绪,但是已经对外提供服务,请求进来就会触发503服务不可用异常;微服务下线时,容器已进入销毁过程,但是注册中心还在健康检查时间范围没有剔除服务,仍然可以对外提供服务,请求进来也会触发503服务不可用异常。
针对上面两种情况,spring-cloud-huawei结合spring-boot-actuator实现微服务优雅上下线,以解决动态扩缩容服务不可用问题。
- pom必须的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
</dependency>
- 开启优雅上下线配置
实现原理为:微服务启动时,先注册一个DOWN状态实例,此时外部无法调用该实例,容器设置脚本持续检查服务/health接口情况,当返回UP状态时,触发服务状态更新。
自动注册状态为DOWN实例配置:
spring:
cloud:
servicecomb:
instance:
initial-status: DOWN
开启优雅上下线功能配置:
spring:
cloud:
servicecomb:
graceful:
servicecombEngine:
enabled: true
配置actuator的endpoint管理:
management:
endpoints:
web:
exposure:
include: "*"
path-mapping:
servicecomb-service-registry: service-registry/servicecomb
management.endpoints.web.exposure.include内容可以是"*",也可以是"health,servicecomb-service-registry"。 management.endpoints.web.path-mapping.servicecomb-service-registry配置"servicecomb-service-registry"Endpoint的调用uri。
- 结合K8S事件,执行脚本完成实例状态更新
优雅上线
实现容器pod的PostStart,并持续检查/health接口,当/health接口返回UP时,调用定义的path-mapping路径,通知微服务注册实现优雅上线。
"post_start": {
"type": "command",
"parameters": {},
"command": [
"sh",
"/opt/cloud/cdpipelinesvr/online.sh" # 根据业务填写脚本路径
]
}
结合上述设置配置,可以通过以下接口调用实现服务注册:调用 http://127.0.0.1:8080/actuator/service-registry/servicecomb ,其中端口号为当前微服务端口,请求body内容如下:
{
"status": "UP"
}
online.sh自动化脚本,注意需要转化为unix格式sed -i 's/\r//' :
ip=$(ifconfig | grep 'inet' | grep -v 127.0.0.1 | awk '{print $2}')
port=8443 #服务端口号
status_check() {
result=$(curl -k -X GET "http://$ip:$port/actuator/health" -H "accept: application/json") #协议根据服务定http/https
echo "${result}" | cut -c12-13
}
status=$(status_check)
count=0
until [ "UP"x = "$status"x ]; do
status=$(status_check)
count=$((count + 1))
echo "check fail, retrying ${count}"
sleep 1
done
curl -k -X POST http://$ip:$port/actuator/service-registry/servicecomb --header 'Content-Type: application/json' --data-raw '{"status": "UP"}' #协议根据服务定http/https
优雅下线
实现容器pod的PreStop,调用 http://127.0.0.1:8080/actuator/service-registry/servicecomb ,status设置为DOWN,提前将服务从注册中心下线,实现优雅下线。
"pre_stop": {
"type": "command",
"parameters": {},
"command": [
"sh",
"/opt/cloud/cdpipelinesvr/offline.sh" # 根据业务填写脚本路径
]
}
offline.sh自动化脚本,注意需要转化为unix格式sed -i 's/\r//' :
ip=$(ifconfig | grep 'inet' | grep -v 127.0.0.1 | awk '{print $2}')
port=8443 # 微服务端口
curl -X POST http://$ip:$port/actuator/service-registry/servicecomb --header 'Content-Type: application/json' --data-raw '{"status": "DOWN"}' # 此处协议根据服务自身可改为https或http
sleep 20 # 睡眠20秒
- pom必须的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-nacos</artifactId>
</dependency>
- 开启优雅上下线配置
实现原理为:微服务启动时,关闭自动注册,此时服务无法对外提供服务,容器设置脚本持续检查服务/health接口情况,当返回UP状态时,触发服务状态更新。
关闭Nacos自动注册配置:
spring:
cloud:
nacos:
discovery:
register-enabled: false
开启优雅上下线功能:
spring:
cloud:
servicecomb:
graceful:
nacosEngine:
enabled: true
配置actuator的endpoint管理:
management:
endpoints:
web:
exposure:
include: "*"
path-mapping:
nacos-service-registry: service-registry/nacos
management.endpoints.web.exposure.include内容可以是"*",也可以是"health,nacos-service-registry"。 management.endpoints.web.path-mapping.nacos-service-registry配置"nacos-service-registry"Endpoint的调用uri。
- 结合K8S事件,执行脚本完成实例状态更新
优雅上线
实现容器pod的PostStart,并持续检查/health接口,当/health接口返回UP时,调用定义的path-mapping路径,通知微服务注册实现优雅上线。
"post_start": {
"type": "command",
"parameters": {},
"command": [
"sh",
"/opt/cloud/cdpipelinesvr/online.sh" # 根据业务填写脚本路径
]
}
结合上述设置配置,可以通过以下接口调用实现服务注册:调用 http://127.0.0.1:8080/actuator/service-registry/nacos ,其中端口号为当前微服务端口,请求body内容如下:
{
"status": "UP"
}
online.sh自动化脚本,注意需要转化为unix格式sed -i 's/\r//' :
ip=$(ifconfig | grep 'inet' | grep -v 127.0.0.1 | awk '{print $2}')
port=8443 #服务端口号
status_check() {
result=$(curl -k -X GET "http://$ip:$port/actuator/health" -H "accept: application/json") #协议根据服务定http/https
echo "${result}" | cut -c12-13
}
status=$(status_check)
count=0
until [ "UP"x = "$status"x ]; do
status=$(status_check)
count=$((count + 1))
echo "check fail, retrying ${count}"
sleep 1
done
curl -k -X POST http://$ip:$port/actuator/service-registry/nacos --header 'Content-Type: application/json' --data-raw '{"status": "UP"}' #协议根据服务定http/https
优雅下线
实现容器pod的PreStop,调用 http://127.0.0.1:8080/actuator/service-registry/nacos ,status设置为DOWN,提前将服务从注册中心下线,实现优雅下线。
"pre_stop": {
"type": "command",
"parameters": {},
"command": [
"sh",
"/opt/cloud/cdpipelinesvr/offline.sh" # 根据业务填写脚本路径
]
}
offline.sh自动化脚本,注意需要转化为unix格式sed -i 's/\r//' :
ip=$(ifconfig | grep 'inet' | grep -v 127.0.0.1 | awk '{print $2}')
port=8443 # 微服务端口
curl -X POST http://$ip:$port/actuator/service-registry/nacos --header 'Content-Type: application/json' --data-raw '{"status": "DOWN"}' # 此处协议根据服务自身可改为https或http
sleep 20 # 睡眠20秒
-
使用Spring Cloud Huawei功能
-
使用服务治理
-
生态集成
-
迁移改造问题
-
配置参考
-
优秀实践
-
常见问题