SpringCloudAlibaba(六):SpringCloudAlibaba Sentinel實現限流降級

大概有半個月沒有進行更新了,確實也在糾結一些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」

「本篇源碼示例:」

https://github.com/CainGao/SpringCloudAlibabaExample  

本文分享自微信公衆號 - 指尖數蟲(zhijianshuchong)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章