1)我是在windows10下運行,golang1.13.8
問題描述:
frontend在運行時,總是提示無法查找到key,經檢查代碼發現使用zrpcclient在創建etcd客戶端時候莫名的錯誤,
估計與使用的庫有關。
解決方案:多創建幾次,保證成功:
// 所有的調用都使用"brpc"
func NewZRpcClient(protoName string, conf *ZRpcClientConfig, cb ZPpcClientCallBack) *ZRpcClient {
clients := map[string][]string{}
c := &ZRpcClient{
callback: cb,
}
// 這裏傳進去一個空值,應該後面在watcher中addclient中添加到cgm中
c.clients = net2.NewTcpClientGroupManager(protoName, clients, c)
// Check name
for i := 0; i < len(conf.Clients); i++ {
// service discovery
etcdConfg := clientv3.Config{
Endpoints: conf.Clients[i].EtcdAddrs,
}
watcher := &Watcher{
name: conf.Clients[i].Name,
}
// 這裏的意思是本地的變量watcher裏的 *watcher2.ClientWatcher類型watcher,
watcher.watcher, _ = watcher2.NewClientWatcher("/nebulaim", conf.Clients[i].Name, etcdConfg, c.clients)
// 之前這裏的值在後面用的時候發現是空值
k := 0
for ; k <= 10 && watcher.watcher == nil; {
fmt.Printf("出現了問題,連接etcd錯誤, 重試第 %d 次 \n", k+2)
time.Sleep(1 * time.Second)
watcher.watcher, _ = watcher2.NewClientWatcher("/nebulaim", conf.Clients[i].Name, etcdConfg, c.clients)
k = k + 1
}
if k >= 10 {
fmt.Println("NewClientWatcher中etcd錯誤,試了10次還不行,重啓一次試試")
}
if conf.Clients[i].Balancer == "ketama" {
watcher.ketama = load_balancer.NewKetama(10, nil)
} else {
watcher.ketama = nil
}
c.watchers = append(c.watchers, watcher)
}
return c
}
後面發現,多試一次基本就好了,有人提示在前面加Sleep也可以。
2)待續