官方接口文档
https://tarscloud.github.io/TarsDocs/dev/tars-web-api.html#deploy-server
由于工作原因,上不同的平台, 需要部署很多微服务,如果一个一个服务部署就很消耗体力,于是便写了一个脚本,批量部署
#!/bin/bash
#CGI 节点数量
CGINodeNum=1
#tars服务节点数量
TarsNodeNum=2
#base服务节点数量,设置是0,则自动部署到tars服务节点
BaseNodeNum=1
# 设置 tarsweb 地址 //'http://default.tarsweb.xming6.net/ 的域名 #tarsweb=default.tarsweb.xming6.net
tarsweb=default.tarsweb.xming6.net
# 设置 tarswebIP 绑定 tarsweb 的ip
tarswebIP=192.168.102.43
# 设置 tarswebTicket 在tarsweb 用户中心生成
tarswebTicket=d37c80dd24zz1d295bcea255c73ccd59676293PP
if [ "${tarsweb}" = "" ]; then
echo "请设置 tarsweb 域名 !"
exit 1
fi
if [ "${tarswebIP}" = "" ]; then
echo "请设置 tarswebIP 地址 !"
exit 1
fi
if [ "${tarswebTicket}" = "" ]; then
echo "请设置 tarswebTicket !"
exit 1
fi
## 需要处理的包
tarsSvcList=(
"TARS,Mmdb,tars_go,tars.cmdb.default,MainObj:10017"
"TARS,DOOM,tars_php,tars.tarsphp.tcp,MainObj:10018|DrillTodoObj:10019"
"HTTP,Account,tars_go,tars.go.default,Account.webObj:10075"
"HTTP,kk,tars_nodejs,tars.nodejs.default,webObj:10076"
)
function ServantInfo(){
AppName=$1
AppSvc=$2
SvcType=$3
TemplateName=$4
NodeName=$5
protocol=$6
ServantList=$7
adapters=""
for ServantInfo in ${ServantList[@]}
do
Servant=(${ServantInfo//:/ })
if [ ${#adapters} -gt 0 ]; then
adapters="${adapters},"
fi
adapters="${adapters}{\"obj_name\":\"${Servant[0]}\",\"bind_ip\":\"${NodeName}\",\"port\":\"${Servant[1]}\",\"port_type\":\"tcp\",\"protocol\":\"${protocol}\",\"thread_num\":8,\"max_connections\":200000,\"queuecap\":10000,\"queuetimeout\":60000}"
done
deployBody="{\"application\":\"${AppName}\",\"server_name\":\"${AppSvc}\",\"node_name\":\"${NodeName}\",\"server_type\":\"${SvcType}\",\"template_name\":\"${TemplateName}\",\"enable_set\":false,\"set_name\":\"\",\"set_area\":\"\",\"set_group\":\"\",\"adapters\":[${adapters}]}"
echo ${deployBody}
curl -i -x "" http://${tarswebIP}/api/deploy_server?ticket=${tarswebTicket} -H "Host:${tarsweb}" -H "Content-Type:application/json" -d "${deployBody}"
}
## deployBody
function deployServer(){
AppName=$1
AppSvc=$2
SvcType=$3
TemplateName=$4
ServantString=$5
ServantList=(${ServantString//|/ })
if [ "TNebula" == ${AppName} ]; then
if [ ${BaseNodeNum} -gt 0 ] && [ "Cmdb" == ${AppSvc} -o "Cmdbx" == ${AppSvc} -o "UnifiedAccount" == ${AppSvc} -o "Task" == ${AppSvc} -o "UnifiedPrivilege" == ${AppSvc} ]; then
for((nodeId=0;nodeId<${BaseNodeNum};nodeId++));
do
node_name="tars-node-base-1-${nodeId}.tars-node-svc-base-1"
ServantInfo ${AppName} ${AppSvc} ${SvcType} ${TemplateName} ${node_name} "tars" ${ServantList}
done
else
for((nodeId=0;nodeId<${TarsNodeNum};nodeId++));
do
node_name="tars-node-${nodeId}.tars-node-svc"
ServantInfo ${AppName} ${AppSvc} ${SvcType} ${TemplateName} ${node_name} "tars" ${ServantList}
done
fi
elif [ "TNHTTP" == ${AppName} ]; then
for((nodeId=0;nodeId<${CGINodeNum};nodeId++));
do
node_name="tars-node-cgi-0-${nodeId}.tars-node-svc-cgi-0"
ServantInfo ${AppName} ${AppSvc} ${SvcType} ${TemplateName} ${node_name} "not_tars" ${ServantList}
done
fi
}
for tarsSvc in ${tarsSvcList[@]}
do
tarsSvcInfo=(${tarsSvc//,/ })
echo ${tarsSvcInfo[1]}
deployServer ${tarsSvcInfo[0]} ${tarsSvcInfo[1]} ${tarsSvcInfo[2]} ${tarsSvcInfo[3]} ${tarsSvcInfo[4]}
done