springcloud(11)sentinel安裝和基本使用


作為阿里版的hystrix,sentinel簡化了配置方式,提供了可視化界面網站和便捷的配置方式,更加貼合實際的使用方式,各種優點使得sentinel成為服務降級熔斷流控等的最佳選擇。

1.安裝啟用

https://github.com/alibaba/Sentinel/releases

官網選擇合適的版本下載,其本體是jar文件。

java -jar sentinel-dashboard-1.7.2.jar

由於下載的安裝包是jar,所以直接在cmd中啟動即可。

登錄地址默認是localhost:8080,賬號密碼都是sentinel,第一次進入界面會是空白,且只有訪問過應用一遍,界面內才會出現對應的應用程序的信息。

 

 

 2.配置應用項目連接sentinel。

1)配置pom文件

 <dependencies>
        <!-- SpringCloud ailibaba nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!-- SpringCloud ailibaba sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.bai</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--熱部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
View Code

2)配置yml

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinal-service
  cloud:
    nacos:
      discovery:
        #Nacos服務注冊中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentin dashboard地址
        dashboard: localhost:8080
        # 默認8719端口,假如被占用了會自動從8719端口+1進行掃描,直到找到未被占用的 端口
        port: 8719
      datasource:   #配置流控規則持久化
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: cloudalibaba-sentinel-service
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow
management:
  endpoints:
    web:
      exposure:
        include: '*'
View Code

3)啟動類常規配置

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelMain8401 {
    public static void main(String[] args) {
        SpringApplication.run(SentinelMain8401.class,args);
    }
}
View Code

4)controller測試

@RestController
@Slf4j
public class FlowLimitController {

    @GetMapping(value = "/testA")
    public String testA(){
        return "******this is A";
    }
    @GetMapping(value = "/testB")
    public String testB(){
        log.info(Thread.currentThread().getName()+"\t"+"***testB");
        return "******this is B";
    }
    @GetMapping(value = "/testD")
    public String testD(){
       /* try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("this is testD  RT");*/
        int a=10/0;
        log.info("this is testD  異常比例");
        return "!!!!!TESTD";
    }@GetMapping(value = "/testE")
    public String testE(){
        int a=10/0;
        log.info("this is testE  異常數");
        return "!!!!!TESTE";
    }

    @GetMapping(value = "/hotkey")
    @SentinelResource(value = "hotkey",blockHandler = "hotkeyhandler")
    public String hotkey(@RequestParam(value = "p1",required = false)String p1,
                         @RequestParam(value = "p2",required = false)String p2){
        return ">>>>>>hotkey is ready";
    }
    public String hotkeyhandler(String p1, String p2, BlockException exception){
        return ".....hotkey is failed o(╥﹏╥)o";
    }

}
View Code

3.常規使用

1)流控規則

點擊主頁面的流控規則選擇新增規則

 

資源名,自己要訪問方法的地址

閾值類型,QPS指一秒內通過的訪問量,線程數則是啟用多少個線程來訪問同一個資源。

單機閾值,例如寫1,選QPS那么一秒內多於1次訪問testA就會進行流量控制,強制報錯,線程同理。

流控模式,直接則是遇到閾值就直接失敗且默認,關聯則是可以在一個資源上關聯另一個資源,當資源訪問量超過閾值就會強制停止另一個資源進行控制。

流控效果,快速失敗是默認,warm up 是為了避免系統突然一段時間內進行了高並發的情況,對閾值進行限制,分為一段段的增長,最終達到閾值。排隊等待即訪問量過來根據規則一個個通過。

2)降級規則

區別於hystrix,sentinel通過網頁可以配置三種降級策略。

 

 RT:建議去查看官網,有明確的限定規則,即1秒內5次以上訪問,且平均訪問時間不得超過4900,如若超過以上限制就會進行降級處理。

異常比例:一定時間內錯誤達到一定比例就會觸發降級。

異常數:一定時間內錯誤達到一定數就會觸發降級。(以上配置需要嚴格遵守官網標准)

3.熱點規則

熱點規則是為了對方法中的某一個參數進行限流控制,可以精確到特定的參數,而且可以對指定的變量進行額外的控制。

 

 熱點規則只支持QPS,需要配合sentinelresource注解

 @SentinelResource(value = "hotkey",blockHandler = "hotkeyhandler")
   

blockhandler為熱點限定的方法出現錯誤的時候兜底的另一個方法。避免出現錯誤時候使用的系統自帶的頁面,可以控制想要出現的信息。

public String hotkeyhandler(String p1, String p2, BlockException exception){
        return ".....hotkey is failed o(╥﹏╥)o";
    }
View Code

4.系統規則

 

 系統規則可以為整個應用加上層限制,最先進行判斷,如果超過規定的閾值,就會觸發降級規則,出現錯誤頁面提示。

5.接口形式兜底方法。

  @GetMapping(value = "/byhander")
    @SentinelResource(value = "byhander",blockHandlerClass = Myhander.class,blockHandler = "byhander1")
    public CommonResult byhander(){
        return new CommonResult(200,"成功訪問到URL",new PayMent(21L,"999"));
    }

hander接口

public class Myhander {
    public static CommonResult byhander1(BlockException exception){
        return new CommonResult(400,"訪問失敗了"+exception.getClass().getCanonicalName()+"這里是hander1");
    }
    public static CommonResult byhander2(BlockException exception){
        return new CommonResult(400,"訪問失敗了"+exception.getClass().getCanonicalName()+"這里是hander2");
    }
}

選擇什么樣的兜底方法,如果進行報錯的話,就會選擇對應的方法名稱。

 本篇所有代碼均在GitHub:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM