public static void RegisterCustomOperation(ICustomOperation customOperation, string servicePath) { if (customOperation == null || string.IsNullOrWhiteSpace(customOperation.OperationRestPath)) { return; } servicePath = string.IsNullOrWhiteSpace(servicePath) ? ServiceMetadata.DefaultServicePath : servicePath.Trim().ToLower(); if (!EndpointHost.Config.MetadataMap.ContainsKey(servicePath)) { throw new Exception(string.Format("Make sure the service path '{0}' is existing in the apphost.", servicePath)); } string restPath = customOperation.OperationRestPath.Trim().ToLower(); if (!CustomOperations.ContainsKey(servicePath)) { CustomOperations[servicePath] = new Dictionary <string, ICustomOperation>(); } CustomOperations[servicePath][restPath] = customOperation; WhiteListPlugin.ExcludePathController(restPath, servicePath); }
void SyncDynamicSettings(object sender, ElapsedEventArgs e) { if (_isPolicySyncRunning) { return; } _isPolicySyncRunning = true; try { if (string.IsNullOrWhiteSpace(_policyServiceUrl)) { AntFxConfigWebServiceUtils.SyncConfig(); } if (string.IsNullOrWhiteSpace(_policyServiceUrl)) { return; } if (_policyServiceClient == null) { _policyServiceClient = PolicyServiceClient.GetInstance(_policyServiceUrl); _policyServiceClient.Format = _dataTransferFormat;//"json" _policyServiceClient.Timeout = TimeSpan.FromSeconds(100); _policyServiceClient.ReadWriteTimeout = TimeSpan.FromSeconds(300); _policyServiceClient.EnableCHystrixSupport = false; } foreach (ServiceMetadata metadata in EndpointHost.Config.MetadataMap.Values) { if (!_serviceEnabledMap[metadata.ServicePath]) { continue; } //获取每个服务的配置 一般都是一个服务 GetServiceSettingsResponseType policySyncResponse = null; try { policySyncResponse = _policyServiceClient.GetServiceSettings(new GetServiceSettingsRequestType() { ServiceName = metadata.ServiceName, ServiceNamespace = metadata.ServiceNamespace }); } catch (Exception ex) { _log.Error(SyncErrorTitle, ex, new Dictionary <string, string> { { "ErrorCode", "FXD300014" }, { "ServiceName", metadata.ServiceName }, { "ServiceNamespace", metadata.ServiceNamespace } }); continue; } try { metadata.LogErrorWithRequestInfo = policySyncResponse.ServiceSettings.LogErrorWithRequestInfo; metadata.LogCommonRequestInfo = policySyncResponse.ServiceSettings.LogCommonRequestInfo; metadata.CircuitBreakerForceClosed = policySyncResponse.ServiceSettings.CircuitBreakerForceClosed; Dictionary <string, TimeSpan> timeoutSettings = GenerateOperationTimeoutDictionary(policySyncResponse.ServiceSettings.OperationTimeoutSettings); foreach (Operation operation in metadata.Operations) { //把每个方法的电路设置为连接的状态 operation.HystrixCommand.Properties.CircuitBreakerForceClosed.Set(metadata.CircuitBreakerForceClosed); string operationName = operation.Name.ToLower(); if (timeoutSettings.ContainsKey(operationName)) { operation.HystrixCommand.Properties.ExecutionIsolationThreadTimeout.Set(timeoutSettings[operationName]); } } foreach (IPlugin plugin in EndpointHost.Plugins) { if (plugin is WhiteListPlugin) { bool enabled; List <string> newWhiteList; if (plugin is AppIdWhiteListPlugin) { enabled = policySyncResponse.ServiceSettings.EnableAppIdWhiteListCheck; newWhiteList = policySyncResponse.ServiceSettings.AppIdWhiteList; } else if (plugin is IPWhiteListPlugin) { enabled = policySyncResponse.ServiceSettings.EnableIPWhiteListCheck; newWhiteList = policySyncResponse.ServiceSettings.IPWhiteList; } else { continue; } WhiteListPlugin whiteListPlugin = plugin as WhiteListPlugin; whiteListPlugin.Refresh(metadata.ServicePath, enabled, newWhiteList); } else if (plugin is BlackListPlugin) { bool enabled; List <string> newBlackList; if (plugin is AppIdBlackListPlugin) { enabled = policySyncResponse.ServiceSettings.EnableAppIdBlackListCheck; newBlackList = policySyncResponse.ServiceSettings.AppIdBlackList; } else if (plugin is IPBlackListPlugin) { enabled = policySyncResponse.ServiceSettings.EnableIPBlackListCheck; newBlackList = policySyncResponse.ServiceSettings.IPBlackList; } else { continue; } BlackListPlugin blackListPlugin = plugin as BlackListPlugin; blackListPlugin.Refresh(metadata.ServicePath, enabled, newBlackList); } else if (plugin is RateLimitingPlugin) { bool enabled; int rateLimit; if (plugin is RateLimitingWithMapPlugin) { Dictionary <string, int> rateLimitMap; if (plugin is AppIdRateLimitingPlugin) { enabled = policySyncResponse.ServiceSettings.EnableAppIdRateLimitingCheck; rateLimit = policySyncResponse.ServiceSettings.AppIdRateLimit; rateLimitMap = policySyncResponse.ServiceSettings.AppIdRateLimitSettings.ToDictionary(item => item.AppId, item => item.RateLimit); } else if (plugin is IPRateLimitingPlugin) { enabled = policySyncResponse.ServiceSettings.EnableIPRateLimitingCheck; rateLimit = policySyncResponse.ServiceSettings.IPRateLimit; rateLimitMap = policySyncResponse.ServiceSettings.IPRateLimitSettings.ToDictionary(item => item.IP, item => item.RateLimit); } else { continue; } RateLimitingWithMapPlugin rateLimitingPlugin = plugin as RateLimitingWithMapPlugin; rateLimitingPlugin.Refresh(metadata.ServicePath, enabled, rateLimit, rateLimitMap); continue; } else { if (plugin is ServiceRateLimitingPlugin) { enabled = policySyncResponse.ServiceSettings.EnableServiceRateLimitingCheck; rateLimit = policySyncResponse.ServiceSettings.ServiceRateLimit; } else if (plugin is OperationRateLimitingPlugin) { enabled = policySyncResponse.ServiceSettings.EnableOperationRateLimitingCheck; Dictionary <string, int> operationRateLimitMap = policySyncResponse.ServiceSettings.OperationRateLimitSettings .ToDictionary(i => i.OperationName.Trim().ToLower(), i => i.RateLimit); OperationRateLimitingPlugin operationRateLimitPlugin = plugin as OperationRateLimitingPlugin; operationRateLimitPlugin.Refresh(metadata.ServicePath, enabled, operationRateLimitMap); continue; } else { continue; } RateLimitingPlugin rateLimitingPlugin = plugin as RateLimitingPlugin; rateLimitingPlugin.Refresh(metadata.ServicePath, enabled, rateLimit); } } } } catch (Exception ex) { try { _log.Fatal( "Regression Bug", ex, new Dictionary <string, string> { { "ErrorCode", "FXD300015" }, { "ServiceName", metadata.ServiceName }, { "ServiceNamespace", metadata.ServiceNamespace }, { "Dynamic Policy Response", TypeSerializer.SerializeToString(policySyncResponse) } }); } catch { } } } } catch { } finally { _isPolicySyncRunning = false; } }