99国产午夜精品一区二区天美,伊人久久电影网,熟妇在线中文字幕,午夜男女无遮挡免费

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

來(lái)源:?jiǎn)袅▎袅?/span>

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


(資料圖片)

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

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

#!/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"

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

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

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

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

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

基于這個(gè)幾個(gè)點(diǎn), 寫入下面實(shí)現(xiàn)腳本。

register 和 deregister實(shí)現(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á)式檢驗(yàn)?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?$@

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

用法

./register-service.sh?[OPTIONS]參數(shù)-h,?--help:?顯示幫助信息并退出。-o,?-option:?1?register?;?2?deregister-e,--env???????環(huán)境標(biāo)識(shí),?default?prod-p,--project??項(xiàng)目名字,?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???需要注冊(cè)到該服務(wù)的地址,多個(gè)以“|”分開(kāi),,?eg?1.1.1.1|2.2.2.2?default?''?[Require],必須提供-i,--interface??需要注冊(cè)的端口?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)簽:

推薦

財(cái)富更多》

動(dòng)態(tài)更多》

熱點(diǎn)