TARS 服务优雅关闭
TARS是腾讯开源的微服务调用框架,框架基础设施(RegistryServer,NodeServer,ConfigServer等)是由C++编写,框架上的微服务支持C++、Java、Go等。在众多介绍TARS的使用文章中都是介绍服务的开发及部署,没有文章介绍服务如何进行优雅关闭,优雅关闭对于生产环境尤为重要,不支持优雅关闭将在服务升级或服务实例调整时对业务产生影响。本文将讲通过分析TARS框架源码介绍一种实现优雅关闭的方式。
TARS服务关闭逻辑
AdminRegistryServer
WEB调用AdminRegistryServer的stopServer方法停止服务,stopServer代码比较简单概括说此方法做了两件事:
- 更新数据库服务状态(RegistryServer查询服务数据库)
- 通知tarsnode关闭服务,stopServer方法
NodeServer
NodeServer是结束进程的实施者,AdminRegistryServer通过调用NodeServer接口方法stopServer关闭服务,在stopServer中由CommandStop类来处理具体的服务进程停止操作,我们看一下CommandStop的核心方法executor的源代码:
int CommandStop::execute(string& sResult) { bool needWait = false; ........... string sStopScript = _serverObjectPtr->getStopScript(); //配置了脚本或者非tars服务 if( TC_Common::lower(TC_Common::trim(_desc.serverType)) == "tars_php" ){ 生成停止shell脚本 ...............
}else if (!sStopScript.empty() || _serverObjectPtr->isTarsServer() == false) { 如果设置了停止脚本,则运行启动脚本 map<string, string> mResult; _serverObjectPtr->getActivator()->doScript(sStopScript, sResult, mResult); 设置需要等待 needWait = true |