原理:curl模拟post和put请求,定时发送心跳?
bash register_nacos.sh
#!/bin/bash
SERVICE_ADDR=127.0.0.1:8848
IP=127.0.0.1
PORT=9999
SERVICE_NAME=name
GROUP_NAME=DEFAULT_GROUP
USER=nacos
PASS=nacos
urlencode(){
echo $1 | tr -d '\n' | od -An -tx1 | tr ' ' '%' | tr -d [:cntrl:]
}
postdata="a=1"
declare -A map=(["serviceName"]="$SERVICE_NAME" ["groupName"]="$GROUP_NAME" ["username"]="$USER" ["password"]="$PASS")
for key in ${!map[@]}
do
postdata=$postdata"&"$key"="${map[$key]}
done
declare -A beatmap=(["serviceName"]="$SERVICE_NAME" ["ip"]="$IP" ["port"]="$PORT" ["weight"]="1.0" ["ephemera"]="true" ["healthy"]="true")
beatstr=""
for key in ${!beatmap[@]}
do
beatstr=$beatstr'"'$key'":"'${beatmap[$key]}'",'
postdata=$postdata"&"$key"="${beatmap[$key]}
done
beatstr="{"${beatstr:0:$(expr length $beatstr)-1}"}"
beatstr=$(urlencode $beatstr)
postdata=$postdata"&beat=$beatstr"
curl -s -S -X POST -d "$postdata" "http://$SERVICE_ADDR/nacos/v1/ns/instance"
while true
do
curl -s -S -X PUT -d "$postdata" "http://$SERVICE_ADDR/nacos/v1/ns/instance/beat"
echo ''
sleep 3
done
由于alpine不支持数组格式,所以改为枚举参数变量的形式
sh?register_nacos_apline.sh
#!/bin/sh
#sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
#apk add curl
SERVICE_ADDR=127.0.0.1:8848
IP=127.0.0.1
PORT=9999
SERVICE_NAME=name
GROUP_NAME=DEFAULT_GROUP
USER=nacos
PASS=nacos
urlencode(){
echo $1 | tr -d '\n' | od -An -tx1 | tr ' ' '%' | tr -d [:cntrl:]
}
postdata="serviceName="$SERVICE_NAME
postdata=$postdata"&groupName="$GROUP_NAME
postdata=$postdata"&username="$USER
postdata=$postdata"&password="$PASS
postdata=$postdata"&ip="$IP
postdata=$postdata"&port="$PORT
postdata=$postdata"&weight=1.0"
postdata=$postdata"&ephemera=true"
postdata=$postdata"&healthy=true"
beatstr='"serviceName":"'$SERVICE_NAME'",'
beatstr=$beatstr'"ip":"'$IP'",'
beatstr=$beatstr'"port":"'$PORT'",'
beatstr=$beatstr'"ephemera":"true"'
beatstr="{"$beatstr"}"
beatstr=$(urlencode $beatstr)
postdata=$postdata"&beat=$beatstr"
curl -s -S -X POST -d "$postdata" "http://$SERVICE_ADDR/nacos/v1/ns/instance"
while true
do
curl -s -S -X PUT -d "$postdata" "http://$SERVICE_ADDR/nacos/v1/ns/instance/beat"
echo ''
sleep 3
done
参考资料:
解决在alpine下无法使用数组表达式的问题 - 简书
shell 下 urlencode/urldecode 编码/解码的方法 - Boblim - 博客园
shell中使用map_uxff的博客-CSDN博客_shell脚本map
【shell】shell获取函数字符串返回值_bible_reader的博客-CSDN博客_shell 返回字符串
|