Esempio n. 1
0
        /// <summary> 添加心跳包发送器 </summary>
        /// <param name="request"></param>
        /// <param name="callback"></param>
        /// <param name="interval">间隔(秒,默认:5)</param>
        /// <returns></returns>
        public string AddServiceBeat(CreateInstanceRequest request, Action <string> callback, int interval = 5)
        {
            var key = JsonConvert.SerializeObject(request).Md5();

            if (_listeners.TryGetValue(key, out var listener))
            {
                listener.Callbacks.Add(callback);
                return(key);
            }

            var timer = new Timer(async sender => await ConfigBeat(sender), new object[] { key, request, interval },
                                  TimeSpan.FromSeconds(interval), Timeout.InfiniteTimeSpan);

            listener = new NacosListener(timer)
            {
                Callbacks = new List <Action <string> > {
                    callback
                }
            };
            _listeners.TryAdd(key, listener);
            return(key);
        }
Esempio n. 2
0
        public string AddConfig(AddListenerRequest request, Action <string> callback, int interval = 120, int timeout = 30000)
        {
            var key = BuildKey(request);

            if (_listeners.TryGetValue(key, out var listener))
            {
                listener.Callbacks.Add(callback);
                return(key);
            }

            var timer = new Timer(async sender => { await ConfigPolling(sender); },
                                  new object[] { request, interval, timeout },
                                  TimeSpan.FromSeconds(interval), Timeout.InfiniteTimeSpan);

            listener = new NacosListener(timer)
            {
                Callbacks = new List <Action <string> > {
                    callback
                }
            };
            _listeners.TryAdd(key, listener);
            return(key);
        }