希望今年是最后一個疫情寒冬
已經(jīng)三年沒有和父母一起過年了
明年一定要在一起過年
背景
最近剛?cè)肼氁患夜荆患夹g(shù)總監(jiān)安排了個優(yōu)雅升級的改造任務(wù)
服務(wù)上線的問題
在上線部署過程中會出現(xiàn)服務(wù)短暫不可用的問題,如下:
1、是先停止舊服務(wù)還是先升級新服務(wù),如果先停止舊服務(wù),如何保證舊服務(wù)中的業(yè)務(wù)執(zhí)行完成,而且不會再接受到新的業(yè)務(wù)進(jìn)來。如果先升級新服務(wù),那么同樣如何保證舊服務(wù)不會再接受到新的業(yè)務(wù)進(jìn)來。
2、服務(wù)之間的調(diào)用通過注冊中心,如何保證注冊中心的服務(wù)是新服務(wù),舊服務(wù)如何及時(shí)的剔除。
解決思路
1、通過k8s滾動升級,先啟動新Pod,再停止舊Pod。避免業(yè)務(wù)出現(xiàn)不能使用的情況。
2、k8s容器生命周期結(jié)束前會回調(diào)hook的preStop,preStop調(diào)用服務(wù)接口,接口中執(zhí)行從注冊中心剔除本服務(wù)操作。此時(shí)老服務(wù)已經(jīng)不能被調(diào)用。在超過terminationGracePeriodSeconds配置的30s后,最后直接強(qiáng)殺。此過程保證了老服務(wù)如果有業(yè)務(wù)未執(zhí)行完成,在30s的時(shí)間內(nèi)執(zhí)行完成。
3、對eurekaServer和微服務(wù)的eureka配置及ribbon修改。
優(yōu)雅升級改造步驟
一、k8s
通過k8s容器生命周期hook的preStop實(shí)現(xiàn)回調(diào)
官方說明 https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/
1、修改k8s yml文件
lifecycle:
preStop:
httpGet:
path: /registerCenter/down
port: 8848
scheme: HTTP
terminationGracePeriodSeconds: 30
2、修改k8s升級策略為滾動升級
滾動: 先啟動新Pod,再停止舊Pod。最短準(zhǔn)備時(shí)間30秒,30秒保證新的pod啟動完成。
二、微服務(wù)
1、增加回調(diào)地址代碼
@RequestMapping("/registerCenter")
public class RegisterCenterController {
@GetMapping("/down")
public String downServer() {
log.info("從注冊中心剔除本服務(wù)");
DiscoveryManager.getInstance().shutdownComponent();
return null;
}
}
2、修改微服務(wù)yml配置
eureka:
instance:
prefer-ip-address: true
lease-expiration-duration-in-seconds: 10
lease-renewal-interval-in-seconds: 5
client:
registry-fetch-interval-seconds: 5
三、eureka注冊中心
修改eurekaServer yml配置,關(guān)閉read-only緩存。
eureka:
instance:
prefer-ip-address: true
server:
enable-self-preservation: false
#response-cache-update-interval-ms: 5000
#renewal-percent-threshold: 0.4
use-read-only-response-cache: false
四、ribbon服務(wù)列表拉取時(shí)間
ribbon每隔30s會去拉取服務(wù)列表,網(wǎng)上一大推的都是如下這種修改。
ribbon.ServerListRefreshInterval=5,經(jīng)測試此配置不能使用。
閱讀了ribbon的源碼,通過如下方式進(jìn)行修改ribbon拉取服務(wù)列表的間隔時(shí)間為3秒。
@Configuration
@Slf4j
public class RibbonConfig {
@Bean
public PollingServerListUpdater pollingServerListUpdater() {
log.info("update ribbon pollingServerList 3s");
return new PollingServerListUpdater(1000, 3000);
}
}