前言
在現(xiàn)代分布式系統(tǒng)中,如何有效地保護系統(tǒng)免受突發(fā)流量和故障的影響,是每個開發(fā)人員和架構師都需要思考的重要問題。在這樣的背景下,Sentinel作為一個強大的系統(tǒng)保護和控制組件,為我們提供了降級、限流、熔斷等多種策略,幫助我們更好地保障系統(tǒng)的穩(wěn)定性和可用性。
https://sentinelguard.io/zh-cn/docs/quick-start.html
- 在微服務的體系架構中,如果遇到
服務提供方
不能提供服務
時,怎么辦?- 將采用spring cloud alibaba sentinel進行解決。
- 在學習sentinel之前,先了解相關的觀念。
正文
一. 微服務常見概念
1 服務雪崩
- 服務雪崩:在整條鏈路的服務中,一個服務失敗,導致整條鏈路的服務都失敗的情形。
- 存在整條鏈路服務(Service A、Service B、Service C)
- Service A 流量突然性增加,導致Service B 和Service C 流量也增加。
- Service C 因為抗不住請求,變得不可用。導致Service B的請求變得阻塞。
- 當Service B的資源耗盡,Service B就會變得不可用。
- 最后 Service A 不可用。
2 服務熔斷
- 服務熔斷:當下游的服務因為某種原因突然變得不可用或響應過慢,上游服務為了保證自己整體服務的可用性,不再繼續(xù)調用目標服務,直接返回,快速釋放資源。如果目標服務情況好轉則恢復調用。
最開始處于
closed
狀態(tài),一旦檢測到錯誤到達一定閾值,便轉為open
狀態(tài);這時候會有個 reset timeout,到了這個時間了,會轉移到
half open
狀態(tài);嘗試放行一部分請求到后端,一旦檢測成功便回歸到
closed
狀態(tài),即恢復服務;
3 服務降級
- 什么是服務降級呢?
- 當下游的服務因為某種原因響應過慢,下游服務主動停掉一些不太重要的業(yè)務,釋放出服務器資源,增加響應速度!
- 當下游的服務因為某種原因不可用,上游主動調用本地的一些降級邏輯,避免卡頓,迅速返回給用戶!
4 熔斷和降級的區(qū)別
-
服務熔斷和服務降級的區(qū)別?
- 服務降級有很多種降級方式!如開關降級、限流降級、熔斷降級!
- 服務熔斷屬于降級方式的一種!
- 當發(fā)生下游服務不可用的情況,熔斷和降級必定是一起出現(xiàn)。
-
服務降級大多是屬于一種業(yè)務級別的處理,熔斷屬于框架層級的實現(xiàn)
-
開關降級
在配置中心配置一個開關(變量),在配置中心更改開關,決定哪些服務進行降級
5 Sentinel 介紹
- Sentinel :一個高可用的流量控制與防護組件,保障微服務的穩(wěn)定性。
- Sentinel分為兩個部分,sentinel-core與sentinel-dashboard。
- sentinel-core 部分能夠支持在本地引入sentinel-core進行限流規(guī)則的整合與配置。
- sentinel-dashboard 則在core之上能夠支持在線的流控規(guī)則與熔斷規(guī)則的維護與調整等。
二. core:降級
1 現(xiàn)象1
-
提供者搭建集群(8170/8270),調用者調用,此時關閉提供者的一個服務(8270)
-
存在現(xiàn)象:訪問8170成功訪問,不能訪問8270
- RestTemplate:8170可訪問,當訪問8270時異常。稍等片刻只有8170.
-
略有卡頓,稍等片刻后,不再卡頓。
2 現(xiàn)象2:
-
提供者搭建集群(8170/8270),調用者調用,此時關閉提供者的所有服務
-
現(xiàn)象:無法訪問
3 降級操作
-
添加坐標
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
-
修改yml文件,開啟feign對sentinel的支持
feign: sentinel: enabled: true
-
修改啟動類,開啟feign
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; /** * @author 薛慕昭 * @email 18716011269@163.com */ @SpringBootApplication @EnableDiscoveryClient //服務發(fā)現(xiàn) @EnableFeignClients //遠程調用 public class TestNacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(TestNacosConsumerApplication.class, args ); } }
-
修改Feign實現(xiàn)
package com.czxy.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author 薛慕昭 * @email 18716011269@163.com */ // @FeignClient(value = "服務名", path = "controller配置的路徑" ) @FeignClient(value = "service-provider", fallback = EchoFeignFallback.class ) public interface EchoFeign { // 與 nacos-provider-2.1>EchoController聲明的方法的完全一致 @GetMapping("/echo/{string}") public String echo(@PathVariable String string); }
package com.czxy.feign; import org.springframework.stereotype.Component; /** * @author 薛慕昭 * @email 18716011269@163.com */ @Component public class EchoFeignFallback implements EchoFeign { @Override public String echo(String string) { return "降級處理:" + string; } }
-
關閉服務提供者,測試
- 注意:需重啟服務
三. dashboard 控制面板
1 概述
-
Sentinel Dashboard 是一個
可視化
流控管理工具。 -
Sentinel Dashboard 是一個獨立的項目,sentinel-dashboard-1.8.4.jar,需要使用 java -jar 運行
java -jar -Dserver.port=18080 sentinel-dashboard-1.8.4.jar
-
下載地址
https://github.com/alibaba/Sentinel/releases
2 配置dashboard
-
添加坐標(已有)
<!-- 降級 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
-
配置yml
#server.port=8071 #spring.application.name=service-consumer #spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 #spring.cloud.sentinel.transport.dashboard=192.168.152.153:8080 #端口號 server: port: 8071 spring: application: name: service-consumer #服務名 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 #nacos服務地址 sentinel: transport: dashboard: 127.0.0.1:18080 feign: sentinel: enabled: true
-
測試
-
先訪問資源
http://localhost:8071/feign/echo/123
-
dashboard 登錄
-
查看控制面板
http://localhost:18080/
-
3 設置資源點(埋點)
-
通過 @SentinelResource 注解,設置監(jiān)控點(定義控制資源、配置控制策略)
package com.czxy.nacos.controller; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.czxy.nacos.feign.TestFeign; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author 薛慕昭 * @email 18716011269@163.com */ @RestController @RequestMapping("/feign") public class TestFeignController { @Resource private TestFeign testMyFeign; @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) @SentinelResource("/feign/echo") public String echo(@PathVariable String str) { return testMyFeign.echo(str); } }
-
測試
四. 限流
1 編寫測試類
package com.czxy.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 薛慕昭
* @email 18716011269@163.com
*/
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/login")
public String login(String str) {
return "登錄成功" + str;
}
@GetMapping("/register")
public String register(String str) {
return "注冊成功";
}
}
2 限流方法
- 通過
@SentinelResource
注解的blockHandler
屬性制定限流的處理函數(shù)
package com.czxy.nacos.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 薛慕昭
* @email 18716011269@163.com
*/
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/login")
// 限流設置
@SentinelResource(value="login", blockHandler = "loginBlockHandler")
public String login(String str) {
return "登錄成功" + str;
}
public String loginBlockHandler(String str , BlockException e) {
return str + ": 請稍后重試";
}
@GetMapping("/register")
public String register(String str) {
return "注冊成功";
}
}
3 限流操作
-
運行 sentinel-dashboard-1.8.4.jar
-
訪問測試功能:http://localhost:8071/user/login?str=1234
-
通過 dashboard 設置限流
- QPS:一般指每秒查詢率
-
連續(xù)快速2次訪問測試功能
五. 熔斷降級
1 降級方法
- 使用@SentinelResource注解的fallback屬性來指定降級的方法名
package com.czxy.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 薛慕昭
* @email 18716011269@163.com
*/
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/login")
// 限流設置
@SentinelResource(value="login", blockHandler = "loginBlockHandler")
public String login(String str) {
return "登錄成功" + str;
}
public String loginBlockHandler(String str , BlockException e) {
return str + ": 請稍后重試";
}
@GetMapping("/register")
// 熔斷降級
@SentinelResource(value="register", fallback = "registerFallback")
public String register(String str) {
int r = RandomUtils.nextInt(10);
if(r < 5) {
int i = 1 / 0;
}
return "注冊成功";
}
public String registerFallback(String str) {
return str + ": 熔斷降級";
}
}
2 測試
-
成功
-
熔斷降級
3 降級操作
- 慢調用比例:
- RT:平均響應時間
- 比例閾值:
- 熔斷時長:
- 最小請求數(shù):
- 異常比例:每秒異常總數(shù)占通過量的比值超過閾值(DegradeRule 中的 count)之后,資源進入降級狀態(tài)
- 異常數(shù):當資源近 1 分鐘的異常數(shù)目超過閾值之后會進行熔斷
文章來源:http://www.zghlxwxcb.cn/news/detail-820420.html
六. 限流和降級的區(qū)別?
- 限流是通過設置QPS(每秒查詢率)/線程數(shù),將超過閾值部分拒絕處理;
- 服務降級是監(jiān)控請求響應時間、響應異常比例、異常數(shù)量;超過限定閾值,將進行服務降級熔斷,一定時間內不可用;
結尾
k(String str) {
return str + “: 熔斷降級”;
}
}文章來源地址http://www.zghlxwxcb.cn/news/detail-820420.html
### 2 測試
* 成功
[外鏈圖片轉存中...(img-EtoFL8NX-1705283262451)]
* 熔斷降級
[外鏈圖片轉存中...(img-SNdK9B8Y-1705283262451)]
### 3 降級操作
* 慢調用比例:
* RT:平均響應時間
* 比例閾值:
* 熔斷時長:
* 最小請求數(shù):
* 異常比例:每秒異??倲?shù)占通過量的比值超過閾值(DegradeRule 中的 count)之后,資源進入降級狀態(tài)
* 異常數(shù):當資源近 1 分鐘的異常數(shù)目超過閾值之后會進行熔斷
[外鏈圖片轉存中...(img-7eNiRPkH-1705283262452)]
## 六. 限流和降級的區(qū)別?
* 限流是通過設置QPS(每秒查詢率)/線程數(shù),將超過閾值部分拒絕處理;
* 服務降級是監(jiān)控請求響應時間、響應異常比例、異常數(shù)量;超過限定閾值,將進行服務降級熔斷,一定時間內不可用;
# 結尾
Sentinel的降級、限流、熔斷等功能為我們構建健壯的分布式系統(tǒng)提供了強有力的支持。通過合理地設置各項保護和控制策略,我們可以更好地抵御惡劣環(huán)境下的挑戰(zhàn),保持系統(tǒng)的穩(wěn)定和可靠。因此,在設計和實現(xiàn)分布式系統(tǒng)時,充分利用Sentinel的功能將是一個明智的選擇,它將為系統(tǒng)的高可用性和穩(wěn)定性保駕護航。
到了這里,關于Sentinel 降級、限流、熔斷的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!