溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringCloudAlibaba Sentinel如何實(shí)現(xiàn)限流降級(jí)

發(fā)布時(shí)間:2021-11-24 15:21:02 來(lái)源:億速云 閱讀:159 作者:柒染 欄目:云計(jì)算

SpringCloudAlibaba Sentinel如何實(shí)現(xiàn)限流降級(jí),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

大概有半個(gè)月沒(méi)有進(jìn)行更新了,確實(shí)也在糾結(jié)一些SpringCloudAlibaba的使用場(chǎng)景問(wèn)題。同時(shí)基于當(dāng)前的業(yè)務(wù)與人員配置來(lái)考慮甚至有點(diǎn)想放棄微服務(wù)的方案了。
Sentinel的限流降級(jí)是必不可少的場(chǎng)景,其實(shí)也是基于當(dāng)前的業(yè)務(wù)考慮是否需要Sentinel。
但是最終肯定是需要Sentinel的場(chǎng)景的,還是直接一步到位吧

 

Setinel的基本概念與使用場(chǎng)景

Setinel的介紹為「一個(gè)高可用的流量控制與防護(hù)組件」。流量控制與流量防護(hù)就可以看到這個(gè)組件的意義就是為了保障微服務(wù)的穩(wěn)定性。Sentinel與原有SpringCloud家族的Hystrix的意義是一致的。
其實(shí)能夠?qū)崿F(xiàn)該方案的場(chǎng)景很多,例如一開(kāi)始提到的本來(lái)沒(méi)有準(zhǔn)備使用Sentinel一個(gè)是因?yàn)闃I(yè)務(wù)的原因。另外一個(gè)就是我們本身的一些問(wèn)題從而考慮使用一些更簡(jiǎn)單的方案來(lái)實(shí)現(xiàn)。例如 「nginx」 等。


當(dāng)然相對(duì)于nginx來(lái)說(shuō),Sentinel能夠?qū)崿F(xiàn)的功能與靈活性更好一些。Sentinel的功能更多,所以我也會(huì)慢慢來(lái)開(kāi)始Sentinel的使用介紹。首先我們使用Sentinel實(shí)現(xiàn)一個(gè)限流的功能。當(dāng)然首先我們需要一套Sentinel的環(huán)境。

 
部署Sentinel Dashboard

Sentinel分為兩個(gè)部分,sentinel-core與sentinel-dashboard。
sentinel-core 部分能夠支持在本地引入sentinel-core進(jìn)行限流規(guī)則的整合與配置。
sentinel-dashboard 則在core之上能夠支持在線的流控規(guī)則與熔斷規(guī)則的維護(hù)與調(diào)整等。
言歸正傳我們先部署一個(gè)Sentinel Dashboard。

  • 項(xiàng)目地址:https://github.com/alibaba/Sentinel
  • 下載地址: https://github.com/alibaba/Sentinel/releases    
    本次我們選擇當(dāng)前的最新版 v1.7.2進(jìn)行部署。Sentinel使用的SpringBoot進(jìn)行的開(kāi)發(fā),所以直接下載jar包啟動(dòng)即可使用。
java -jar sentinel-dashboard-1.7.2.jar
 

由于Sentinel-Dashboard是使用SpringBoot進(jìn)行開(kāi)發(fā)的,所以本身沒(méi)有太多的配置文件。默認(rèn)的端口為8080。如果端口沖突可以使用 「--server.port」 進(jìn)行修改綁定。啟動(dòng)成功后使用瀏覽器訪問(wèn)可以看到如下頁(yè)面:

SpringCloudAlibaba Sentinel如何實(shí)現(xiàn)限流降級(jí)  

訪問(wèn)方式為: 「sentinel」/「sentinel」
「WARN:」 如果需要調(diào)整相關(guān)的參數(shù)可以參考github中的具體配置文件進(jìn)行修改。配置如下:

#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}
   
項(xiàng)目集成
  • 首先引入相關(guān)的依賴,引入     「spring-cloud-starter-alibaba-sentinel」模塊。    
    [     「groupId」再次提醒一下,com.alibaba.cloud為畢業(yè)版本]
<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>
 
  • 然后我們就可以把項(xiàng)目中的增加 sentinel-dashboard的相關(guān)配置
spring.cloud.sentinel.transport.dashboard=localhost:8080
 
  • 創(chuàng)建啟動(dòng)類(lèi)與增加Rest訪問(wèn)接口    
        「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";
    }

}
 
  • 訪問(wèn)REST接口打開(kāi)Sentinel-dashboard頁(yè)面SpringCloudAlibaba Sentinel如何實(shí)現(xiàn)限流降級(jí)可以看到我們每次訪問(wèn)的都會(huì)在Dashboard中顯示出來(lái),并且我們的服務(wù)也已經(jīng)注冊(cè)到了sentinel中。

  • 增加流控規(guī)則
    現(xiàn)在我們可以直接在左側(cè)的 簇點(diǎn)鏈路 中查找到我們?cè)L問(wèn)的端口進(jìn)行流控規(guī)則的設(shè)置。SpringCloudAlibaba Sentinel如何實(shí)現(xiàn)限流降級(jí)現(xiàn)在新增一個(gè) QPS=1 的規(guī)則,其他使用默認(rèn)SpringCloudAlibaba Sentinel如何實(shí)現(xiàn)限流降級(jí)

配置完成后我們當(dāng)前的限流效果應(yīng)該就是每秒只能有一條請(qǐng)求成功發(fā)送。其他請(qǐng)求將會(huì)直接失敗。

  • 直接快速訪問(wèn)接口實(shí)現(xiàn)測(cè)試     SpringCloudAlibaba Sentinel如何實(shí)現(xiàn)限流降級(jí)

當(dāng)前可以發(fā)現(xiàn),當(dāng)請(qǐng)求的QPS大于1時(shí),也就是每秒發(fā)送的次數(shù)大于1就會(huì)直接返回 「Blocked by Sentinel」

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI