亚洲av成人中文无码专区-亚洲三区在线观看内射后入-国产午夜福利精品一区二区三区-亚洲国产天堂久久综合-极品熟妇大蝴蝶20p

每日觀點:Shell腳本實戰(zhàn):Consul 服務(wù)注冊和注銷的最佳實踐

來源:嗶哩嗶哩

業(yè)務(wù)爸爸需求: 寫一個腳本用于向 Consul 注冊或注銷服務(wù)。


(資料圖片)

Consul API 實現(xiàn)服務(wù)的注冊和注銷功能

拿到這個需求,我第一時間想到是用 curl 調(diào)用 Consul API 實現(xiàn)服務(wù)的注冊和注銷功能。 如下:

#!/bin/bashCONSUL_TOKEN="<your_token_here>"CONSUL_URL="http://localhost:8500"function?register_service?{??curl?\????--request?PUT?\????--header?"Content-Type:?application/json"?\????--header?"X-Consul-Token:?${CONSUL_TOKEN}"?\????--data?"@-?${CONSUL_URL}/v1/agent/service/register"?<<EOF{??"ID":?"${1}",??"Name":?"${2}",??"Address":?"${3}",??"Port":?${4}}EOF}function?deregister_service?{??curl?\????--request?PUT?\????--header?"X-Consul-Token:?${CONSUL_TOKEN}"?\????"${CONSUL_URL}/v1/agent/service/deregister/${1}"}#?example?usageregister_service?"my-service"?"My?Service"?"localhost"?8080deregister_service?"my-service"

在這個實現(xiàn)中,我們將認(rèn)證token存儲在CONSUL_TOKEN變量中,并在curl請求中使用--header "X-Consul-Token: ${CONSUL_TOKEN}"來添加它。

這個是可以實現(xiàn)業(yè)務(wù)爸爸的需求, 但是,這種實現(xiàn)有以下幾個問題:

1.?不能批量添加相同的類型的指標(biāo)。

2.?唯一ID需要手動維護(hù)。

3.?沒有配置文件留底,雖然三節(jié)點的consul集群很小概率同時出問題, 但是有留底還是一個很好習(xí)慣。

基于這個幾個點, 寫入下面實現(xiàn)腳本。

register 和 deregister實現(xiàn)

#!/bin/bash#?author?小毛驢#?SHELL_DIR=$(cd?$(dirname?$0);pwd)BACKUP_DIR=${SHELL_DIR}"/consulbackup"SOURCEFILE=${SHELL_DIR}"/origin.json"LOGFILE=${SHELL_DIR}"/add.log"BASESVCRIR=/data/consul/config/TOKENFILE=/data/consul/consul_token.yaml?OPTION=${OPTION:="1"}ENV=${ENV:="prod"}PROJECT=${PROJECT:="fb"}MODULE=${MODULE:="api"}SERVICENAME=${SERVICENAME:="node-exporter"}NAME=${NAME:="core"}TAG=${TAG:="node-exporter"}IPADDRS=""IPADDR=""PORT=${PORT:="9520"}LEGAL_IP="^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"if?[?!?-e?"${BACKUP_DIR}"?];then????mkdir?-p?${BACKUP_DIR}fifunction?usage(){????????echo?-e?"\nUsage:?$0?<option>?<value>"????????echo?-e?"\n?option:"????????echo?-e?"\n?-o,--option????option?name,?default?1:add?,?2:delete"????????echo?-e?"\n?-e,--env???????env?name,?default?prod"????????echo?-e?"\n?-p,--project???project?name,?default?fb"????????echo?-e?"\n?-m,--module????module?name,?default?api"????????echo?-e?"\n?-n,--name??????api?name,?default?core"????????echo?-e?"\n?-s,--svcname???serviceName?name,?default?node-exporter"????????echo?-e?"\n?-t,--tag???????tag?name,?default?node-exporter"????????echo?-e?"\n?-a,--address???ip?address,?eg?1.1.1.1|2.2.2.2?default?''?[Require]"????????echo?-e?"\n?-i,--interface?interface?num?default?9520"????????echo?-e?"\n?-h,--help??????help"}function?checkIp(){????#正則表達(dá)式檢驗?ip?地址????[[??$1?=~?$LEGAL_IP?]]????if?[?$??-gt?0?];?then????????echo?"$1?is?an?error?ip?fromat"????????exit?10????fi}function?svcDeregister()?{????if?[?!?-e?"$1"?];then????????echo?"$1?not?exit,?please?check!"????else????????/bin/consul?services?deregister??-token-file=${TOKENFILE}??$1????fi}function?svcRegister()?{????if?[?!?-e?"$1"?];then????????echo?"$1?not?exit,?please?check!"????else????????/bin/consul?services?register??-token-file=${TOKENFILE}?$1????fi}function?genJson()?{cat?>?"${SERVICENAME}-${ENV}-${PROJECT}-${MODULE}-${NAME}-${IPADDR}.json"?<<EOF{??"service":?{????"id":?"${SERVICENAME}-${ENV}-${PROJECT}-${MODULE}-${NAME}-${IPADDR}",????"name":?"${SERVICENAME}",????"tags":?[???????"${TAG}"????],????"address":?"${IPADDR}",????"port":?${PORT},????"meta":?{??????"name":?"${NAME}-${IPADDR}",??????"module":?"${MODULE}",??????"project":?"${PROJECT}",??????"env":?"${ENV}"????},????"check":?{??????"http":?"http://${IPADDR}:${PORT}/metrics",??????"interval":?"10s"????},????"weights":?{??????"passing":?10,??????"warning":?1????}??}}EOF}function?removeJson()?{????mv?"${SERVICENAME}-${ENV}-${PROJECT}-${MODULE}-${NAME}-${IPADDR}.json"??${BACKUP_DIR}}function?main()?{????if?(($#?==?0));then????????usage????????exit????fi????cd?${SHELL_DIR}????while?[?-n?"$1"?];do????????case?$1?in?????????-o|--option?)????????????OPTION=$2????????????shift????????????;;????????-e|--env?)????????????ENV=$2????????????shift????????????;;????????-p|--project?)????????????PROJECT=$2????????????shift????????????;;????????-m|--module??)????????????MODULE=$2????????????shift????????????;;????????-n|--name?)????????????NAME=$2????????????shift????????????;;????????-s|--svcname?)????????????SERVICENAME=$2????????????shift????????????;;????????-t|--tag?)????????????TAG=$2????????????shift????????????;;????????-a|--address?)????????????IPADDRS=$2????????????shift????????????;;????????-i|--interface?)????????????PORT=$2????????????shift????????????;;????????-h|--help?)????????????usage????????????exit????????????;;????????*)????????????echo?-e?"\n$1?unknow?an?option"????????????usage????????????exit????????esac????????shift?????done????if?[?"${IPADDRS}"x?==?""x?];then????????echo?-e?"\n-a|--address?option?is?Require"????????usage????????exit????fi????SVCRIR="${BASESVCRIR}/${SERVICENAME}"????if?[?!?-e?"${SVCRIR}"?];then????????mkdir?-p?${SVCRIR}????fi????cd?${SVCRIR}????hosts=($(echo?${IPADDRS}?|?sed?'s/|/?/g'))????for((?i=0;i<${#hosts[@]};i++))?????do????????echo?${hosts[$i]}????????checkIp?${hosts[$i]}????????IPADDR="${hosts[$i]}"????????if?[[?"${OPTION}"x?==?"1"x??]];?then????????????genJson????????????#statements????????????svcRegister?"${SERVICENAME}-${ENV}-${PROJECT}-${MODULE}-${NAME}-${IPADDR}.json"?????????fi????????if?[[?"${OPTION}"x?==?"2"x??]];?then????????????#statements????????????svcDeregister?"${SERVICENAME}-${ENV}-${PROJECT}-${MODULE}-${NAME}-${IPADDR}.json"?????????????removeJson????????fi????done?}main?$@

這個腳本首先定義了BACKUP_DIR,BASESVCRIR 和TOKENFILE 等路徑。

用法

./register-service.sh?[OPTIONS]參數(shù)-h,?--help:?顯示幫助信息并退出。-o,?-option:?1?register?;?2?deregister-e,--env???????環(huán)境標(biāo)識,?default?prod-p,--project??項目名字,?default?fb-m,--module???業(yè)務(wù)模塊名,?default?api-n,--name??????模塊下面的應(yīng)用名,?default?core-s,--svcname???service?名字,?default?node-exporter-t,--tag???????tag?名字,?default?node-exporter-a,--address???需要注冊到該服務(wù)的地址,多個以“|”分開,,?eg?1.1.1.1|2.2.2.2?default?''?[Require],必須提供-i,--interface??需要注冊的端口?9520

./register-service.sh -o 1 -e prod -p fb -m ops -n consul-ops -s consul_exporter-t consul_exporter -a "10.2.0.6|10.2.0.10|10.2.0.41" -i 9107

標(biāo)簽:

推薦

財富更多》

動態(tài)更多》

熱點