Ai
1 Star 0 Fork 0

nggs/micro

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
李文建 提交于 2020-04-04 21:04 +08:00 . 移植microservice的代码
package cluster
import (
"fmt"
"strconv"
"strings"
)
const (
KeySep = ":"
SectionConfig = "config"
SectionInstance = "instance"
SectionPayload = "payload"
)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func GenKey(args ...interface{}) (key string) {
var f string
length := len(args)
for i := 0; i < length; i++ {
if i != length-1 {
f += "%v"
f += KeySep
} else {
f += "%v"
}
}
key = fmt.Sprintf(f, args...)
return
}
func ParseKey(key string) (etcdRoot string, section string, serviceName string, serviceID int, err error) {
var strServiceID string
etcdRoot, section, serviceName, strServiceID, err = ParseKeyS(key)
if err != nil {
return
}
if strServiceID != "" {
if strServiceID != GlobalConfigName {
serviceID, err = strconv.Atoi(strServiceID)
if err != nil {
err = fmt.Errorf("ParseKey fail, %v", err)
return
}
}
}
return
}
func ParseKeyS(key string) (etcdRoot string, section string, serviceName string, strServiceID string, err error) {
parts := strings.Split(key, KeySep)
length := len(parts)
if length >= 4 {
etcdRoot = parts[0]
section = parts[1]
serviceName = parts[2]
strServiceID = parts[3]
} else if length >= 3 {
etcdRoot = parts[0]
section = parts[1]
serviceName = parts[2]
} else if length >= 2 {
etcdRoot = parts[0]
section = parts[1]
} else {
err = fmt.Errorf("ParseKeyS fail, parts not enough")
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/nggs/micro.git
git@gitee.com:nggs/micro.git
nggs
micro
micro
bac99dff65eb

搜索帮助