SpringBoot集成Sentinel實現(xiàn)接口流量控制
Hello,大家好,我是麥洛,今天帶大家來了解一下SpringBoot如何繼承Sentinel來實現(xiàn)接口流量控制
Sentinel控制臺搭建
在我的上一篇文章阿里出品的Sentinel到底是個什么玩意?中,已經(jīng)介紹過如何準(zhǔn)備Sentinel控制臺,大家可以直接參考;
Sentinel 客戶端
項目搭建
首先我們來創(chuàng)建一個測試項目,這里初始化項目的url建議大家填寫阿里云的地址,會有驚喜😅
- http://start.aliyun.com
接下來就是常規(guī)操作,一路next,在下圖的位置稍微注意一下

說明:
同大家以前創(chuàng)建項目一樣,只需要在這里勾選Sentinel就可以啦🚀
項目創(chuàng)建好以后,我們發(fā)現(xiàn)pom文件中引入了下面的依賴

有的小伙伴看網(wǎng)上博客,也會有下面的方式,指定版本號
- <!-- sentinel -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
- <version>2.1.0.RELEASE</version>
- </dependency>
如果你使用我推薦的阿里云的Url,會發(fā)現(xiàn)Sentinel的版本號都定義父工程,Cloud的各個組件的兼容性就不要大家操心了
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>${spring-cloud-alibaba.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
打開項目配置文件,會發(fā)現(xiàn)它已經(jīng)為我們自動加好了配置,真的超級方便👏
- server.port=8083
- # 應(yīng)用名稱
- spring.application.name=springcloud-sentinel
- # Sentinel 控制臺地址
- spring.cloud.sentinel.transport.dashboard=localhost:8080
- # 取消Sentinel控制臺懶加載
- # 默認(rèn)情況下 Sentinel 會在客戶端首次調(diào)用的時候進(jìn)行初始化,開始向控制臺發(fā)送心跳包
- # 配置 sentinel.eager=true 時,取消Sentinel控制臺懶加載功能
- spring.cloud.sentinel.eager=true
- # 如果有多套網(wǎng)絡(luò),又無法正確獲取本機(jī)IP,則需要使用下面的參數(shù)設(shè)置當(dāng)前機(jī)器可被外部訪問的IP地址,供admin控制臺使用
- # spring.cloud.sentinel.transport.client-ip=# sentinel 配置
- spring.application.name=frms
- spring.cloud.sentinel.transport.dashboard=localhost:8080
- spring.cloud.sentinel.transport.heartbeat-interval-ms=500
如何定義資源
編程式定義
官網(wǎng)提供的demo
- package com.milo.sentinel;
- import com.alibaba.csp.sentinel.Entry;
- import com.alibaba.csp.sentinel.SphU;
- import com.alibaba.csp.sentinel.slots.block.BlockException;
- import com.alibaba.csp.sentinel.slots.block.RuleConstant;
- import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
- import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 項目入口
- * @author Milo Lee
- * @date 2021-3-20 19:07
- *
- */
- @SpringBootApplication
- public class SentinelApplication {
- public static void main(String[] args) {
- SpringApplication.run(SentinelApplication.class, args);
- // 配置規(guī)則.
- initFlowRules();
- while (true) {
- // 1.5.0 版本開始可以直接利用 try-with-resources 特性
- try (Entry entry = SphU.entry("HelloWorld")) {
- // 被保護(hù)的邏輯
- Thread.sleep(300);
- System.out.println("hello world");
- } catch (BlockException | InterruptedException ex) {
- // 處理被流控的邏輯
- System.out.println("blocked!");
- }
- }
- }
- private static void initFlowRules(){
- List<FlowRule> rules = new ArrayList<>();
- FlowRule rule = new FlowRule();
- rule.setResource("HelloWorld");
- rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
- // Set limit QPS to 20.
- rule.setCount(20);
- rules.add(rule);
- FlowRuleManager.loadRules(rules);
- }
- }
注解式定義
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(ServiceApplication.class, args);
- }
- }
- @Service
- public class TestService {
- @SentinelResource(value = "sayHello")
- public String sayHello(String name) {
- return "Hello, " + name;
- }
- }
- @RestController
- public class TestController {
- @Autowired
- private TestService service;
- @GetMapping(value = "/hello/{name}")
- public String apiHello(@PathVariable String name) {
- return service.sayHello(name);
- }
- }
@SentinelResource 注解用來標(biāo)識資源是否被限流、降級。上述例子上該注解的屬性 sayHello 表示資源名。
啟動控制臺
- java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar
控制臺配置規(guī)則
控制臺的操作我們用編程式定義的例子來演示,大家啟動我們的服務(wù)

我們會發(fā)現(xiàn)除了sentinel-dashboard之外,多了一個milolee-sentinel,這個就是我們的服務(wù),它的名稱其實對應(yīng)我們配置文件定義的應(yīng)用名稱:
- # 應(yīng)用名稱
- spring.application.name=milolee-sentinel
點擊機(jī)器列表,這這里如果能發(fā)現(xiàn)你的機(jī)器,那就是成功上線了

實時監(jiān)控

簇點鏈路

流控規(guī)則配置
給我們的資源HelloWorld配置流控規(guī)則,它的QPS(每秒請求數(shù))為1,如圖:

通過查看實時監(jiān)控,我們發(fā)現(xiàn)已經(jīng)生效

降級規(guī)則配置
給我們的資源HelloWorld添加一個降級規(guī)則配置,如果QPS大于1,且平均響應(yīng)時間大于20ms,則接口下來接口在2秒鐘無法訪問,之后自動恢復(fù)。

目前這些規(guī)則僅在內(nèi)存態(tài)生效,應(yīng)用重啟之后,該規(guī)則會丟失。后續(xù)文章我們會繼續(xù)學(xué)習(xí)動態(tài)規(guī)則

💯關(guān)于控制臺的使用,大家可以參考官方文檔,比較詳細(xì)https://sentinelguard.io/zh-cn/docs/dashboard.html