不說了,直接上官方文檔
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md
Sentinel Example
項目說明
本項目演示如何使用 Sentinel starter 完成 Spring Cloud 應用的限流管理。
Sentinel 是阿里巴巴開源的分布式系統的流量防衛組件,Sentinel 把流量作為切入點,從流量控制,熔斷降級,系統負載保護等多個維度保護服務的穩定性。
示例
如何接入
在啟動示例進行演示之前,我們先了解一下如何接入 Sentinel。
注意:本章節只是為了便于您理解接入方式,本示例代碼中已經完成接入工作,您無需再進行修改。
- 首先,修改
pom.xml
文件,引入 Sentinel starter。
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
-
接入限流埋點
-
HTTP 埋點
Sentinel starter 默認為所有的 HTTP 服務提供了限流埋點,如果只想對 HTTP 服務進行限流,那么只需要引入依賴,無需修改代碼。
-
自定義埋點
如果需要對某個特定的方法進行限流或降級,可以通過
@SentinelResource
注解來完成限流的埋點,示例代碼如下:@SentinelResource("resource") public String hello() {return "Hello"; }
當然也可以通過原始的
SphU.entry(xxx)
方法進行埋點,可以參見 Sentinel 文檔。
-
-
配置限流規則
Sentinel 提供了兩種配置限流規則的方式:代碼配置 和 控制臺配置。本示例使用的方式為通過控制臺配置。
- 通過代碼來實現限流規則的配置。一個簡單的限流規則配置示例代碼如下,更多限流規則配置詳情請參考 Sentinel 文檔。
List<FlowRule> rules = new ArrayList<FlowRule>(); FlowRule rule = new FlowRule(); rule.setResource(str); // set limit qps to 10 rule.setCount(10); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); rule.setLimitApp("default"); rules.add(rule); FlowRuleManager.loadRules(rules);
- 通過控制臺進行限流規則配置請參考文章后面的圖文說明。
啟動 Sentinel 控制臺
-
首先需要獲取 Sentinel 控制臺,支持直接下載和源碼構建兩種方式。
- 直接下載:下載 Sentinel 控制臺
- 源碼構建:進入 Sentinel Github 項目頁面,將代碼 git clone 到本地自行編譯打包,參考此文檔。
-
啟動控制臺,執行 Java 命令
java -jar sentinel-dashboard.jar
完成 Sentinel 控制臺的啟動。
控制臺默認的監聽端口為 8080。Sentinel 控制臺使用 Spring Boot 編程模型開發,如果需要指定其他端口,請使用 Spring Boot 容器配置的標準方式,詳情請參考 Spring Boot 文檔。
應用啟動
-
增加配置,在應用的 /src/main/resources/application.properties 中添加基本配置信息
spring.application.name=sentinel-example server.port=18083 spring.cloud.sentinel.transport.dashboard=localhost:8080
-
啟動應用,支持 IDE 直接啟動和編譯打包后啟動。
- IDE直接啟動:找到主類
ServiceApplication
,執行 main 方法啟動應用。 - 打包編譯后啟動:首先執行
mvn clean package
將工程編譯打包,然后執行java -jar sentinel-core-example.jar
啟動應用。
- IDE直接啟動:找到主類
調用服務
使用 curl 分別調用兩個 URL,可以看到訪問成功。
配置限流規則并驗證
- 訪問 http://localhost:8080 頁面,可以在左側看到 Sentinel-Example 應用已經注冊到了控制臺,單擊 流控規則 ,可以看到目前的流控規則為空。
注意:如果您在控制臺沒有找到應用,請調用一下進行了 Sentinel 埋點的 URL 或方法,因為 Sentinel 使用了 lazy load 策略。詳細的排查過程請參見 Sentinel FAQ。
-
配置 URL 限流規則:點擊新增流控規則,資源名填寫需要限流的 URL 相對路徑,單機閾值選擇需要限流的閾值,點擊新增進行確認。(為了便于演示效果,這里將值設置成了 1)。
-
配置自定義限流規則:點擊新增流控規則,資源名填寫
@SentinelResource
注解value
字段的值,單機閾值選擇需要限流的閾值,點擊新增進行確認。(為了便于演示效果,這里將值設置成了 1)。 -
訪問 URL,當 QPS 超過 1 時,可以看到限流效果如下。
自定義限流處理邏輯
- 默認限流異常處理
URL 限流觸發后默認處理邏輯是,直接返回 “Blocked by Sentinel (flow limiting)”。
如果需要自定義處理邏輯,實現的方式如下:
public class CustomUrlBlockHandler implements UrlBlockHandler {@Overridepublic void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {// todo add your logic}
}WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler());
- 使用
@SentinelResource
注解下的限流異常處理
如果需要自定義處理邏輯,填寫 @SentinelResource
注解的 blockHandler
屬性(針對所有類型的 BlockException
,需自行判斷)或 fallback
屬性(針對熔斷降級異常),注意對應方法的簽名和位置有限制,詳情見 Sentinel 注解支持文檔。示例實現如下:
public class TestService {// blockHandler 是位于 ExceptionUtil 類下的 handleException 靜態方法,需符合對應的類型限制.@SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})public void test() {System.out.println("Test");}// blockHandler 是位于當前類下的 exceptionHandler 方法,需符合對應的類型限制.@SentinelResource(value = "hello", blockHandler = "exceptionHandler")public String hello(long s) {return String.format("Hello at %d", s);}public String exceptionHandler(long s, BlockException ex) {// Do some log here.ex.printStackTrace();return "Oops, error occurred at " + s;}
}
public final class ExceptionUtil {public static void handleException(BlockException ex) {System.out.println("Oops: " + ex.getClass().getCanonicalName());}
}
一個簡單的 @SentinelResource
示例可以見 sentinel-demo-annotation-spring-aop。
Endpoint 信息查看
Spring Boot 應用支持通過 Endpoint 來暴露相關信息,Sentinel Starter 也支持這一點。
在使用之前需要在 Maven 中添加 spring-boot-starter-actuator
依賴,并在配置中允許 Endpoints 的訪問。
- Spring Boot 1.x 中添加配置
management.security.enabled=false
- Spring Boot 2.x 中添加配置
management.endpoints.web.exposure.include=*
Spring Boot 1.x 可以通過訪問 http://127.0.0.1:18083/sentinel 來查看 Sentinel Endpoint 的信息。Spring Boot 2.x 可以通過訪問 http://127.0.0.1:18083/actuator/sentinel 來訪問。
查看實時監控
Sentinel 控制臺支持實時監控查看,您可以通過 Sentinel 控制臺查看各鏈路的請求的通過數和被限流數等信息。
其中 p_qps
為通過(pass) 流控的 QPS,b_qps
為被限流 (block) 的 QPS。
ReadableDataSource 支持
Sentinel 內部提供了動態規則的擴展實現 ReadableDataSource。
Sentinel starter 整合了目前存在的幾類 ReadableDataSource。只需要在配置文件中進行相關配置,即可在 Spring 容器中自動注冊 DataSource。
比如要定義兩個ReadableDataSource,分別是 FileRefreshableDataSource
和 NacosDataSource
,配置如下:
spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=jsonspring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds2.nacos.dataId=sentinel
spring.cloud.sentinel.datasource.ds2.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds2.nacos.data-type=json
ds1
和 ds2
表示ReadableDataSource的名稱,可隨意編寫。ds1
和 ds2
后面的 file
和 nacos
表示ReadableDataSource的類型。
目前支持file
, nacos
, zk
, apollo
,redis
這5種類型。
其中nacos
,zk
,apollo
,redis
這4種類型的使用需要加上對應的依賴sentinel-datasource-nacos
, sentinel-datasource-zookeeper
, sentinel-datasource-apollo
, sentinel-datasource-redis
。
當ReadableDataSource加載規則數據成功的時候,控制臺會打印出相應的日志信息:
[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 3 DegradeRule
[Sentinel Starter] DataSource ds2-sentinel-nacos-datasource load 2 FlowRule
More
Sentinel 是一款功能強大的中間件,從流量控制,熔斷降級,系統負載保護等多個維度保護服務的穩定性。此 Demo 僅演示了 使用 Sentinel 作為限流工具的使用,更多 Sentinel 相關的信息,請參考 Sentinel 項目。
如果您對 spring cloud sentinel starter 有任何建議或想法,歡迎在 issue 中或者通過其他社區渠道向我們提出。