通过Dapr实现一个简单的基于.net的微服务电商系统(十八)——服务保护之多级缓存...
?很久沒有更新dapr系列了。今天帶來的是一個小的組件集成,通過多級緩存框架來實現對服務的緩存保護,依舊是一個簡易的演示以及對其設計原理思路的講解,歡迎大家轉發留言和star
目錄:
一、通過Dapr實現一個簡單的基于.net的微服務電商系統
二、通過Dapr實現一個簡單的基于.net的微服務電商系統(二)——通訊框架講解
三、通過Dapr實現一個簡單的基于.net的微服務電商系統(三)——一步一步教你如何擼Dapr
四、通過Dapr實現一個簡單的基于.net的微服務電商系統(四)——一步一步教你如何擼Dapr之訂閱發布
通過Dapr實現一個簡單的基于.net的微服務電商系統(五)——一步一步教你如何擼Dapr之狀態管理
通過Dapr實現一個簡單的基于.net的微服務電商系統(六)——一步一步教你如何擼Dapr之Actor服務
通過Dapr實現一個簡單的基于.net的微服務電商系統(七)——一步一步教你如何擼Dapr之服務限流
通過Dapr實現一個簡單的基于.net的微服務電商系統(八)——一步一步教你如何擼Dapr之鏈路追蹤
通過Dapr實現一個簡單的基于.net的微服務電商系統(九)——一步一步教你如何擼Dapr之OAuth2授權
通過Dapr實現一個簡單的基于.net的微服務電商系統(九)——一步一步教你如何擼Dapr之OAuth2授權-百度版
通過Dapr實現一個簡單的基于.net的微服務電商系統(十)——一步一步教你如何擼Dapr之綁定
通過Dapr實現一個簡單的基于.net的微服務電商系統(十一)——一步一步教你如何擼Dapr之自動擴/縮容
通過Dapr實現一個簡單的基于.net的微服務電商系統(十二)——istio+dapr構建多運行時服務網格
通過Dapr實現一個簡單的基于.net的微服務電商系統(十三)——istio+dapr構建多運行時服務網格之生產環境部署
通過Dapr實現一個簡單的基于.net的微服務電商系統(十四)——開發環境容器調試小技巧
通過Dapr實現一個簡單的基于.net的微服務電商系統(十五)——集中式接口文檔實現
通過Dapr實現一個簡單的基于.net的微服務電商系統(十六)——dapr+sentinel中間件實現服務保護
通過Dapr實現一個簡單的基于.net的微服務電商系統(十七)——服務保護之動態配置與熱重載
附錄:(如果你覺得對你有用,請給個star)
一、電商Demo地址:https://github.com/sd797994/Oxygen-Dapr.EshopSample
二、通訊框架地址:https://github.com/sd797994/Oxygen-Dapr
首先我們看看最終效果如何,重新拉取代碼并rebuild之后,登錄admin.dapreshop.com:30882在基礎配置新增了兩個模塊,其中swagger文檔只是簡單的對系列15文章中創建的集中式文檔的簡易集成。服務保護配置就是本次新增的部分了,其界面如下:
? 當我們需要保護某個接口時,點擊新增限流規則,并通過下拉選擇我們的服務+路徑即可配置一個規則,點擊保存并重啟網關會自動調用k8s進行component的重載并重啟apigateway。
? 在稍微等待20秒左右網關重啟后(亦可通過使用kubectl get po -n dapreshop | findstr apigateway觀察網關重啟)即可通過并發測試來看看其效果。可以看到正確的對我們的接口產生了保護,也就是10秒內產生了100次左右的有效訪問,剩余的訪問被攔截并返回了429請求過多。
? 在dapr的middleware-sentinel文檔中可以看到還支持熔斷降級、并發隔離、熱點參數等等規則,不過目前測試過發現僅有服務限流規則拒絕類型的限流對dapr有效,其他規則暫時沒有效果,不知道是不是dapr1.2的bug還是什么情況,已經github提了issuesl...
下面簡單講講如何實現熱更新的。首先我們需要在apigateway注入一個空的sentinel config component:
apiVersion: dapr.io/v1alpha1 kind: Component metadata:name: sentinelnamespace: dapreshop spec:type: middleware.http.sentinelversion: v1metadata:- name: appNamevalue: "rules"- name: logDirvalue: "/tmp"- name: flowRulesvalue: >-[]- name: circuitbreakerRulesvalue: >-[]接著我在publicservice實現了熱更新相關代碼,具體代碼在PublicService\Infrastructure\Common\AliSentinel中。通過引入了KubernetesClient的方式通過sdk操作component的讀寫以及deployment的更新。核心代碼如下:
static Kubernetes kubernetes = new Kubernetes(KubernetesClientConfiguration.BuildConfigFromConfigFile(SentinelComponentBaseConfig.kubeconfig));/// <summary>/// 注冊規則/// </summary>/// <param name="aliSentinelConfig"></param>public static async Task RegisterSentinelConfig(SentinelConfigList aliSentinelConfigList){await GetAndSaveSentinelComponent(component =>{component.FlowRules = aliSentinelConfigList.FlowRules.GetDistinct();component.BreakingRules = aliSentinelConfigList.BreakingRules.GetDistinct();});}/// <summary>/// 獲取所有注冊規則/// </summary>/// <returns></returns>public static async Task<SentinelConfigList> GetAll(){var component = await GetDefaultSentinelComponent();return new SentinelConfigList(){FlowRules = component.FlowRules,BreakingRules = component.BreakingRules};}#region 本地方法/// <summary>/// 獲取默認的SentinelComponent/// </summary>/// <returns></returns>static async Task<SentinelComponent> GetDefaultSentinelComponent(){var component = new SentinelComponent();await component.Create(kubernetes);return component;}/// <summary>/// 傳遞委托變更默認SentinelComponent/// </summary>/// <param name="operatorComponent"></param>static async Task GetAndSaveSentinelComponent(Action<SentinelComponent> operatorComponent){var component = await GetDefaultSentinelComponent();operatorComponent(component);component.SetMetaData();Patch(component);ReloadDeploy();}/// <summary>/// Patch SentinelComponent到k8s環境/// </summary>/// <param name="component"></param>static void Patch(SentinelComponent component){var patch = new JsonPatchDocument<SentinelComponent>();patch.Replace(x => x.spec.metadata, component.spec.metadata);kubernetes.PatchNamespacedCustomObject(new V1Patch(patch, V1Patch.PatchType.JsonPatch), SentinelComponentBaseConfig.Group, SentinelComponentBaseConfig.Version, SentinelComponentBaseConfig.NamespaceParameter, SentinelComponentBaseConfig.Plural, SentinelComponentBaseConfig.ComponentName);}/// <summary>/// 重啟相關deploy更新SentinelComponent/// </summary>static void ReloadDeploy(){var deploy = kubernetes.ReadNamespacedDeployment(SentinelComponentBaseConfig.DeploymentName, SentinelComponentBaseConfig.NamespaceParameter);deploy.Spec.Template.Metadata.Annotations[SentinelComponentBaseConfig.restart] = DateTime.UtcNow.ToString("s");var patch = new JsonPatchDocument<V1Deployment>();patch.Replace(e => e.Spec.Template.Metadata.Annotations, deploy.Spec.Template.Metadata.Annotations);kubernetes.PatchNamespacedDeployment(new V1Patch(patch, V1Patch.PatchType.JsonPatch), SentinelComponentBaseConfig.DeploymentName, SentinelComponentBaseConfig.NamespaceParameter);}接著我們在application暴露兩個接口用于get component和save component。在頁面上接入相關接口后即可正確的讀取和寫入component并滾動更新相關k8s資源從而實現熱更新。整個限流流程大致如下:
? 好了,今天的分享就到這里,照例歡迎fork+star~
總結
以上是生活随笔為你收集整理的通过Dapr实现一个简单的基于.net的微服务电商系统(十八)——服务保护之多级缓存...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 理解 Azure AD 安全默认值设置
- 下一篇: 通过Dapr实现一个简单的基于.net的