成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[Spring-Cloud-Alibaba] Sentinel 規(guī)則持久化

only_do / 1896人閱讀

摘要:在之前的練習(xí)中,只要應(yīng)用重啟,就需要重新配置,這樣在我們實際的項目是非常不實用的,那么有沒有辦法把我們配置的規(guī)則保存下來呢答案是,那么接下來,給大家來介紹如何將規(guī)則持久化。重新啟動測試效果添加流控規(guī)則查看同步的配置

在之前的練習(xí)中,只要應(yīng)用重啟,就需要重新配置,這樣在我們實際的項目是非常不實用的,那么有沒有辦法把我們配置的規(guī)則保存下來呢?答案是YES,那么接下來,給大家來介紹如何將Sentinel規(guī)則持久化。

Document: 傳送門

File Datasource(文件存儲)

Pull 模式

Push 模式

Nacos configuration

Apollo

File Datasource
Pull 模式
原理:
擴展寫數(shù)據(jù)源(WritableDataSource), 客戶端主動向某個規(guī)則管理中心定期輪詢拉取規(guī)則,這個規(guī)則中心可以是 RDBMS、文件 等
pull 模式的數(shù)據(jù)源(如本地文件、RDBMS 等)一般是可寫入的。使用時需要在客戶端注冊數(shù)據(jù)源:將對應(yīng)的讀數(shù)據(jù)源注冊至對應(yīng)的 RuleManager,將寫數(shù)據(jù)源注冊至 transport 的 WritableDataSourceRegistry 中。

過程如下:

Pull Demo

Step 1: 添加配置

    
        com.alibaba.csp
        sentinel-datasource-extension
    

Step 2: 編寫持久化代碼,實現(xiàn)com.alibaba.csp.sentinel.init.InitFunc

代碼參考自:傳送門

import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.*;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * FileDataSourceInit for : 自定義Sentinel存儲文件數(shù)據(jù)源加載類
 *
 * @author Isaac.Zhang | 若初
 * @since 2019/7/21
 */
public class FileDataSourceInit implements InitFunc {
    @Override
    public void init() throws Exception {
        // TIPS: 如果你對這個路徑不喜歡,可修改為你喜歡的路徑
        String ruleDir = System.getProperty("user.home") + "/sentinel/rules";
        String flowRulePath = ruleDir + "/flow-rule.json";
        String degradeRulePath = ruleDir + "/degrade-rule.json";
        String systemRulePath = ruleDir + "/system-rule.json";
        String authorityRulePath = ruleDir + "/authority-rule.json";
        String hotParamFlowRulePath = ruleDir + "/param-flow-rule.json";

        this.mkdirIfNotExits(ruleDir);
        this.createFileIfNotExits(flowRulePath);
        this.createFileIfNotExits(degradeRulePath);
        this.createFileIfNotExits(systemRulePath);
        this.createFileIfNotExits(authorityRulePath);
        this.createFileIfNotExits(hotParamFlowRulePath);
        // 流控規(guī)則
        ReadableDataSource> flowRuleRDS = new FileRefreshableDataSource<>(
                flowRulePath,
                flowRuleListParser
        );
        // 將可讀數(shù)據(jù)源注冊至FlowRuleManager
        // 這樣當(dāng)規(guī)則文件發(fā)生變化時,就會更新規(guī)則到內(nèi)存
        FlowRuleManager.register2Property(flowRuleRDS.getProperty());
        WritableDataSource> flowRuleWDS = new FileWritableDataSource<>(
                flowRulePath,
                this::encodeJson
        );
        // 將可寫數(shù)據(jù)源注冊至transport模塊的WritableDataSourceRegistry中
        // 這樣收到控制臺推送的規(guī)則時,Sentinel會先更新到內(nèi)存,然后將規(guī)則寫入到文件中
        WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);

        // 降級規(guī)則
        ReadableDataSource> degradeRuleRDS = new FileRefreshableDataSource<>(
                degradeRulePath,
                degradeRuleListParser
        );
        DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
        WritableDataSource> degradeRuleWDS = new FileWritableDataSource<>(
                degradeRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);

        // 系統(tǒng)規(guī)則
        ReadableDataSource> systemRuleRDS = new FileRefreshableDataSource<>(
                systemRulePath,
                systemRuleListParser
        );
        SystemRuleManager.register2Property(systemRuleRDS.getProperty());
        WritableDataSource> systemRuleWDS = new FileWritableDataSource<>(
                systemRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);

        // 授權(quán)規(guī)則
        ReadableDataSource> authorityRuleRDS = new FileRefreshableDataSource<>(
                flowRulePath,
                authorityRuleListParser
        );
        AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
        WritableDataSource> authorityRuleWDS = new FileWritableDataSource<>(
                authorityRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);

        // 熱點參數(shù)規(guī)則
        ReadableDataSource> hotParamFlowRuleRDS = new FileRefreshableDataSource<>(
                hotParamFlowRulePath,
                hotParamFlowRuleListParser
        );
        ParamFlowRuleManager.register2Property(hotParamFlowRuleRDS.getProperty());
        WritableDataSource> paramFlowRuleWDS = new FileWritableDataSource<>(
                hotParamFlowRulePath,
                this::encodeJson
        );
        ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
    }

    /**
     * 流控規(guī)則對象轉(zhuǎn)換
     */
    private Converter> flowRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference>() {
            }
    );
    /**
     * 降級規(guī)則對象轉(zhuǎn)換
     */
    private Converter> degradeRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference>() {
            }
    );
    /**
     * 系統(tǒng)規(guī)則對象轉(zhuǎn)換
     */
    private Converter> systemRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference>() {
            }
    );

    /**
     * 授權(quán)規(guī)則對象轉(zhuǎn)換
     */
    private Converter> authorityRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference>() {
            }
    );

    /**
     * 熱點規(guī)則對象轉(zhuǎn)換
     */
    private Converter> hotParamFlowRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference>() {
            }
    );

    /**
     * 創(chuàng)建目錄
     *
     * @param filePath
     */
    private void mkdirIfNotExits(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 創(chuàng)建文件
     *
     * @param filePath
     * @throws IOException
     */
    private void createFileIfNotExits(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    private  String encodeJson(T t) {
        return JSON.toJSONString(t);
    }
}

Step 3: 啟用上述代碼

resource 目錄下創(chuàng)建 resources/META-INF/services 目錄并創(chuàng)建文件com.alibaba.csp.sentinel.init.InitFunc ,內(nèi)容為:

com.sxzhongf.sharedcenter.configuration.sentinel.datasource.FileDataSourceInit

Pull 優(yōu)缺點

優(yōu)點

簡單,無任何依賴

沒有額外依賴

缺點

不保證一致性(規(guī)則是使用FileRefreshableDataSource定時更新,會有延遲)

實時性不保證(規(guī)則是使用FileRefreshableDataSource定時更新)

拉取過于頻繁也可能會有性能問題

由于文件存儲于本地,容易丟失

參考資料:

ITMUCH

Sentinel WIKI

Push 模式
推薦通過控制臺設(shè)置規(guī)則后將規(guī)則推送到統(tǒng)一的規(guī)則中心,客戶端實現(xiàn) ReadableDataSource接口端監(jiān)聽規(guī)則中心實時獲取變更,流程如下:

實現(xiàn)原理

控制臺推送規(guī)則到Nacos/遠程配置中心

Sentinel client 艦艇Nacos配置變化,更新本地緩存

shared_center service 加工

添加依賴

  
      com.alibaba.csp
      sentinel-datasource-nacos
  

添加配置

spring:
  cloud:
    sentinel:
      datasource:
        sxzhongf_flow:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-flow-rules
            groupId: SENTINEL_GROUP
            # 規(guī)則類型,取值見:org.springframework.cloud.alibaba.sentinel.datasource.RuleType
            rule_type: flow
        sxzhongf_degrade:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade

Sentinel dashboard 加工

Dashboard 規(guī)則改造主要通過2個接口:

com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider & com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher

Download Sentinel Source Code

修改原sentinel-dashboard項目下的POM文件

    
    
        com.alibaba.csp
        sentinel-datasource-nacos
        
        
    

偷懶模式:復(fù)制sentinel-dashboard項目下test下的nacos包(

src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacossrc/main/java/com/alibaba/csp/sentinel/dashboard/rule

修改controller中的默認provider & publisher

com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2

    @Autowired
    // @Qualifier("flowRuleDefaultProvider")
        @Qualifier("flowRuleNacosProvider")
    private DynamicRuleProvider> ruleProvider;
    @Autowired
        // @Qualifier("flowRuleDefaultPublisher")
    @Qualifier("flowRuleNacosPublisher")
    private DynamicRulePublisher> rulePublisher;

打開 /Sentinel-1.6.2/sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html文件,修改代碼:


            
              


---

改為

  
  •   NACOS 流控規(guī)則 V1
  • Dashboard中要修改的代碼已經(jīng)好了。

    重新啟動 Sentinel-dashboard mvn clean package -DskipTests

    測試效果

    Sentinel 添加流控規(guī)則:

    Nacos 查看同步的配置:

    文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

    轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/75534.html

    相關(guān)文章

    • Spring Cloud Alibaba整合Sentinel流控

      摘要:前面我們都是直接通過集成的依賴,通過編碼的方式配置規(guī)則等。對于集成到中阿里已經(jīng)有了一套開源框架,就是用于將一系列的框架成功的整合到中。但這也是在學(xué)習(xí)過程中遇到的一個問題,還是得通過調(diào)試源碼的方式去發(fā)現(xiàn)問題的原因。 前面我們都是直接通過集成sentinel的依賴,通過編碼的方式配置規(guī)則等。對于集成到Spring Cloud中阿里已經(jīng)有了一套開源框架spring-cloud-alibaba...

      ytwman 評論0 收藏0
    • [Spring-Cloud-Alibaba] Sentinel 整合RestTemplate &am

      摘要:開發(fā)階段很有意義。源碼整合配置文件中添加來開啟編寫類,實現(xiàn)默認用戶遠程調(diào)用被限流降級,默認用戶應(yīng)用定義可以拿到異常信息無法拿到異常信息若初啟動應(yīng)用,設(shè)置流控規(guī)則,結(jié)果展示如下默認用戶源碼 Sentinel API Github : WIKI Sphu (指明要保護的資源名稱) Tracer (指明調(diào)用來源,異常統(tǒng)計接口) ContextUtil(標(biāo)示進入調(diào)用鏈入口) 流控規(guī)則(針...

      libin19890520 評論0 收藏0
    • springcloud(二)——spring-cloud-alibaba集成sentinel入門

      摘要:介紹隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來越重要。以流量為切入點,從流量控制熔斷降級系統(tǒng)負載保護等多個維度保護服務(wù)的穩(wěn)定性。完備的實時監(jiān)控同時提供實時的監(jiān)控功能。您只需要引入相應(yīng)的依賴并進行簡單的配置即可快速地接入。 Sentinel 介紹 隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來越重要。 Sentinel 以流量為切入點,從流量控制、熔斷降級、系統(tǒng)負載保護等多個維度...

      darkbug 評論0 收藏0
    • Spring Cloud Alibaba基礎(chǔ)教程:Sentinel使用Nacos存儲規(guī)則

      摘要:所以,在整合了做規(guī)則存儲之后,需要知道在下面兩個地方修改存在不同的效果控制臺中修改規(guī)則僅存在于服務(wù)的內(nèi)存中,不會修改中的配置值,重啟后恢復(fù)原來的值。控制臺中修改規(guī)則服務(wù)的內(nèi)存中規(guī)則會更新,中持久化規(guī)則也會更新,重啟后依然保持。 通過上一篇《使用Sentinel實現(xiàn)接口限流》的介紹,相信大家對Sentinel已經(jīng)有了初步的認識。在Spring Cloud Alibaba的整合封裝之下,接...

      xingqiba 評論0 收藏0
    • 阿里Sentinel控制臺源碼修改-對接Apollo規(guī)則久化

      摘要:改造背景前面我們講解了如何對接來持久化限流的規(guī)則,對接后可以直接通過的后臺進行規(guī)則的修改,推送到各個客戶端實時生效。因此推送規(guī)則正確做法應(yīng)該是配置中心控制臺控制臺配置中心數(shù)據(jù)源,而不是經(jīng)數(shù)據(jù)源推送至配置中心。 改造背景 前面我們講解了如何對接Apollo來持久化限流的規(guī)則,對接后可以直接通過Apollo的后臺進行規(guī)則的修改,推送到各個客戶端實時生效。 但還有一個問題就是Sentinel...

      zhoutao 評論0 收藏0

    發(fā)表評論

    0條評論

    only_do

    |高級講師

    TA的文章

    閱讀更多
    最新活動
    閱讀需要支付1元查看
    <