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

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

有的小伙伴看網(wǎng)上博客,也會(huì)有下面的方式,指定版本號(hào)
- <!-- sentinel -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
- <version>2.1.0.RELEASE</version>
- </dependency>
如果你使用我推薦的阿里云的Url,會(huì)發(fā)現(xiàn)Sentinel的版本號(hào)都定義父工程,Cloud的各個(gè)組件的兼容性就不要大家操心了
- <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>
打開(kāi)項(xiàng)目配置文件,會(huì)發(fā)現(xiàn)它已經(jīng)為我們自動(dòng)加好了配置,真的超級(jí)方便👏
- server.port=8083
- # 應(yīng)用名稱(chēng)
- spring.application.name=springcloud-sentinel
- # Sentinel 控制臺(tái)地址
- spring.cloud.sentinel.transport.dashboard=localhost:8080
- # 取消Sentinel控制臺(tái)懶加載
- # 默認(rèn)情況下 Sentinel 會(huì)在客戶(hù)端首次調(diào)用的時(shí)候進(jìn)行初始化,開(kāi)始向控制臺(tái)發(fā)送心跳包
- # 配置 sentinel.eager=true 時(shí),取消Sentinel控制臺(tái)懶加載功能
- spring.cloud.sentinel.eager=true
- # 如果有多套網(wǎng)絡(luò),又無(wú)法正確獲取本機(jī)IP,則需要使用下面的參數(shù)設(shè)置當(dāng)前機(jī)器可被外部訪(fǎng)問(wèn)的IP地址,供admin控制臺(tái)使用
- # 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;
- /**
- * 項(xiàng)目入口
- * @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 版本開(kāi)始可以直接利用 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 注解用來(lái)標(biāo)識(shí)資源是否被限流、降級(jí)。上述例子上該注解的屬性 sayHello 表示資源名。
啟動(dòng)控制臺(tái)
- java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar
控制臺(tái)配置規(guī)則
控制臺(tái)的操作我們用編程式定義的例子來(lái)演示,大家啟動(dòng)我們的服務(wù)

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

實(shí)時(shí)監(jiān)控

簇點(diǎn)鏈路

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

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

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

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

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