80 lines
1.8 KiB
Markdown
80 lines
1.8 KiB
Markdown
|
|
## 代码结构
|
|||
|
|
|
|||
|
|
``` shell
|
|||
|
|
├── README.md
|
|||
|
|
├── client.go # grpc客户端调用
|
|||
|
|
├── config.go # 配置结构
|
|||
|
|
├── resolver.go # 地址解析
|
|||
|
|
└── server.go # grpc服务端调用
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 数据结构
|
|||
|
|
|
|||
|
|
### 基本结构
|
|||
|
|
|
|||
|
|
``` golang
|
|||
|
|
type BasicConfig struct {
|
|||
|
|
DialTimeout int `toml:"dial_timeout"` // 连接超时时间,默认5秒
|
|||
|
|
EndPoints []string `toml:"endpoints"` // etcd的地址
|
|||
|
|
Prefix string `toml:"prefix"` // etcd服务的路径前缀,区分环境用
|
|||
|
|
Debug bool `toml:"debug"` // 使用debug模式,打印详细的etcd交互日志,很多,非调试不建议打开
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### grpc服务端用的配置
|
|||
|
|
|
|||
|
|
``` golang
|
|||
|
|
type NameConfig struct {
|
|||
|
|
BasicConfig
|
|||
|
|
Servers []string `toml:"servers"` // 注册的服务名称,可以是多个
|
|||
|
|
Addr string `toml:"addr"` // 服务地址,可以是IP:PORT或者:PORT格式
|
|||
|
|
TTL int64 `toml:"ttl"` // 租约超时时间,默认5秒
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### grpc客户端用的配置
|
|||
|
|
|
|||
|
|
``` golang
|
|||
|
|
type ResolverConfig struct {
|
|||
|
|
BasicConfig
|
|||
|
|
WatchServers []string `toml:"watch_servers"` // 依赖的服务名称
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 使用方法
|
|||
|
|
|
|||
|
|
### grpc服务端
|
|||
|
|
|
|||
|
|
``` golang
|
|||
|
|
// demo
|
|||
|
|
&config.EtcdName = {
|
|||
|
|
EndPoints:[]string{"127.0.0.1:2379"},
|
|||
|
|
Prefix: "/ng/test/okash",
|
|||
|
|
Servers: []string{"orders.Orders", "product.Product"},
|
|||
|
|
Addr: ":8701",
|
|||
|
|
}
|
|||
|
|
etcdName, err := gfetcd.NameRegist(&config.EtcdName)
|
|||
|
|
if err != nil {
|
|||
|
|
panic(err)
|
|||
|
|
}
|
|||
|
|
defer etcdName.Unregister()
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### grpc客户端
|
|||
|
|
|
|||
|
|
```golang
|
|||
|
|
// demo
|
|||
|
|
&config.EtcdReslv{
|
|||
|
|
EndPoints:[]string{"127.0.0.1:2379"},
|
|||
|
|
Prefix: "/ng/test/okash",
|
|||
|
|
WatchServers: []string{"product.Product"},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 连接etcd
|
|||
|
|
EtcdReslv, err = gfetcd.NewResolver(&config.EtcdReslv)
|
|||
|
|
|
|||
|
|
// 调用方法
|
|||
|
|
err := EtcdReslv.Call(context.Background(), &XXXReq{}, &XXXResp{})
|
|||
|
|
```
|
|||
|
|
|