偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

系統(tǒng) Linux
Hello,大家好,我是麥洛,今天帶大家來了解一下SpringBoot如何繼承Sentinel來實現(xiàn)接口流量控制。

 

Hello,大家好,我是麥洛,今天帶大家來了解一下SpringBoot如何繼承Sentinel來實現(xiàn)接口流量控制

Sentinel控制臺搭建

在我的上一篇文章阿里出品的Sentinel到底是個什么玩意?中,已經(jīng)介紹過如何準(zhǔn)備Sentinel控制臺,大家可以直接參考;

Sentinel 客戶端

項目搭建

首先我們來創(chuàng)建一個測試項目,這里初始化項目的url建議大家填寫阿里云的地址,會有驚喜😅

  1. http://start.aliyun.com 

 

接下來就是常規(guī)操作,一路next,在下圖的位置稍微注意一下


說明:

同大家以前創(chuàng)建項目一樣,只需要在這里勾選Sentinel就可以啦🚀

項目創(chuàng)建好以后,我們發(fā)現(xiàn)pom文件中引入了下面的依賴


有的小伙伴看網(wǎng)上博客,也會有下面的方式,指定版本號

  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,會發(fā)現(xiàn)Sentinel的版本號都定義父工程,Cloud的各個組件的兼容性就不要大家操心了

  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> 

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

  1. server.port=8083 
  2. # 應(yīng)用名稱 
  3. spring.application.name=springcloud-sentinel 
  4. # Sentinel 控制臺地址 
  5. spring.cloud.sentinel.transport.dashboard=localhost:8080 
  6. # 取消Sentinel控制臺懶加載 
  7. # 默認(rèn)情況下 Sentinel 會在客戶端首次調(diào)用的時候進(jìn)行初始化,開始向控制臺發(fā)送心跳包 
  8. # 配置 sentinel.eager=true 時,取消Sentinel控制臺懶加載功能 
  9. spring.cloud.sentinel.eager=true 
  10. # 如果有多套網(wǎng)絡(luò),又無法正確獲取本機(jī)IP,則需要使用下面的參數(shù)設(shè)置當(dāng)前機(jī)器可被外部訪問的IP地址,供admin控制臺使用 
  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.  * 項目入口 
  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 版本開始可以直接利用 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 注解用來標(biāo)識資源是否被限流、降級。上述例子上該注解的屬性 sayHello 表示資源名。

啟動控制臺

  1. java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar 

 

控制臺配置規(guī)則

控制臺的操作我們用編程式定義的例子來演示,大家啟動我們的服務(wù)


我們會發(fā)現(xiàn)除了sentinel-dashboard之外,多了一個milolee-sentinel,這個就是我們的服務(wù),它的名稱其實對應(yīng)我們配置文件定義的應(yīng)用名稱:

  1. # 應(yīng)用名稱 
  2. spring.application.name=milolee-sentinel 

點擊機(jī)器列表,這這里如果能發(fā)現(xiàn)你的機(jī)器,那就是成功上線了


實時監(jiān)控


簇點鏈路


流控規(guī)則配置

給我們的資源HelloWorld配置流控規(guī)則,它的QPS(每秒請求數(shù))為1,如圖:


通過查看實時監(jiān)控,我們發(fā)現(xiàn)已經(jīng)生效


降級規(guī)則配置

給我們的資源HelloWorld添加一個降級規(guī)則配置,如果QPS大于1,且平均響應(yīng)時間大于20ms,則接口下來接口在2秒鐘無法訪問,之后自動恢復(fù)。


目前這些規(guī)則僅在內(nèi)存態(tài)生效,應(yīng)用重啟之后,該規(guī)則會丟失。后續(xù)文章我們會繼續(xù)學(xué)習(xí)動態(tài)規(guī)則


💯關(guān)于控制臺的使用,大家可以參考官方文檔,比較詳細(xì)https://sentinelguard.io/zh-cn/docs/dashboard.html

 

責(zé)任編輯:姜華 來源: 今日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)新

2013-07-22 14:25:29

iOS開發(fā)ASIHTTPRequ

2011-06-23 09:09:37

流量控制

2010-05-27 10:43:29

Linux流量控制

2021-03-09 07:38:15

Percona Xtr流量控制運維

2010-06-17 17:00:07

Linux流量控制

2019-07-02 10:22:15

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

2009-02-05 10:13:00

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

2024-12-02 08:02:36

2010-05-27 11:03:44

Linux流量控制

2010-08-06 10:02:07

2010-06-04 11:21:42

Linux 流量控制

2024-03-04 00:02:00

Redis存儲令牌

2009-12-08 15:18:01

路由器功能

2010-11-30 09:40:15

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

2009-10-27 20:14:15

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

2010-06-13 13:34:47

Linux 流量控制
點贊
收藏

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