sentinel?整合spring?cloud限流的過程解析
spring cloud基于http進(jìn)行服務(wù)調(diào)用,大致過程如下:
- 服務(wù)提供端:提供http接口,并向服務(wù)中心注冊(cè)服務(wù)信息
- 服務(wù)消費(fèi)端:將服務(wù)端的http接口作為本地服務(wù),從注冊(cè)中心讀取服務(wù)提供端信息,使用feign發(fā)起遠(yuǎn)程調(diào)用
相關(guān)依賴
<!-- 服務(wù)注冊(cè)與發(fā)現(xiàn) -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel限流 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>示例
為簡(jiǎn)化處理,直接對(duì)url接口進(jìn)行限流,不做服務(wù)調(diào)用

application.yml
spring:
application:
name: hello-sentinel
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8081限流可使用本地配置、或者sentinel dashboard配置
HelloController
@RestController
public class HelloController {
@SentinelResource(value = "hello", blockHandler = "blockHandle")
@RequestMapping("/hello")
public String hello(){
return "hello";
}
public String blockHandle(BlockException e){
e.printStackTrace();
return "被限流了";
}************
本地限流配置

CustomFlowRule
public class CustomFlowRule implements InitFunc {
@Override
public void init() throws Exception {
List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
flowRule.setResource("hello");
flowRule.setCount(1);
flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
flowRules.add(flowRule);
FlowRuleManager.loadRules(flowRules);
}
}META-INF/services/com.alibaba.csp.sentinel.init.InitFunc
com.example.demo.rule.CustomFlowRule
jmeter 測(cè)試



2022-03-27 22:30:50.534 INFO 1791 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-03-27 22:30:50.547 INFO 1791 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished 2022-03-27 22:30:50.557 INFO 1791 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.227 seconds (JVM running for 2.824) 2022-03-27 22:31:04.044 INFO 1791 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-03-27 22:31:04.044 INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-03-27 22:31:04.049 INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms INFO: Sentinel log output type is: file INFO: Sentinel log charset is: utf-8 INFO: Sentinel log base directory is: /Users/huli/logs/csp/ INFO: Sentinel log name use pid is: false com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException
************
sentinel dashboard配置限流
啟動(dòng)sentinel dashboard
java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar 參數(shù)說明: -Dserver.port=8081:指定控制臺(tái)啟動(dòng)端口 -Dcsp.sentinel.dashboard.server:指定控制臺(tái)地址和端口 -Dproject.name=sentinel-dashboard:指定控制臺(tái)項(xiàng)目名稱
localhost:8081,控制臺(tái)配置流控策略


說明:若需使用本地降級(jí)方法,需在下方的hello配置流控規(guī)則
jmeter 測(cè)試



2022-03-28 08:50:29.165 INFO 853 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-03-28 08:50:29.198 INFO 853 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished 2022-03-28 08:50:29.210 INFO 853 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.315 seconds (JVM running for 4.03) 2022-03-28 08:52:05.792 INFO 853 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-03-28 08:52:05.793 INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-03-28 08:52:05.802 INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms INFO: Sentinel log output type is: file INFO: Sentinel log charset is: utf-8 INFO: Sentinel log base directory is: /Users/huli/logs/csp/ INFO: Sentinel log name use pid is: false com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException com.alibaba.csp.sentinel.slots.block.flow.FlowException
到此這篇關(guān)于sentinel 整合spring cloud限流的過程解析的文章就介紹到這了,更多相關(guān)sentinel 整合spring cloud限流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用SensitiveWord實(shí)現(xiàn)敏感詞過濾
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用SensitiveWord實(shí)現(xiàn)敏感詞過濾功能,文中示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01
SpringBoot 使用WebSocket功能(實(shí)現(xiàn)步驟)
本文通過詳細(xì)步驟介紹了SpringBoot 使用WebSocket功能,首先需要導(dǎo)入WebSocket坐標(biāo),編寫WebSocket配置類,用于注冊(cè)WebSocket的Bean,結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-02-02
SpringBoot2.x配置多數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot2.x配置多數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
淺談Java中向上造型向下造型和接口回調(diào)中的問題
這篇文章主要介紹了淺談Java中向上造型向下造型和接口回調(diào)中的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

