Spring Cloud Alibaba Sentinel
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
相關文章
- Spring-Cloud-Alibaba之SentinelSpringCloud
- Spring Cloud Alibaba(9)---Sentinel概述SpringCloud
- Spring Cloud Alibaba元件之SentinelSpringCloud元件
- Spring Cloud Alibaba教程:Sentinel的使用SpringCloud
- Spring Cloud Alibaba(四)--Gateway與SentinelSpringCloudGateway
- [Spring-Cloud-Alibaba] Sentinel 規則持久化SpringCloud持久化
- Spring Cloud Alibaba(11)---Sentinel+Nacos持久化SpringCloud持久化
- Spring Cloud Alibaba:Sentinel實現熔斷與限流SpringCloud
- Spring Cloud Alibaba | Sentinel: 服務限流基礎篇SpringCloud
- Spring Cloud Alibaba | Sentinel: 服務限流高階篇SpringCloud
- 當Spring Cloud Alibaba Sentinel碰上Spring Cloud Sleuth會擦出怎樣的火花SpringCloud
- Spring Cloud Alibaba系列(六)sentinel的實際應用SpringCloud
- 9.Spring Cloud Alibaba Sentinel流控熔斷元件SpringCloud元件
- Spring Cloud Alibaba Sentinel 主要原理和核心類介紹SpringCloud
- Spring Cloud Alibaba - Sentinel入門案例(四)(熱點規則 )SpringCloud
- 記一次spring cloud alibaba+Sentinel監控整合SpringCloud
- Spring Cloud Alibaba基礎教程:使用Sentinel實現介面限流SpringCloud
- spring cloud alibaba系列(二)Sentinel應用的限流管理SpringCloud
- Spring Cloud Alibaba(10)---Sentinel控制檯搭建+整合SpringCloudAlibabaSpringCloudGC
- Spring Cloud Alibaba系列(五)sentinel實現服務限流降級SpringCloud
- Spring Cloud Alibaba基礎教程:Sentinel使用Apollo儲存規則SpringCloud
- Spring Cloud Alibaba基礎教程:Sentinel使用Nacos儲存規則SpringCloud
- Spring Cloud Alibaba基礎教程:Sentinel Dashboard同步Apollo儲存規則SpringCloud
- Spring Cloud Alibaba | Sentinel: 分散式系統的流量防衛兵初探SpringCloud分散式
- Spring Cloud Alibaba系列——Sentinel降級規則簡介與實踐SpringCloud
- Spring Cloud Alibaba生態探索:Dubbo、Nacos及Sentinel的完美結合SpringCloud
- Spring Cloud Alibaba基礎教程:Sentinel Dashboard中修改規則同步到NacosSpringCloud
- Spring Cloud Alibaba SeataSpringCloud
- 一篇搞定Sentinel-搭建Spring Cloud Alibaba服務元件Sentinel實現服務資源控制SpringCloud元件
- spring cloud 和 阿里微服務spring cloud AlibabaSpringCloud阿里微服務
- Spring Cloud Alibaba之NacosSpringCloud
- Spring Cloud Alibaba, 開箱即用SpringCloud
- 阿里Sentinel支援Spring Cloud Gateway啦阿里SpringCloudGateway
- Spring Cloud Alibaba入門篇SpringCloud
- Spring Cloud Alibaba(2)---Nacos概述SpringCloud
- Spring-Cloud-Alibaba之SeataSpringCloud
- Spring Cloud Alibaba(13)---Sleuth概述SpringCloud
- 詳細剖析Spring Cloud 和Spring Cloud Alibaba的前世今生SpringCloud