Tars服务容器化

2021-11-25
3分钟阅读时长

tars容器化逻辑

tars 协议文件

module tars
{
    struct AdapterConf{
        0 require string servant;
        1 require string endpoint;
        2 optional string protocol;
        3 optional int maxConns;
        4 optional int threadNum;
        5 optional int queueCap;
        6 optional int queueTimeout;
    };
    
    struct onStartupReq{
        0 require string nodeName;
        1 require string application;
        2 require string server;
        3 optional string setID;
        4 optional vector<AdapterConf> adapters;
        5 optional bool disableFlow;
        6 optional string State = "active";
        7 optional string Version;
        8 optional string templateName;
        9 optional string serverType;
    };

    struct onPrestopReq{
        0 require string nodeName;
        1 optional string application;
        2 optional string server;
    };

    struct keepAliveReq{
        0 require string nodeName;
        1 require string state; // inactive or active
        2 optional string application;
        3 optional string server;
    };

    struct RegisterMetricsReq{
        0 require string nodeName;
        1 require string application;
        2 require string server;
        3 require int metricsPort;
    };

    struct GetMetricsAdaptersReq{
        0 optional map<string, string> filter; // enable in feature
    };

    struct MetricsAdapterInfo{
        0 optional vector<string> targets;
        1 optional map<string, string> labels;
    };

    interface tarsregistry
    {
        void onStartup(onStartupReq req);
        void onPrestop(onPrestopReq req);
        void keepAlive(keepAliveReq req);
        void registerMetrics(RegisterMetricsReq req);
        void getMetricsAdapters(GetMetricsAdaptersReq req, out vector<MetricsAdapterInfo> rsp);
    };
};

onStartup

-- 增加 t_node_info  记录			
insert into t_node_info(node_name, node_obj, endpoint_ip, endpoint_port, 
				last_reg_time, last_heartbeat, setting_state, present_state) 
				values(?, ?, ?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), ?, ?)
				ON DUPLICATE KEY UPDATE last_heartbeat = CURRENT_TIMESTAMP()

-- 增加 t_server_conf  记录				    
insert into t_server_conf(
		application, server_name, node_name, patch_version, present_state,
		enable_set, set_name, set_area, set_group, template_name, server_type,
		setting_state, registry_timestamp, patch_time, posttime
	 )
	 values(
		?, ?, ?, ?, ?,
		?, ?, ?, ?, ?, ?,
		"active", CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP()
	 )
	 ON DUPLICATE KEY UPDATE patch_version=?, present_state=?, template_name=?, server_type=?,
	 enable_set=?, set_name=?, set_area=?, set_group=?,
	 setting_state="active", registry_timestamp=CURRENT_TIMESTAMP(), patch_time=CURRENT_TIMESTAMP(), posttime=CURRENT_TIMESTAMP()
	 
-- 增加 t_adapter_conf  记录	
insert into t_adapter_conf(
			application, server_name, node_name,
			adapter_name, servant, thread_num, endpoint, 
			protocol, max_connections, queuecap, queuetimeout
		 )
		 values(
			?, ?, ?,
			?, ?, ?, ?,
			?, ?, ?, ?
		 )
		 ON DUPLICATE KEY UPDATE
		 adapter_name=?, servant=?, thread_num=?, endpoint=?,
		 protocol=?, max_connections=?, queuecap=?, queuetimeout=?

onPrestop

-- 删除 t_adapter_conf 记录
delete from t_adapter_conf where node_name=? and application=? and server_name=?
-- 删除 t_server_conf 记录
delete from t_server_conf where node_name=? and application=? and server_name=?
-- 删除 t_config_files 记录
delete from t_config_files where host<>'' and host=? and server_name=?

keepAlive

-- 更新node监控上报时间
update t_node_info set last_heartbeat = CURRENT_TIMESTAMP() where node_name = ?
-- 更新节点状态
update t_node_info set present_state = ? where node_name = ?
-- 更新服务状态
update t_server_conf set present_state = ? where node_name = ? and application = ? and server_name = ?

清理异常数据[tarscli notify clean n]

-- 获取待清理服务列表
select node_name, application, server_name from t_server_conf where node_name in 
		(select node_name from t_node_info where present_state='inactive' and last_heartbeat<?)
-- 获取待清理节点列表		
select node_name from t_node_info where present_state='inactive' and last_heartbeat<? 
	and node_name not in (select node_name from t_server_conf)

-- 清理无效服务列表
delete from t_server_conf where node_name in 
	(select node_name from t_node_info where present_state='inactive' and last_heartbeat<?)
-- 清理 t_adapter_conf
delete from t_adapter_conf where node_name in 
	(select node_name from t_node_info where present_state='inactive' and last_heartbeat<?)
-- 清理无效配置记录
delete from t_config_files where host<>'' and host in 
	(select node_name from t_node_info where present_state='inactive' and last_heartbeat<?)
-- 清理无效节点
delete from t_node_info where present_state='inactive' and last_heartbeat<? 
	and node_name not in (select node_name from t_server_conf)

tarscli

# 1.生成服务配置文件
# 2.读取配置,[生成workerId],调用 onStartup 注册服务,服务状态初始化为 activating
# 3.启动tarsnode 
# 4.定时检测tarsnode保活和调用 keepAlive 上报服务状态
tarscli supervisor

# 1.读取配置,2.服务就绪检测(主要检测服务端口)
tarscli hzcheck

# 1.调用 keepAlive 修改服务状态为 deactivating 
# 2.给服务发送 prestop 管理命令
# 3.等待 N 秒后, 调用 onPrestop 销毁注册服务,目前k8s最多等待29s
tarscli prestop

# 1.生成服务配置文件
tarscli genconf

TarsGo 调用链支持测试

helm repo add tars-stable https://tarscloud.github.io/TarsDocker/charts/stable

kubectl create namespace tars-test

helm install tars-test tars-stable/tars --namespace tars-test \
    --set tars.namespace=tars-test,tars.replicas=1,tarsnode.replicas=1,tars.host=domain.com,tars.port=6080,tars.data=/data/shared/tars-data,mysql.data=/data/shared/mysql-data,mysql.rebuild=false

helm install tars-test tars-stable/tars --namespace tars-test     --set tars.namespace=tars-test,tars.replicas=1,tarsnode.replicas=1,tars.host=domain.com,tars.port=6080,tars.data=/data/shared/tars-data,mysql.data=/data/shared/mysql-data,mysql.rebuild=false

curl http://10.0.0.224:8200/json/HelloWorld.PHPTarsuser.Index/jsonfunc \
-d '{"name":"postman","req":{"x":100,"y":200,"name":"postman"}}' -vvv


curl http://10.0.0.224:8200/json/Trace.TarsTraceFrontServer.FrontendObj/Add -d '{"a":1, "b":2}' -vvv; echo ""

curl http://10.0.0.224:8200/json/Trace.TarsTraceFrontServer.FrontendObj/jsonfunc \
-d '{
	"name": "postman",
	"req": {
		"x":100,
		"y":200,
		"name": "postman"
	}
}'