Exemplo n.º 1
0
        /// <summary>
        /// 截流
        /// </summary>
        public Tuple <bool, string> Throttle(string serviceName, string methodName, bool isThrottle)
        {
            var srv = _consulSrv.GetService(serviceName).Result;

            if (srv == null || srv.Count == 0)
            {
                return(Tuple.Create(false, $"consul中找不到服务({serviceName})"));
            }

            var result = true;
            var msg    = "";

            foreach (var item in srv)
            {
                var channel = new Channel(item.Address, item.Port, ChannelCredentials.Insecure);
                try
                {
                    var client = new BaseServiceClient(channel);
                    var res    = client.AddDelThrottle(new AddDelThrottleRQ
                    {
                        MethodName = methodName,
                        IsDel      = !isThrottle
                    }, deadline: DateTime.UtcNow.AddSeconds(_config.GetValue <int>("GrpcTimeout")));
                }
                catch (Exception ex)
                {
                    result = false;
                    msg   += $"{item.Address};{item.Port}执行失败" + Environment.NewLine;
                    _logger.LogError(ex, ex.Message);
                }
                finally
                {
                    channel.ShutdownAsync().Wait();
                }
            }
            return(Tuple.Create(result, msg));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnGet(string serviceName, string serverAddress = null)
        {
            ServiceName        = serviceName?.Trim();
            CurrentAddressInfo = serverAddress?.Trim();
            AddressInfos       = new List <string> {
                CurrentAddressInfo
            };

            if (string.IsNullOrWhiteSpace(ServiceName) && string.IsNullOrWhiteSpace(CurrentAddressInfo))
            {
                return(RedirectToPage("Error", new { msg = "请指定consul服务名称或Grpc服务地址" }));
            }

            if (!string.IsNullOrWhiteSpace(serverAddress))
            {
                var arr  = serverAddress.Split(':', System.StringSplitOptions.RemoveEmptyEntries);
                var ip   = arr[0];
                var port = int.Parse(arr[1]);

                Info = await _grpcSrv.GetInfo(ip, port);
            }
            else
            {
                var service = await _consulSrv.GetService(ServiceName);

                if (service == null || service.Count == 0)
                {
                    return(RedirectToPage("Error", new { msg = $"consul中找不到服务{ServiceName}" }));
                }
                AddressInfos = service.Select(q => $"{q.Address}:{q.Port}").Distinct().ToList();
                var ip   = service.First().Address;
                var port = service.First().Port;
                CurrentAddressInfo = $"{ip}:{port}";

                Info = await _grpcSrv.GetInfo(ip, port);
            }

            if (Info == null)
            {
                return(RedirectToPage("Error", new { msg = $"consul服务({ServiceName})未集成FM.GrpcExtensions" }));
            }
            return(Page());
        }