自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SpringBoot集成Sentinel實(shí)現(xiàn)接口流量控制

系統(tǒng) Linux
Hello,大家好,我是麥洛,今天帶大家來(lái)了解一下SpringBoot如何繼承Sentinel來(lái)實(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ì)有驚喜😅

  1. 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)

  1. <!-- sentinel --> 
  2.  <dependency> 
  3.   <groupId>com.alibaba.cloud</groupId> 
  4.   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> 
  5.   <version>2.1.0.RELEASE</version> 
  6.  </dependency> 

如果你使用我推薦的阿里云的Url,會(huì)發(fā)現(xiàn)Sentinel的版本號(hào)都定義父工程,Cloud的各個(gè)組件的兼容性就不要大家操心了

  1. <dependencyManagement> 
  2.        <dependencies> 
  3.            <dependency> 
  4.                <groupId>org.springframework.boot</groupId> 
  5.                <artifactId>spring-boot-dependencies</artifactId> 
  6.                <version>${spring-boot.version}</version> 
  7.                <type>pom</type> 
  8.                <scope>import</scope> 
  9.            </dependency> 
  10.            <dependency> 
  11.                <groupId>com.alibaba.cloud</groupId> 
  12.                <artifactId>spring-cloud-alibaba-dependencies</artifactId> 
  13.                <version>${spring-cloud-alibaba.version}</version> 
  14.                <type>pom</type> 
  15.                <scope>import</scope> 
  16.            </dependency> 
  17.        </dependencies> 
  18.    </dependencyManagement> 

打開(kāi)項(xiàng)目配置文件,會(huì)發(fā)現(xiàn)它已經(jīng)為我們自動(dòng)加好了配置,真的超級(jí)方便👏

  1. server.port=8083 
  2. # 應(yīng)用名稱(chēng) 
  3. spring.application.name=springcloud-sentinel 
  4. # Sentinel 控制臺(tái)地址 
  5. spring.cloud.sentinel.transport.dashboard=localhost:8080 
  6. # 取消Sentinel控制臺(tái)懶加載 
  7. # 默認(rèn)情況下 Sentinel 會(huì)在客戶(hù)端首次調(diào)用的時(shí)候進(jìn)行初始化,開(kāi)始向控制臺(tái)發(fā)送心跳包 
  8. # 配置 sentinel.eager=true 時(shí),取消Sentinel控制臺(tái)懶加載功能 
  9. spring.cloud.sentinel.eager=true 
  10. # 如果有多套網(wǎng)絡(luò),又無(wú)法正確獲取本機(jī)IP,則需要使用下面的參數(shù)設(shè)置當(dāng)前機(jī)器可被外部訪(fǎng)問(wèn)的IP地址,供admin控制臺(tái)使用 
  11. # spring.cloud.sentinel.transport.client-ip=# sentinel 配置 
  12. spring.application.name=frms 
  13. spring.cloud.sentinel.transport.dashboard=localhost:8080 
  14. spring.cloud.sentinel.transport.heartbeat-interval-ms=500 

如何定義資源

編程式定義

官網(wǎng)提供的demo

  1. package com.milo.sentinel; 
  2.  
  3. import com.alibaba.csp.sentinel.Entry; 
  4. import com.alibaba.csp.sentinel.SphU; 
  5. import com.alibaba.csp.sentinel.slots.block.BlockException; 
  6. import com.alibaba.csp.sentinel.slots.block.RuleConstant; 
  7. import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; 
  8. import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; 
  9. import org.springframework.boot.SpringApplication; 
  10. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  11.  
  12. import java.util.ArrayList; 
  13. import java.util.List; 
  14.  
  15. /** 
  16.  * 項(xiàng)目入口 
  17.  * @author Milo Lee 
  18.  * @date 2021-3-20 19:07 
  19.  * 
  20.  */ 
  21. @SpringBootApplication 
  22. public class SentinelApplication { 
  23.  
  24.     public static void main(String[] args) { 
  25.         SpringApplication.run(SentinelApplication.class, args); 
  26.  
  27.         // 配置規(guī)則. 
  28.         initFlowRules(); 
  29.         while (true) { 
  30.             // 1.5.0 版本開(kāi)始可以直接利用 try-with-resources 特性 
  31.             try (Entry entry = SphU.entry("HelloWorld")) { 
  32.                 // 被保護(hù)的邏輯 
  33.                 Thread.sleep(300); 
  34.                 System.out.println("hello world"); 
  35.             } catch (BlockException | InterruptedException ex) { 
  36.                 // 處理被流控的邏輯 
  37.                 System.out.println("blocked!"); 
  38.             } 
  39.         } 
  40.  
  41.     } 
  42.  
  43.     private static void initFlowRules(){ 
  44.         List<FlowRule> rules = new ArrayList<>(); 
  45.         FlowRule rule = new FlowRule(); 
  46.         rule.setResource("HelloWorld"); 
  47.         rule.setGrade(RuleConstant.FLOW_GRADE_QPS); 
  48.         // Set limit QPS to 20. 
  49.         rule.setCount(20); 
  50.         rules.add(rule); 
  51.         FlowRuleManager.loadRules(rules); 
  52.     } 
  53.  

注解式定義

  1. @SpringBootApplication 
  2. public class Application { 
  3.  
  4.     public static void main(String[] args) { 
  5.         SpringApplication.run(ServiceApplication.class, args); 
  6.     } 
  7.  
  8. @Service 
  9. public class TestService { 
  10.  
  11.     @SentinelResource(value = "sayHello"
  12.     public String sayHello(String name) { 
  13.         return "Hello, " + name
  14.     } 
  15.  
  16. @RestController 
  17. public class TestController { 
  18.  
  19.     @Autowired 
  20.     private TestService service; 
  21.  
  22.     @GetMapping(value = "/hello/{name}"
  23.     public String apiHello(@PathVariable String name) { 
  24.         return service.sayHello(name); 
  25.     } 

@SentinelResource 注解用來(lái)標(biāo)識(shí)資源是否被限流、降級(jí)。上述例子上該注解的屬性 sayHello 表示資源名。

啟動(dòng)控制臺(tái)

  1. 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):

  1. # 應(yīng)用名稱(chēng) 
  2. 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

 

責(zé)任編輯:姜華 來(lái)源: 今日J(rèn)ava
相關(guān)推薦

2023-10-08 12:14:42

Sentinel流量控制

2023-06-20 08:10:00

2010-06-04 10:49:58

Linux流量控制

2010-02-03 23:04:31

流量控制P2P華夏創(chuàng)新

2011-06-23 09:09:37

流量控制

2013-07-22 14:25:29

iOS開(kāi)發(fā)ASIHTTPRequ

2010-05-27 10:43:29

Linux流量控制

2010-06-17 17:00:07

Linux流量控制

2021-03-09 07:38:15

Percona Xtr流量控制運(yùn)維

2010-05-27 11:03:44

Linux流量控制

2010-06-04 11:21:42

Linux 流量控制

2024-12-02 08:02:36

2009-02-05 10:13:00

局域網(wǎng)流量控制數(shù)據(jù)流量

2019-07-02 10:22:15

TCP流量數(shù)據(jù)

2010-08-06 10:02:07

2024-03-04 00:02:00

Redis存儲(chǔ)令牌

2009-12-08 15:18:01

路由器功能

2010-11-30 09:40:15

流量控制設(shè)備AllotQOS策略

2009-10-27 20:14:15

數(shù)據(jù)傳輸流量控制網(wǎng)管技巧

2021-05-14 07:45:07

Sentinel 接口限流
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)