实现证书管理

This commit is contained in:
GoEdgeLab
2020-09-30 17:46:49 +08:00
parent 17fc8295c4
commit 89c52b2730

View File

@@ -17,8 +17,8 @@ import (
) )
type RPCClient struct { type RPCClient struct {
apiConfig *configs.APIConfig apiConfig *configs.APIConfig
nodeClients []pb.NodeServiceClient conns []*grpc.ClientConn
} }
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { 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") return nil, errors.New("api config should not be nil")
} }
nodeClients := []pb.NodeServiceClient{}
conns := []*grpc.ClientConn{} conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints { for _, endpoint := range apiConfig.RPC.Endpoints {
conn, err := grpc.Dial(endpoint, grpc.WithInsecure()) 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") return nil, errors.New("[RPC]no available endpoints")
} }
// node clients
for _, conn := range conns {
nodeClients = append(nodeClients, pb.NewNodeServiceClient(conn))
}
return &RPCClient{ return &RPCClient{
apiConfig: apiConfig, apiConfig: apiConfig,
nodeClients: nodeClients, conns: conns,
}, nil }, nil
} }
func (this *RPCClient) NodeRPC() pb.NodeServiceClient { func (this *RPCClient) NodeRPC() pb.NodeServiceClient {
if len(this.nodeClients) > 0 { return pb.NewNodeServiceClient(this.pickConn())
return this.nodeClients[rands.Int(0, len(this.nodeClients)-1)]
}
return nil
} }
func (this *RPCClient) Context() context.Context { 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) ctx = metadata.AppendToOutgoingContext(ctx, "nodeId", this.apiConfig.NodeId, "token", token)
return ctx 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)]
}