Пример #1
0
        /// <summary>
        /// Add beat information.
        /// </summary>
        /// <param name="serviceName">service name </param>
        /// <param name="beatInfo">beat information </param>
        public Task AddBeatInfo(string serviceName, BeatInfo beatInfo)
        {
            _logger.LogInformation("[BEAT] adding beat: {0} to beat map.", beatInfo.ToJsonString());
            string key = BuildKey(serviceName, beatInfo.ip, beatInfo.port);

            if (_dom2Beat.TryRemove(key, out var existBeat))
            {
                existBeat.stopped = true;
            }

            _dom2Beat.AddOrUpdate(key, beatInfo, (x, y) => beatInfo);

            var timer = new Timer(
                async x =>
            {
                var info = x as BeatInfo;
                await BeatTask(info);
            }, beatInfo, beatInfo.period, beatInfo.period);

            _beatTimer.AddOrUpdate(key, timer, (x, y) => timer);

            return(Task.CompletedTask);
        }
Пример #2
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");
            }
        }