# README
grpc客户端组件
提供用于 https://github.com/zly-app/zapp 的组件
快速开始
- 创建一个项目
mkdir client && cd client
go mod init client
- 添加
hello/hello.proto
文件
syntax = 'proto3';
package hello; // 决定proto引用路径和rpc路由
option go_package = "server/hello/hello"; // 用于对golang包管理的定位
service helloService{
rpc Say(SayReq) returns (SayResp);
}
message SayReq{
string msg = 1;
}
message SayResp{
string msg = 1;
}
- 编译 proto
protoc \
--go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
hello/hello.proto
- 添加
main.go
文件
package main
import (
"context"
"github.com/zly-app/zapp"
"github.com/zly-app/grpc"
"github.com/zly-app/grpc/example/pb/hello"
)
func main() {
app := zapp.NewApp("grpc-client")
defer app.Exit()
helloClient := hello.NewHelloServiceClient(grpc.GetClientConn("hello")) // 获取客户端. 每次请求都应该尽量重新获取客户端
// 调用
resp, err := helloClient.Say(context.Background(), &hello.SayReq{Msg: "hello"})
if err != nil {
app.Fatal(resp)
}
app.Info("收到结果", resp.GetMsg())
}
- 运行
go mod tidy && go run .
配置文件
添加配置文件 configs/default.yaml
.
最少配置设置
components:
grpc:
hello: # 客户端名
Address: localhost:3000 # 链接地址
完整配置说明
components:
grpc:
hello: # 服务名
Address: localhost:3000 # 服务地址, 参考 https://github.com/zly-app/grpc/tree/master/discover
Balance: weight_consistent_hash # 均衡器, 支持 round_robin, weight_random, weight_hash, weight_consistent_hash
WaitFirstConn: true # 初始化时等待第一个链接
MinIdle: 2 # 最小闲置
MaxIdle: 4 # 最大闲置
MaxActive: 10 # 最大活跃连接数, 小于1表示不限制
BatchIncrement: 4 # 批次增量, 当conn不够时, 一次性最多申请多少个链接
BatchShrink: 4 # 批次缩容, 当conn太多时(超过最大闲置), 一次性最多释放多少个链接
IdleTimeout: 3600 # 空闲链接超时时间, 单位秒, 如果一个连接长时间未使用将被视为连接无效, 小于1表示永不超时
WaitTimeout: 5 # 等待获取连接的超时时间, 单位秒
MaxWaitConnCount: 2000 # 最大等待conn的数量, 小于1表示不限制
ConnectTimeout: 5 # 连接超时, 单位秒
MaxConnLifetime: 3600 # 一个连接最大存活时间, 单位秒, 小于1表示不限制
CheckIdleInterval: 5 # 检查空闲间隔, 单位秒
ProxyAddress: "" # 代理地址. 支持 socks5, socks5h. 示例: socks5://127.0.0.1:1080 socks5://127.0.0.1:1080 socks5://user:[email protected]:1080
TLSCertFile: "" # tls公钥文件路径
TLSDomain: "" # tls签发域名
请求负载均衡
首先你需要选择一个负载均衡器, 通过配置 Balance 设置, 它提供了如下均衡器
- round_robin
轮询器. 每次请求会按顺序选择一个服务节点, 节点轮训完毕后会重新从第一个服务节点开始
- weight_random
加权随机. 允许为节点设置一个权重值, 每次请求会随机选择一个服务节点, 权重越高, 请求时被选中的概率越大.
- weight_hash
加权 hash. 允许为节点设置一个权重值, 每次请求会根据提供的 hashKey
计算 hash 值然后对总权重求余, 余数计算所在的服务节点, 权重越高被选取的机会越大.
在服务节点连接异常时, 会重新编排节点, 导致使用同一个 hashKey
的请求落在不同的服务节点上, 推荐使用 weight_consistent_hash
.
如果在请求时没有设置 hashKey
会降级为加权随机.
- weight_consistent_hash
加权一致性 hash. 允许为节点设置一个权重值, 权重值会作为节点的分片数, 每个分片计算hash值落在一个具有 2^32 个点的环上. 每次请求会根据提供的 hashKey
计算 hash 值落在环的一个点上, 由这个点得到是哪个服务节点的分片而选取这个服务节点.
在服务节点连接异常时, 由这个服务节点负责的环上的点会交给别的服务, 所以只会有部分使用同一个 hashKey
的请求会落在不同的服务节点上, 当服务节点连接恢复后, 这部分请求会回到原来的服务节点.
如果在请求时没有设置 hashKey
会降级为加权随机.
hashKey 设置方式
在请求时增加 grpc.WithHashKey(hashKey string)
选项发送请求.
目标选择
有时候希望能自己决定使用哪个服务节点, 可以在请求时增加 grpc.WithTarget(serverName string)
选项发送请求, 该请求会直接选择设置的服务节点, 但是如果该服务节点连接异常会产生 no instance
错误.
如果未对服务节点设置服务名, 则这个服务节点的默认服务名的值为该服务的 host:端口
, 如: localhost:3000
, 192.168.1.3:3030
服务注册与发现
转到这里