Beispiel #1
0
        public override RemoteConnection ConnectToServer(RemoteServerInfo serverInfo)
        {
            try
            {
                var channel = new Grpc.Core.Channel(
                    serverInfo.ServerIp,
                    serverInfo.ServerPort,
                    Grpc.Core.ChannelCredentials.Insecure,
                    /* keep config and naming using diff channel */
                    new List <Grpc.Core.ChannelOption> {
                    new Grpc.Core.ChannelOption(GetName(), 1)
                });

                // after nacos alpha2 server check response was changed!!
                var response = ServerCheck(channel);
                if (response == null || response is not ServerCheckResponse scResp)
                {
                    ShuntDownChannel(channel);
                    return(null);
                }

                var streamClient  = new Nacos.BiRequestStream.BiRequestStreamClient(channel);
                var requestClient = new Nacos.Request.RequestClient(channel);

                GrpcConnection grpcConn = new GrpcConnection(serverInfo);
                grpcConn.SetConnectionId(scResp.ConnectionId);

                var streamCall = BindRequestStream(streamClient, grpcConn);

                // stream observer to send response to server
                grpcConn.SetBiRequestStreamClient(streamCall);
                grpcConn.SetRequestClient(requestClient);
                grpcConn.SetChannel(channel);

                // after nacos alpha2 setup request was changed!!
                ConnectionSetupRequest conSetupRequest = new ConnectionSetupRequest
                {
                    ClientVersion = Constants.CLIENT_VERSION,
                    Labels        = labels,
                    Abilities     = clientAbilities,
                    ClientIp      = Utils.NetUtils.LocalIP(),
                    Tenant        = GetTenant()
                };

                grpcConn.SendRequest(conSetupRequest, BuildMeta(conSetupRequest.GetRemoteType()));
                return(grpcConn);
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "[{0}]Fail to connect to server!", this.GetName());
                return(null);
            }
        }
        private CommonResponse ServerCheck(Grpc.Core.ChannelBase channel)
        {
            try
            {
                var payload = GrpcUtils.Convert <object>(new { }, new RequestMeta {
                    Type = RemoteRequestType.Req_ServerCheck
                });

                var client = new Nacos.Request.RequestClient(channel);
                var resp   = client.request(payload);

                var res = GrpcUtils.Parse(resp);
                return((CommonResponse)res);
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "[{0}]Fail to server check!", GetName());
                return(null);
            }
        }
 public void SetRequestClient(Nacos.Request.RequestClient client) => this.reqClient = client;