예제 #1
0
        public static void AddService <T>(T service)
        {
            if (instance is IServiceContainer container)
            {
                container.AddService(typeof(T), service);
            }

            InvalidateCachedServices(); // Invalidate our own cached services
            ServiceChanged?.Invoke();
        }
예제 #2
0
        /// <summary>
        /// 拉取改变
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="consulUrl"></param>
        /// <param name="consulTag"></param>
        /// <returns></returns>
        private async Task PollForChanges(string serviceName, string consulUrl, string consulTag)
        {
            if (_pollForChanges.Contains(serviceName))
            {
                return;
            }
            _pollForChanges.Add(serviceName);

            var client = CreateConsulClient(consulUrl);

            while (true)
            {
                try
                {
                    _lastIndexs.TryGetValue(serviceName, out var lastIndex);
                    var queryOptions = new QueryOptions()
                    {
                        WaitIndex = lastIndex
                    };
                    var res = await client.Health.Service(serviceName, consulTag, true, queryOptions);

                    if (res != null && UpdateLastIndex(serviceName, res))
                    {
                        if (res.Response.Count() == 0)
                        {
                            var ex = new Exception($"get health {serviceName} is null, StatuCode:{res.StatusCode}");
                            LoggerAccessor.Instance.OnLoggerError(new InternalException(GrpcErrorCode.Internal, $"PollForChanges", ex), LogType.ClientLog);
                        }
                        else
                        {
                            ServiceChanged?.Invoke(serviceName, res.Response.Select(q => $"{q.Service.Address}:{q.Service.Port}").ToList());
                        }
                    }
                }
                catch (Exception ex)
                {
                    LoggerAccessor.Instance.OnLoggerError(new InternalException(GrpcErrorCode.Internal, "PollForChanges", ex), LogType.ClientLog);
                }
            }
        }
예제 #3
0
 private void OnServiceChanged(Object sender, EventArgs args)
 {
     ServiceChanged?.Invoke(sender, args);
 }
예제 #4
0
 public static void RemoveService <T>()
 {
     Services.RemoveService(typeof(T));
     InvalidateCachedServices(); // Invalidate our own cached services
     ServiceChanged?.Invoke();
 }