1 Star 1 Fork 0

xjieinfo/xjgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client.go 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
老马 提交于 2021-07-27 11:33 . Context增加TraceInfo
package xjrpc
import (
"context"
"fmt"
"github.com/coreos/etcd/clientv3"
"math/rand"
"net/rpc"
"time"
)
type Client struct {
EtcdEndpoints []string
}
func (this *Client) Call(basePath, servicePath string, args interface{}, reply interface{}) (err error) {
address := this.GetAddress(basePath)
client, err := rpc.DialHTTP("tcp", address)
defer client.Close()
if err != nil {
panic(err.Error())
}
err = client.Call(servicePath, args, reply)
return
}
func (this *Client) GetAddress2(basePath string) string {
switch basePath {
case "/xjrpc/admin":
return "127.0.0.1:12002"
case "/xjrpc/member":
return "127.0.0.1:12003"
case "/xjrpc/product":
return "127.0.0.1:12004"
case "/xjrpc/marketing":
return "127.0.0.1:12008"
default:
return ""
}
}
func (this *Client) GetAddress(basePath string) string {
path := "/xjrpc" + basePath + "/"
list := this.EtcdList(path)
item := list[rand.Int()%len(list)]
l := len(path)
if len(item) > l {
return item[l:]
} else {
return ""
}
}
func (this *Client) EtcdList(key string) []string {
cli, err := clientv3.New(clientv3.Config{
Endpoints: this.EtcdEndpoints,
DialTimeout: 5 * time.Second,
})
if err != nil {
fmt.Println("connect failed, err:", err)
return nil
}
//fmt.Println("connect success")
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
resp, err := cli.Get(ctx, key, clientv3.WithPrefix())
cancel()
if err != nil {
fmt.Println("get failed, err:", err)
return nil
}
list := make([]string, 0)
for _, ev := range resp.Kvs {
key := fmt.Sprintf("%s", ev.Key)
list = append(list, key)
}
return list
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xjieinfo/xjgo.git
git@gitee.com:xjieinfo/xjgo.git
xjieinfo
xjgo
xjgo
v0.7.9

搜索帮助