日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
五小步快速集成使用sentinel限流

1、環(huán)境和資源準備

sentinel支持許多流控方式,比如:單機限流、熔斷降級、集群限流、系統(tǒng)保護規(guī)則、黑白名單授權(quán)等。

本文介紹如何快速集成使用sentinel,文中以單機限流為例,使用代碼而非控制臺配置的方式限流。

  • sentinel官網(wǎng)地址:https://sentinelguard.io/zh-cn/index.html
  • github地址:https://github.com/alibaba/Sentinel
  • 本文采用的版本是1.8.0,下載地址:https://github.com/alibaba/Sentinel/releases/tag/v1.8.0
  • sentinel-dashboard下載地址:https://github.com/alibaba/Sentinel/releases/download/v1.8.0/sentinel-dashboard-1.8.0.jar
  • 本文使用的項目地址:https://github.com/yclxiao/spring-sentinel-demo,代碼中有一部分使用的是官方demo。

2、啟動sentinel-dashboard

從上文地址下載sentinel-dashboard,然后執(zhí)行命令啟動:java -jar sentinel-dashboard-1.8.0.jar

啟動完畢后,通過http://localhost:8080/#/dashboard訪問dashboard,出現(xiàn)如下界面:

3、項目集成sentinel

項目中集成sentinel分如下5步。

3.1、引入pom



  com.alibaba.csp
  sentinel-core
  1.8.0



  com.alibaba.csp
  sentinel-transport-simple-http
  1.8.0



  com.alibaba.csp
  sentinel-annotation-aspectj
  1.8.0

3.2、增加sentinel-aop

@Configuration
public class AopConfiguration {
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

3.3、增加sentinel.properties配置

在application.properties同級目錄下,增加sentinel.properties文件,配置內(nèi)容如下:

# 集成到sentinel的項目名稱
project.name=spring-sentinel-demo
# 對應(yīng)的sentinel-dashboard地址
csp.sentinel.dashboard.server=localhost:8080

同時需要加載sentinel.properties配置,有兩種加載方式,選擇一種即可,如下:

3.4、設(shè)置需要被限流的資源

給需要被限流的資源打上注解@SentinelResource,使用方式如下。

  • 默認情況下,超出配置的流控閾值后,直接拋出 FlowException(BlockException) 異常,可以使用blockHandler自定義。
  • fallback用于配置熔斷降級的方法,當發(fā)生慢調(diào)用、異常數(shù)、異常比例數(shù),會調(diào)用fallback方法。
  • 可以針對部分異常情況做忽略處理,不再觸發(fā)熔斷降級。
@Service
public class TestServiceImpl implements top.mangod.springsentineldemo.service.TestService {

    @Override
    @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {top.mangod.springsentineldemo.service.ExceptionUtil.class})
    public void test() {
        System.out.println("Test");
    }

    @Override
    @SentinelResource(value = "hello", fallback = "helloFallback")
    public String hello(long s) {
        if (s < 0) {
            throw new IllegalArgumentException("invalid arg");
        }
        return String.format("Hello at %d", s);
    }

    @Override
    @SentinelResource(value = "helloAnother", defaultFallback = "defaultFallback",
        exceptionsToIgnore = {IllegalStateException.class})
    public String helloAnother(String name) {
        if (name == null || "bad".equals(name)) {
            throw new IllegalArgumentException("oops");
        }
        if ("foo".equals(name)) {
            throw new IllegalStateException("oops");
        }
        return "Hello, " + name;
    }

    public String helloFallback(long s, Throwable ex) {
        // Do some log here.
        ex.printStackTrace();
        return "Oops, error occurred at " + s;
    }

    public String defaultFallback() {
        System.out.println("Go to default fallback");
        return "default_fallback";
    }
}

3.5、指定和加載流控規(guī)則

文中我使用代碼方式制定流控規(guī)則,在控制臺中也可以直接配置流控規(guī)則,為什么不使用控制臺方式呢?

如果是類似云原生的部署環(huán)境,比如:將spring應(yīng)用打成docker鏡像,然后在部署到Kubernetes中,部署之后Pod地址是會變化。

只要應(yīng)用的地址變化后,之前的配置就消失了。不可能每次地址變化后都到控制臺去重新配置策略,所以需要選擇代碼方式制定規(guī)則。

流控規(guī)則一般會有如下幾個:

  • 資源限流規(guī)則FlowRule
  • 異常熔斷降級規(guī)則DegradeRule
  • 系統(tǒng)過載保護規(guī)則SystemRule
  • 訪問黑白名單規(guī)則AuthorityRule

控制臺設(shè)置流控規(guī)則,如下:

代碼制定和加載流控規(guī)則,如下:

public static void main(String[] args) {
    // 加載限流規(guī)則
    initSentinelRule();
    SpringApplication.run(SpringSentinelDemoApplication.class, args);
  }

  private static void initSentinelRule() {
    // 資源限流
    FlowRule flowRule = new FlowRule("test")
        .setCount(1)
        .setGrade(RuleConstant.FLOW_GRADE_QPS);
    List list = new ArrayList<>();
        /*if (method) {
            FlowRule flowRule1 = new FlowRule("test:sayHello(java.lang.String)")
                    .setCount(5)
                    .setGrade(RuleConstant.FLOW_GRADE_QPS);
            list.add(flowRule1);
        }*/
    list.add(flowRule);
    FlowRuleManager.loadRules(list);

    // 異常降級
    /*List DegradeRules = new ArrayList<>();
    DegradeRule degradeRule = new DegradeRule("");
    degradeRule.setGrade(CircuitBreakerStrategy.ERROR_RATIO.getType());
    degradeRule.setCount(0.7); // Threshold is 70% error ratio
    degradeRule.setMinRequestAmount(100)
            .setStatIntervalMs(30000) // 30s
            .setTimeWindow(10);
    DegradeRules.add(degradeRule);
    DegradeRuleManager.loadRules(DegradeRules);*/

    // 系統(tǒng)負載保護
    /*List systemRules = new ArrayList<>();
    SystemRule systemRule = new SystemRule();
    systemRule.setHighestSystemLoad(10);
    systemRules.add(systemRule);
    SystemRuleManager.loadRules(systemRules);*/
    
    // 黑白名單授權(quán)訪問
    /*AuthorityRule rule = new AuthorityRule();
    rule.setResource("test");
    rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
    rule.setLimitApp("appA,appB");
    AuthorityRuleManager.loadRules(Collections.singletonList(rule));*/
  }

4、啟動測試

項目啟動完畢后,訪問鏈接http://localhost:9091/foo,就可以在控制臺上看到被限流的資源

在流控規(guī)則這里,可以看到上文中在代碼里設(shè)置的規(guī)則:

啟動項目后,測試限流效果,如下:

  1. 1秒內(nèi)多次訪問http://localhost:9091/foo,可以看到觸發(fā)了限流異常:

  1. 輸入http://localhost:9091/foo?t=-1會觸發(fā)異常。fallback用于配置熔斷降級的方法,當發(fā)生慢調(diào)用、異常數(shù)、異常比例數(shù)時,會調(diào)用fallback方法。
  2. 輸入http://localhost:9091/baz/foo、http://localhost:9091/baz/bad,會分別觸發(fā)異常和默認fallback。

5、總結(jié)

本文主要介紹spring項目如何快速集成sentinel實現(xiàn)系統(tǒng)限流。

首先啟動sentinel-dashboard,然后使用5個簡單步驟即可使用sentinel限流。

在應(yīng)用server的IP地址頻繁變動的場景下,建議使用代碼方式限流。

流控的方式較多,你需要根據(jù)自身的業(yè)務(wù)需求做選擇,我一般情況下選擇單機流控和系統(tǒng)保護。


分享題目:五小步快速集成使用sentinel限流
文章出自:http://www.dlmjj.cn/article/djcgieh.html