Spring Cloud Alibaba Sentinel

Braveyzx發表於2018-11-30

Alibaba Sentinel :資源、規則

1、倉庫

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-alibaba-cloud.version>0.2.0.RELEASE</spring-alibaba-cloud.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-alibaba-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2、引入包

<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>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

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

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-annotation-aspectj</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
</dependency>


<!--<dependency>-->
    <!--<groupId>com.alibaba.csp</groupId>-->
    <!--<artifactId>sentinel-demo-annotation-spring-aop</artifactId>-->
    <!--<version>0.1.1</version>-->
<!--</dependency>-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

3、資源

 

3.1  新建類1

@Configuration
public class SentinelAspectConfiguration {

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

 

3.1  新建類2
public  class ExceptionUtil {

    public static void handleException(BlockException ex) {
        System.out.println("Oops: " + ex.getClass().getCanonicalName());
    }

}

 

3.2定義資源

@Service
public class TestService {


    // 對應的 `handleException` 函式需要位於 `ExceptionUtil` 類中,並且必須為 static 函式.
    @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
    public void test() {
        System.out.println("test通過");
//
    }

    // 原函式
    @SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
    public String hello(long s) {
        return String.format("Hello通過", s);
    }

    // Fallback 函式,函式簽名與原函式一致.
    public String helloFallback(long s) {
        return String.format("helloFallbackhelloFallback", s);
    }

    // Block 異常處理函式,引數最後多一個 BlockException,其餘與原函式一致.
    public static String exceptionHandler(long s, BlockException ex) {
        // Do some log here.
        ex.printStackTrace();
        return "Oops, error occurred at " + s;
    }
}

 

3.3新建測試介面

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private TestService service;


    @GetMapping({"/foo"})
    public String foo() throws Exception {
        this.service.test();
        return this.service.hello(1);
    }
}

 

4、啟動 sentinel-dashboard.jar

         下載地址:https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0

          訪問http://localhost:8080

5、新增配置

spring.application.name=component-sentinel
server.port=18083
spring.cloud.sentinel.transport.dashboard=localhost:8080

5、觸發客戶端初始化

確保客戶端有訪問量,Sentinel 會在客戶端首次呼叫的時候進行初始化,開始向控制檯傳送心跳包。

Sentinel 的理念是開發者只需要關注資源的定義,當資源定義成功後可以動態增加各種流控降級規則。

Sentinel 提供兩種方式修改規則:

  • 通過 API 直接修改 (loadRules)
  • 通過 DataSource 適配不同資料來源修改

https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95

訪問測試介面。新增規則,只存在記憶體中,所以關閉即消失。

      

 

 

 

簇點監控

獲取簇點列表

當應用啟動之後,可以執行下列命令,獲得當前所有簇點(ClusterNode)的列表(JSON 格式):

curl http://localhost:8719/clusterNode

結果示例:

[
 {"avgRt":0.0, //平均響應時間
 "blockRequest":0, //每分鐘攔截的請求個數
 "blockedQps":0.0, //每秒攔截個數
 "curThreadNum":0, //併發個數
 "passQps":1.0, // 每秒成功通過請求
 "passReqQps":1.0, //每秒到來的請求
 "resourceName":"/registry/machine", 資源名稱
 "timeStamp":1529905824134, //時間戳
 "totalQps":1.0, // 每分鐘請求數
 "totalRequest":193}, 
  ....
]

 

查詢某個簇點的詳細資訊

可以用下面命令模糊查詢該簇點的具體資訊,其中 id 對應 resource name,支援模糊查詢:

curl http://localhost:8719/cnode?id=xxxx

 

鏈路監控

我們可以通過命令 curl http://localhost:8719/tree 來查詢鏈路入口的鏈路樹形結構

https://github.com/alibaba/Sentinel/wiki/%E5%AE%9E%E6%97%B6%E7%9B%91%E6%8E%A7

相關文章