最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中基于推、拉模式的sentinel規(guī)則持久化詳解

 更新時(shí)間:2023年09月12日 10:08:26   作者:雪峰.貴  
這篇文章主要介紹了Java中基于推、拉模式的sentinel規(guī)則持久化詳解,推模式是sentinelDashboard?把規(guī)則推給Nacos,Nacos監(jiān)聽規(guī)則的變化推給微服務(wù),拉模式是sentinelDashboard?把規(guī)則直接給微服務(wù),?Nacos定時(shí)的同步微服務(wù)端的規(guī)則,需要的朋友可以參考下

推 模式

sentinelDashboard 把規(guī)則 推給Nacos,Nacos監(jiān)聽規(guī)則的變化推給微服務(wù)

下載sentinel懶人包

執(zhí)行 java -jar sentinel-dashboard.jar

加依賴

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

加配置

spring:
  cloud:
    sentinel:
      datasource:
        # 名稱隨意
        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
        degrade:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade
        system:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-system-rules
            groupId: SENTINEL_GROUP
            rule-type: system
        authority:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-authority-rules
            groupId: SENTINEL_GROUP
            rule-type: authority
        param-flow:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-param-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: param-flow

拉模式

sentinelDashboard 把規(guī)則直接給微服務(wù), Nacos定時(shí)的同步微服務(wù)端的規(guī)則。

加依賴

<dependency>
     <groupId>com.alibaba.csp</groupId>
     <artifactId>sentinel-datasource-extension</artifactId>
 </dependency>

創(chuàng)建FileDataSourceInit

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 lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
 * 拉模式規(guī)則持久化
 *
 * @author itmuch.com
 */
@Slf4j
public class FileDataSourceInit implements InitFunc {
    @Override
    public void init() throws Exception {
        // TIPS: 如果你對(duì)這個(gè)路徑不喜歡,可修改為你喜歡的路徑
        String ruleDir = "E:\\ideaworkspace\\content-center\\src\\main\\resources\\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 paramFlowRulePath = ruleDir + "\\param-flow-rule.json";
        this.mkdirIfNotExits(ruleDir);
        this.createFileIfNotExits(flowRulePath);
        this.createFileIfNotExits(degradeRulePath);
        this.createFileIfNotExits(systemRulePath);
        this.createFileIfNotExits(authorityRulePath);
        this.createFileIfNotExits(paramFlowRulePath);
        // 流控規(guī)則
        ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(
                flowRulePath,
                flowRuleListParser
        );
        // 將可讀數(shù)據(jù)源注冊(cè)至FlowRuleManager
        // 這樣當(dāng)規(guī)則文件發(fā)生變化時(shí),就會(huì)更新規(guī)則到內(nèi)存
        FlowRuleManager.register2Property(flowRuleRDS.getProperty());
        WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
                flowRulePath,
                this::encodeJson
        );
        // 將可寫數(shù)據(jù)源注冊(cè)至transport模塊的WritableDataSourceRegistry中
        // 這樣收到控制臺(tái)推送的規(guī)則時(shí),Sentinel會(huì)先更新到內(nèi)存,然后將規(guī)則寫入到文件中
        WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
        // 降級(jí)規(guī)則
        ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
                degradeRulePath,
                degradeRuleListParser
        );
        DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
        WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
                degradeRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
        // 系統(tǒng)規(guī)則
        ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
                systemRulePath,
                systemRuleListParser
        );
        SystemRuleManager.register2Property(systemRuleRDS.getProperty());
        WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
                systemRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
        // 授權(quán)規(guī)則
        ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
                flowRulePath,
                authorityRuleListParser
        );
        AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
        WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
                authorityRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
        // 熱點(diǎn)參數(shù)規(guī)則
        ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
                paramFlowRulePath,
                paramFlowRuleListParser
        );
        ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
        WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
                paramFlowRulePath,
                this::encodeJson
        );
        ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
    }
    private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<FlowRule>>() {
            }
    );
    private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<DegradeRule>>() {
            }
    );
    private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<SystemRule>>() {
            }
    );
    private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<AuthorityRule>>() {
            }
    );
    private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<ParamFlowRule>>() {
            }
    );
    private void mkdirIfNotExits(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    private void createFileIfNotExits(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            if (!file.createNewFile()) {
                log.warn("文件創(chuàng)建失敗!filePath = {}", filePath);
            }
        }
    }
    private <T> String encodeJson(T t) {
        return JSON.toJSONString(t);
    }
}

 創(chuàng)建目錄與文件

在這里插入圖片描述

文件里寫入FileDataSourceInit 的全路徑,即 包名+類名

到此這篇關(guān)于Java中基于推、拉模式的sentinel規(guī)則持久化詳解的文章就介紹到這了,更多相關(guān)推、拉模式的sentinel規(guī)則持久化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解租約機(jī)制以及在hbase中的應(yīng)用

    詳解租約機(jī)制以及在hbase中的應(yīng)用

    這篇文章主要介紹了詳解租約機(jī)制以及在hbase中的應(yīng)用的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 統(tǒng)一返回JsonResult踩坑的記錄

    統(tǒng)一返回JsonResult踩坑的記錄

    這篇文章主要介紹了統(tǒng)一返回JsonResult踩坑的記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • MyBatis-Plus 分頁插件配置的兩種方式實(shí)現(xiàn)

    MyBatis-Plus 分頁插件配置的兩種方式實(shí)現(xiàn)

    本文主要介紹了MyBatis-Plus 分頁插件配置的兩種方式實(shí)現(xiàn),包括使用PaginationInterceptor和MybatisPlusInterceptor兩種方式,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • Java通過word模板實(shí)現(xiàn)創(chuàng)建word文檔報(bào)告

    Java通過word模板實(shí)現(xiàn)創(chuàng)建word文檔報(bào)告

    這篇文章主要為大家詳細(xì)介紹了Java如何通過word模板實(shí)現(xiàn)創(chuàng)建word文檔報(bào)告的教程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下
    2022-09-09
  • java web將數(shù)據(jù)導(dǎo)出為Excel格式文件代碼片段

    java web將數(shù)據(jù)導(dǎo)出為Excel格式文件代碼片段

    這篇文章主要為大家詳細(xì)介紹了java web將數(shù)據(jù)導(dǎo)出為Excel格式文件代碼片段,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • SpringMVC?RESTFul實(shí)戰(zhàn)案例刪除功能實(shí)現(xiàn)

    SpringMVC?RESTFul實(shí)戰(zhàn)案例刪除功能實(shí)現(xiàn)

    這篇文章主要為大家介紹了SpringMVC?RESTFul實(shí)戰(zhàn)案例刪除功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Mybatis中如何使用sum對(duì)字段求和

    Mybatis中如何使用sum對(duì)字段求和

    這篇文章主要介紹了Mybatis中如何使用sum對(duì)字段求和,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • IDEA如何恢復(fù)刪除模塊pom文件

    IDEA如何恢復(fù)刪除模塊pom文件

    文章介紹了四種恢復(fù)Maven項(xiàng)目模塊的方法,包括手動(dòng)加回、強(qiáng)制重建模塊關(guān)系、取消忽略并重建緩存以及刪除IDEA項(xiàng)目配置,這些方法可以幫助解決Maven項(xiàng)目模塊丟失或被忽略的問題
    2026-02-02
  • Java Stax解析XML示例

    Java Stax解析XML示例

    這篇文章主要介紹了Java Stax解析XML示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09
  • Java編程讀寫鎖詳解

    Java編程讀寫鎖詳解

    本篇文章給大家詳細(xì)分享了Java編程讀寫鎖的相關(guān)原理以及知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以參考下。
    2018-08-08

最新評(píng)論

阳原县| 安化县| 偏关县| 长阳| 宁河县| 武强县| 宁陵县| 苏州市| 昭苏县| 班戈县| 利津县| 西安市| 准格尔旗| 若羌县| 双鸭山市| 炎陵县| 原平市| 黎平县| 车致| 仁化县| 商洛市| 鄢陵县| 罗定市| 绿春县| 阳城县| 信宜市| 德格县| 白城市| 临安市| 佛山市| 龙川县| 温宿县| 维西| 土默特右旗| 正安县| 新津县| 平凉市| 高邑县| 肃南| 曲周县| 定结县|