核心demo:
1 引入依賴:?
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.8.0</version> </dependency>2? 核心代碼:
?3 限流保護代碼:
以上就是sentinel最核心的代碼使用邏輯。可以在代碼中硬編碼需要保護的資源,然后在調用資源之前使用Api做限流保護。
在實際使用過程中,幾乎不會使用如上方式做資源保護:代碼侵入性強,不能動態擴展需要保護的資源
使用注解完成資源定義(springboot中):
1 引入依賴:?
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.8.0</version> </dependency>
2 核心代碼:
?
?使用注解可以減少代碼的侵入性,但是不能動態的擴展(限流規則還是需要硬編碼提前處理)
springboot + sentinel + sentinel dashboard
? ? 1? 下載sentinel dashboard jar包
sentinel-dashboard-1.8.0.jar(當然也可以通過docker下載鏡像啟動)
? 2? 添加啟動參數完成dashbaord的啟動
-Dserver.port=8718? -Dcsp.sentinel.dashboard.server=localhost:8718? -Dproject.name=sentinel-dashboard? ?-Dcsp.sentinel.api.port=8719
其中:server.port=8719 表示dashbaord的http端口號
-Dcsp.sentinel.dashboard.server=localhost:8718 表示dashboard把自己本身也注冊到dashboard上顯示
-Dproject.name=sentinel-dashboard? 顯示的項目名稱
-Dcsp.sentinel.api.port=8719 客戶端通信端口
3? sprongboot應用中連接到sentinel dashboard上
a 依賴
?b 添加啟動命令
-Dcsp.sentinel.dashboard.server=192.168.56.102:8858? ? ? ? ?---dashbaord地址
-Dproject.name=webDemo2? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ---客戶端限制名稱
-Dcsp.sentinel.api.port=8719? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?---通信端口號
c 配置:
????????
? d 通過調用接口:http://localhost:8112/getStr
此時可以在dashboard的儀表盤上看到接入的應用了:
?在spring cloud中的應用接入:
1 依賴:
2 配置
添加如上依賴和配置之后,無需額外配置就可在dashboard上看到相關的應用信息
至于:sentinel核心代碼的源碼分析和dashboard通信機制在后面的源碼分析會補上