想将前端项目注册到nacos中,用来做标签路由,但是又不想再机器上再引入其它组件的情况下可以使用shell调用nacos的接口直接注册,并启动一个死循环保持住心跳即可。
nacos官网 Open-Api
#!/bin/sh
SERVICE_ADDR=127.0.0.1:8848
IP=${POD_IP}
PORT=80
SERVICE_NAME=portal
NAMESPACE_ID=1xxbce4-xxx-xxx-a374-xxxxxxxx
BRANCH=feat1.1
METADATA=$(cat <<EOF
{"preserved.register.source":"shell","branch":"$BRANCH"}
EOF
)
echo $(urlencode $METADATA)
urlencode(){
echo $1 | tr -d '\n' | od -An -tx1 | tr ' ' '%' | tr -d [:cntrl:]
}
#registe params
registerData="serviceName="$SERVICE_NAME
registerData=$registerData"&healthy=false"
registerData=$registerData"&ip="$IP
registerData=$registerData"&port="$PORT
registerData=$registerData"&weight=1.0"
registerData=$registerData"&namespaceId="$NAMESPACE_ID
registerData=$registerData"&metadata="$(urlencode $METADATA)
#beat params
beatParam="serviceName="$SERVICE_NAME
beatParam=$beatParam"&ip="$IP
beatParam=$beatParam"&port="$PORT
beatParam=$beatParam"&namespaceId="$NAMESPACE_ID
beatData=$(cat <<EOF
{"cluster":"DEFAULT","ip":"$IP","port":$PORT,"metadata":$METADATA,"scheduled":true,"serviceName":"$SERVICE_NAME","weight":1}
EOF
)
beat=$(urlencode $beatData)
echo $beat
beatParam=$beatParam"&beat=$beat"
#register
curl -X POST "http://$SERVICE_ADDR/nacos/v1/ns/instance?$registerData"
#keep beat
while true
do
curl -X PUT "http://$SERVICE_ADDR/nacos/v1/ns/instance/beat?$beatParam"
echo ''
sleep 3
done
启动服务返回结果如下
ok{"clientBeatInterval":5000,"code":10200,"lightBeatEnabled":true}
{"clientBeatInterval":5000,"code":10200,"lightBeatEnabled":true}
{"clientBeatInterval":5000,"code":10200,"lightBeatEnabled":true}
{"clientBeatInterval":5000,"code":10200,"lightBeatEnabled":true}
{"clientBeatInterval":5000,"code":10200,"lightBeatEnabled":true}
{"clientBeatInterval":5000,"code":10200,"lightBeatEnabled":true}
{"clientBeatInterval":5000,"code":10200,"lightBeatEnabled":true}