首頁 > 軟體

sentinel 整合spring cloud限流的過程解析

2022-03-28 19:00:20

spring cloud基於http進行服務呼叫,大致過程如下:

  • 服務提供端:提供http介面,並向服務中心註冊服務資訊
  • 服務消費端:將伺服器端的http介面作為本地服務,從註冊中心讀取服務提供端資訊,使用feign發起遠端呼叫

相關依賴

           <!-- 服務註冊與發現 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
 
       <!-- sentinel限流 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

範例

為簡化處理,直接對url介面進行限流,不做服務呼叫

application.yml

spring:
  application:
    name: hello-sentinel
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8081

限流可使用本地設定、或者sentinel dashboard設定

HelloController

@RestController
public class HelloController {
 
    @SentinelResource(value = "hello", blockHandler = "blockHandle")
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
    public String blockHandle(BlockException e){
        e.printStackTrace();
        return "被限流了";
}

************

本地限流設定

CustomFlowRule

public class CustomFlowRule implements InitFunc {
 
    @Override
    public void init() throws Exception {
        List<FlowRule> flowRules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("hello");
        flowRule.setCount(1);
        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        flowRules.add(flowRule);
        FlowRuleManager.loadRules(flowRules);
    }
}

META-INF/services/com.alibaba.csp.sentinel.init.InitFunc

com.example.demo.rule.CustomFlowRule

jmeter 測試

2022-03-27 22:30:50.534  INFO 1791 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-27 22:30:50.547  INFO 1791 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished
2022-03-27 22:30:50.557  INFO 1791 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.227 seconds (JVM running for 2.824)
2022-03-27 22:31:04.044  INFO 1791 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-27 22:31:04.044  INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-03-27 22:31:04.049  INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException

************

sentinel dashboard設定限流

啟動sentinel dashboard

java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
 
引數說明:
-Dserver.port=8081:指定控制檯啟動埠
-Dcsp.sentinel.dashboard.server:指定控制檯地址和埠
-Dproject.name=sentinel-dashboard:指定控制檯專案名稱

localhost:8081,控制檯設定流控策略

說明:若需使用本地降級方法,需在下方的hello設定流控規則

jmeter 測試

2022-03-28 08:50:29.165  INFO 853 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-28 08:50:29.198  INFO 853 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished
2022-03-28 08:50:29.210  INFO 853 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.315 seconds (JVM running for 4.03)
2022-03-28 08:52:05.792  INFO 853 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-28 08:52:05.793  INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-03-28 08:52:05.802  INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException

到此這篇關於sentinel 整合spring cloud限流的過程解析的文章就介紹到這了,更多相關sentinel 整合spring cloud限流內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com