共計 3390 個字符,預計需要花費 9 分鐘才能閱讀完成。
SpringCloudAlibaba Sentinel 如何實現限流降級,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
大概有半個月沒有進行更新了,確實也在糾結一些 SpringCloudAlibaba 的使用場景問題。同時基于當前的業務與人員配置來考慮甚至有點想放棄微服務的方案了。
Sentinel 的限流降級是必不可少的場景,其實也是基于當前的業務考慮是否需要 Sentinel。
但是最終肯定是需要 Sentinel 的場景的,還是直接一步到位吧
Setinel 的基本概念與使用場景
Setinel 的介紹為「一個高可用的流量控制與防護組件」。流量控制與流量防護就可以看到這個組件的意義就是為了保障微服務的穩定性。Sentinel 與原有 SpringCloud 家族的 Hystrix 的意義是一致的。
其實能夠實現該方案的場景很多,例如一開始提到的本來沒有準備使用 Sentinel 一個是因為業務的原因。另外一個就是我們本身的一些問題從而考慮使用一些更簡單的方案來實現。例如 「nginx」 等。
當然相對于 nginx 來說,Sentinel 能夠實現的功能與靈活性更好一些。Sentinel 的功能更多,所以我也會慢慢來開始 Sentinel 的使用介紹。首先我們使用 Sentinel 實現一個限流的功能。當然首先我們需要一套 Sentinel 的環境。
部署 Sentinel Dashboard
Sentinel 分為兩個部分,sentinel-core 與 sentinel-dashboard。
sentinel-core 部分能夠支持在本地引入 sentinel-core 進行限流規則的整合與配置。
sentinel-dashboard 則在 core 之上能夠支持在線的流控規則與熔斷規則的維護與調整等。
言歸正傳我們先部署一個 Sentinel Dashboard。
項目地址:https://github.com/alibaba/Sentinel 下載地址: https://github.com/alibaba/Sentinel/releases
本次我們選擇當前的最新版 v1.7.2 進行部署。Sentinel 使用的 SpringBoot 進行的開發,所以直接下載 jar 包啟動即可使用。
java -jar sentinel-dashboard-1.7.2.jar
由于 Sentinel-Dashboard 是使用 SpringBoot 進行開發的,所以本身沒有太多的配置文件。默認的端口為 8080。如果端口沖突可以使用 「–server.port」 進行修改綁定。啟動成功后使用瀏覽器訪問可以看到如下頁面:
訪問方式為: 「sentinel」/「sentinel」
「WARN:」 如果需要調整相關的參數可以參考 github 中的具體配置文件進行修改。配置如下:
#spring settings
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
#cookie name setting
server.servlet.session.cookie.name=sentinel_dashboard_cookie
#logging settings
logging.level.org.springframework.web=INFO
logging.file=${user.home}/logs/csp/sentinel-dashboard.log
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
#logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
#auth settings
auth.filter.exclude-urls=/,/auth/login,/auth/logout,/registry/machine,/version
auth.filter.exclude-url-suffixes=htm,html,js,css,map,ico,ttf,woff,png
# If auth.enabled=false, Sentinel console disable login
auth.username=sentinel
auth.password=sentinel
# Inject the dashboard version. It s required to enable
# filtering in pom.xml for this resource file.
sentinel.dashboard.version=${project.version}
項目集成首先引入相關的依賴, 引入
「spring-cloud-starter-alibaba-sentinel」模塊。
[
「groupId」再次提醒一下,com.alibaba.cloud 為畢業版本]
dependencies
dependency
groupId com.alibaba.cloud /groupId
artifactId spring-cloud-starter-alibaba-sentinel /artifactId
/dependency
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-test /artifactId
scope test /scope
/dependency
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-web /artifactId
/dependency
/dependencies
然后我們就可以把項目中的增加 sentinel-dashboard 的相關配置
spring.cloud.sentinel.transport.dashboard=localhost:8080
創建啟動類與增加 Rest 訪問接口
「SentinelFlowControlApplication.java」
@SpringBootApplication
public class SentinelFlowControlApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelFlowControlApplication.class,args);
}
}
「SentinelTestController.java」
@RestController
@RequestMapping(value = sentinel)
public class SentinelTestController {
@RequestMapping(value = hello ,method = RequestMethod.GET)
public String hello(){
return CainGao
}
}
訪問 REST 接口打開 Sentinel-dashboard 頁面可以看到我們每次訪問的都會在 Dashboard 中顯示出來,并且我們的服務也已經注冊到了 sentinel 中。
增加流控規則
現在我們可以直接在左側的 簇點鏈路 中查找到我們訪問的端口進行流控規則的設置。現在新增一個 QPS=1 的規則,其他使用默認
配置完成后我們當前的限流效果應該就是每秒只能有一條請求成功發送。其他請求將會直接失敗。
直接快速訪問接口實現測試
當前可以發現,當請求的 QPS 大于 1 時,也就是每秒發送的次數大于 1 就會直接返回 「Blocked by Sentinel」。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注丸趣 TV 行業資訊頻道,感謝您對丸趣 TV 的支持。