实现证书管理

This commit is contained in:
刘祥超
2020-09-30 17:46:49 +08:00
parent 4218221e1c
commit 83df1be84d

View File

@@ -18,7 +18,7 @@ import (
type RPCClient struct {
apiConfig *configs.APIConfig
nodeClients []pb.NodeServiceClient
conns []*grpc.ClientConn
}
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
@@ -26,8 +26,6 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
return nil, errors.New("api config should not be nil")
}
nodeClients := []pb.NodeServiceClient{}
conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints {
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
@@ -40,22 +38,14 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
return nil, errors.New("[RPC]no available endpoints")
}
// node clients
for _, conn := range conns {
nodeClients = append(nodeClients, pb.NewNodeServiceClient(conn))
}
return &RPCClient{
apiConfig: apiConfig,
nodeClients: nodeClients,
conns: conns,
}, nil
}
func (this *RPCClient) NodeRPC() pb.NodeServiceClient {
if len(this.nodeClients) > 0 {
return this.nodeClients[rands.Int(0, len(this.nodeClients)-1)]
}
return nil
return pb.NewNodeServiceClient(this.pickConn())
}
func (this *RPCClient) Context() context.Context {
@@ -79,3 +69,11 @@ func (this *RPCClient) Context() context.Context {
ctx = metadata.AppendToOutgoingContext(ctx, "nodeId", this.apiConfig.NodeId, "token", token)
return ctx
}
// 随机选择一个连接
func (this *RPCClient) pickConn() *grpc.ClientConn {
if len(this.conns) == 0 {
return nil
}
return this.conns[rands.Int(0, len(this.conns)-1)]
}