可以直接在节点启动时自动注册节点

This commit is contained in:
GoEdgeLab
2020-10-17 11:14:40 +08:00
parent 8ea2cc9a64
commit f1ffe8c438
7 changed files with 130 additions and 4 deletions

View File

@@ -72,6 +72,7 @@ func (this *RPCClient) HTTPAccessLogRPC() pb.HTTPAccessLogServiceClient {
return pb.NewHTTPAccessLogServiceClient(this.pickConn())
}
// 节点上下文信息
func (this *RPCClient) Context() context.Context {
ctx := context.Background()
m := maps.Map{
@@ -94,6 +95,29 @@ func (this *RPCClient) Context() context.Context {
return ctx
}
// 集群上下文
func (this *RPCClient) ClusterContext(clusterId string, clusterSecret string) context.Context {
ctx := context.Background()
m := maps.Map{
"timestamp": time.Now().Unix(),
"type": "cluster",
"userId": 0,
}
method, err := encrypt.NewMethodInstance(teaconst.EncryptMethod, clusterSecret, clusterId)
if err != nil {
utils.PrintError(err)
return context.Background()
}
data, err := method.Encrypt(m.AsJSON())
if err != nil {
utils.PrintError(err)
return context.Background()
}
token := base64.StdEncoding.EncodeToString(data)
ctx = metadata.AppendToOutgoingContext(ctx, "nodeId", clusterId, "token", token)
return ctx
}
// 随机选择一个连接
func (this *RPCClient) pickConn() *grpc.ClientConn {
if len(this.conns) == 0 {