コード例 #1
0
        public async Task <bool> SendHeartbeatAsync(SendHeartbeatRequest request)
        {
            if (request == null)
            {
                throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "request param invalid");
            }

            request.CheckParam();

            var responseMessage = await _clientFactory.DoRequestAsync(HttpMethod.Put, $"{GetBaseUrl()}{RequestPathValue.INSTANCE_BEAT}", request.ToQueryString(), _options.DefaultTimeOut);

            switch (responseMessage.StatusCode)
            {
            case System.Net.HttpStatusCode.OK:
                var result = await responseMessage.Content.ReadAsStringAsync();

                if (result.Equals("ok", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }
                else
                {
                    _logger.LogWarning($"[client.SendHeartbeat] server return {result} ");
                    return(false);
                }

            default:
                _logger.LogWarning($"[client.SendHeartbeat] Send instance beat failed {responseMessage.StatusCode.ToString()}");
                throw new NacosException((int)responseMessage.StatusCode, $"Send instance beat failed {responseMessage.StatusCode.ToString()}");
            }
        }
コード例 #2
0
        public async Task <bool> SendHeartbeatAsync(SendHeartbeatRequest request)
        {
            if (request == null)
            {
                throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "request param invalid");
            }

            request.CheckParam();

            var responseMessage = await _proxy.ReqApiAsync(HttpMethod.Put, RequestPathValue.INSTANCE_BEAT, null, request.ToDict(), _options.DefaultTimeOut);

            switch (responseMessage.StatusCode)
            {
            case System.Net.HttpStatusCode.OK:
                var result = await responseMessage.Content.ReadAsStringAsync();

                var jObj = Newtonsoft.Json.Linq.JObject.Parse(result);

                if (jObj.ContainsKey("code"))
                {
                    int code = int.Parse(jObj["code"].ToString());

                    var flag = code == 10200;

                    if (!flag)
                    {
                        _logger.LogWarning($"[client.SendHeartbeat] server return {result} ");
                    }

                    return(flag);
                }
                else
                {
                    _logger.LogWarning($"[client.SendHeartbeat] server return {result} ");
                    return(false);
                }

            default:
                _logger.LogWarning($"[client.SendHeartbeat] Send instance beat failed {responseMessage.StatusCode.ToString()}");
                throw new NacosException((int)responseMessage.StatusCode, $"Send instance beat failed {responseMessage.StatusCode.ToString()}");
            }
        }
コード例 #3
0
        private async Task BeatTask(BeatInfo beatInfo)
        {
            if (beatInfo.stopped)
            {
                return;
            }

            try
            {
                // send heart beat will register instance
                var request = new SendHeartbeatRequest
                {
                    Ephemeral   = true,
                    ServiceName = beatInfo.serviceName,
                    BeatInfo    = beatInfo,
                    NameSpaceId = _options.Namespace,
                };

                if (request == null)
                {
                    throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "request param invalid");
                }

                request.CheckParam();

                var responseMessage = await _proxy.ReqApiAsync(HttpMethod.Put, RequestPathValue.INSTANCE_BEAT, null, request.ToDict(), _options.DefaultTimeOut);

                switch (responseMessage.StatusCode)
                {
                case System.Net.HttpStatusCode.OK:
                    var result = await responseMessage.Content.ReadAsStringAsync();

                    var jObj = Newtonsoft.Json.Linq.JObject.Parse(result);

                    if (jObj.ContainsKey("code"))
                    {
                        int code = int.Parse(jObj["code"].ToString());

                        var flag = code == 10200;

                        if (!flag)
                        {
                            _logger.LogWarning($"[CLIENT-BEAT] server return {result} ");
                        }
                    }
                    else
                    {
                        _logger.LogWarning($"[CLIENT-BEAT] server return {result} ");
                    }

                    break;

                default:
                    _logger.LogWarning("[CLIENT-BEAT] failed to send beat {0}, {1}", beatInfo.ToJsonString(), responseMessage.StatusCode.ToString());
                    throw new NacosException((int)responseMessage.StatusCode, $"Send instance beat failed {responseMessage.StatusCode.ToString()}");
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Send heart beat to Nacos error");
            }
        }