sentinel|sentinel 整合spring cloud限流的过程解析

spring cloud基于http进行服务调用,大致过程如下:

  • 服务提供端:提供http接口,并向服务中心注册服务信息
  • 服务消费端:将服务端的http接口作为本地服务,从注册中心读取服务提供端信息,使用feign发起远程调用
相关依赖
com.alibaba.cloudspring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloudspring-cloud-starter-alibaba-sentinel

示例 为简化处理,直接对url接口进行限流,不做服务调用
sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

application.yml
spring:application:name: hello-sentinelcloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:dashboard: localhost:8081

限流可使用本地配置、或者sentinel dashboard配置
HelloController
@RestControllerpublic class HelloController { @SentinelResource(value = "https://www.it610.com/article/hello", blockHandler = "blockHandle")@RequestMapping("/hello")public String hello(){return "hello"; }public String blockHandle(BlockException e){e.printStackTrace(); return "被限流了"; }

************
本地限流配置
sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

CustomFlowRule
public class CustomFlowRule implements InitFunc { @Overridepublic void init() throws Exception {List 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); }}

【sentinel|sentinel 整合spring cloud限流的过程解析】META-INF/services/com.alibaba.csp.sentinel.init.InitFunc
com.example.demo.rule.CustomFlowRule

jmeter 测试
sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

2022-03-27 22:30:50.534INFO 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.547INFO 1791 --- [main] c.a.c.n.registry.NacosServiceRegistry: nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished2022-03-27 22:30:50.557INFO 1791 --- [main] com.example.demo.DemoApplication: Started DemoApplication in 2.227 seconds (JVM running for 2.824)2022-03-27 22:31:04.044INFO 1791 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]: Initializing Spring DispatcherServlet 'dispatcherServlet'2022-03-27 22:31:04.044INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet 'dispatcherServlet'2022-03-27 22:31:04.049INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 5 msINFO: Sentinel log output type is: fileINFO: Sentinel log charset is: utf-8INFO: Sentinel log base directory is: /Users/huli/logs/csp/INFO: Sentinel log name use pid is: falsecom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowException

************
sentinel dashboard配置限流
启动sentinel dashboard
java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar 参数说明:-Dserver.port=8081:指定控制台启动端口-Dcsp.sentinel.dashboard.server:指定控制台地址和端口-Dproject.name=sentinel-dashboard:指定控制台项目名称

localhost:8081,控制台配置流控策略
sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

说明:若需使用本地降级方法,需在下方的hello配置流控规则
jmeter 测试
sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

sentinel|sentinel 整合spring cloud限流的过程解析
文章图片

2022-03-28 08:50:29.165INFO 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.198INFO 853 --- [main] c.a.c.n.registry.NacosServiceRegistry: nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished2022-03-28 08:50:29.210INFO 853 --- [main] com.example.demo.DemoApplication: Started DemoApplication in 3.315 seconds (JVM running for 4.03)2022-03-28 08:52:05.792INFO 853 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]: Initializing Spring DispatcherServlet 'dispatcherServlet'2022-03-28 08:52:05.793INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet: Initializing Servlet 'dispatcherServlet'2022-03-28 08:52:05.802INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet: Completed initialization in 9 msINFO: Sentinel log output type is: fileINFO: Sentinel log charset is: utf-8INFO: Sentinel log base directory is: /Users/huli/logs/csp/INFO: Sentinel log name use pid is: falsecom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowException

到此这篇关于sentinel 整合spring cloud限流的过程解析的文章就介绍到这了,更多相关sentinel 整合spring cloud限流内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读