本文共 5453 字,大约阅读时间需要 18 分钟。
springboot
整合 sentinel
简单示例创建 springboot
应用,命名为 Sentinel-Quick-start
Maven
依赖com.alibaba.csp sentinel-core 1.8.0
Controller
@Controllerpublic class TestController { @RequestMapping(path = { "/hello"}, method = RequestMethod.GET) @ResponseBody public String hello() { try { // 设置一个资源名称为 Hello Entry ignored = SphU.entry("Hello"); System.out.println("Hello Sentinel"); return "Hello Sentinel"; } catch (BlockException e) { System.out.println("系统繁忙,请稍后"); e.printStackTrace(); return "系统繁忙,请稍后"; } } /** * 使用代码编写流控规则,项目中不推荐使用,这是硬编码方式 * * 注解 @PostConstruct 的含义是:本类构造方法执行结束后执行 */ @PostConstruct public void initFlowRule() { /* 1.创建存放限流规则的集合 */ Listrules = new ArrayList<>(); /* 2.创建限流规则 */ FlowRule rule = new FlowRule(); /* 定义资源,表示 Sentinel 会对哪个资源生效 */ rule.setResource("Hello"); /* 定义限流的类型(此处使用 QPS 作为限流类型) */ rule.setGrade(RuleConstant.FLOW_GRADE_QPS); /* 定义 QPS 每秒通过的请求数 */ rule.setCount(2); /* 3.将限流规则存放到集合中 */ rules.add(rule); /* 4.加载限流规则 */ FlowRuleManager.loadRules(rules); }}
Sentinel
控制台下载 Sentinel
控制台 jar
包:
启动 Sentinel
控制台,如下图所示
java -Dserver.port=9000 -jar sentinel-dashboard-1.8.0.jar
Sentinel
控制台:http://127.0.0.1:9000/
Sentinel
springboot
应用接入 Sentinel
控制台Maven
依赖com.alibaba.csp sentinel-transport-simple-http 1.8.0
idea
中设置本地应用的 JVM
启动参数-Dcsp.sentinel.dashboard.server=127.0.0.1:9000 Sentinel控制台的地址和端口号-Dproject.name=Sentinel-Quick-Start 本地应用在控制台中的名称
第一次查看控制台,需要先访问一次被限流控制的接口,否则控制台中没有东西
Sentinel
提供的设置流控规则的方式Sentinel
控制台中动态的设置流控规则(推荐使用)Sentinel
控制台中动态的设置流控规则TestController
类删除使用代码编写的流控规则,项目中不推荐使用,这是硬编码方式
@Controllerpublic class TestController { @RequestMapping(path = { "/hello"}, method = RequestMethod.GET) @ResponseBody public String hello() { try { Entry ignored = SphU.entry("Hello"); System.out.println("Hello Sentinel"); return "Hello Sentinel"; } catch (BlockException e) { System.out.println("系统繁忙,请稍后"); e.printStackTrace(); return "系统繁忙,请稍后"; } }}
Sentinel
定义资源的方式使用这种方式当资源发生限流后会抛出 BlockException
异常。这个时候可以捕获异常,进行限流之后的逻辑处理,关键代码如下
@RequestMapping(path = { "/hello"}, method = RequestMethod.GET)@ResponseBodypublic String hello() { try { Entry ignored = SphU.entry("Hello"); System.out.println("Hello Sentinel"); return "Hello Sentinel"; } catch (BlockException e) { System.out.println("系统繁忙,请稍后"); e.printStackTrace(); return "系统繁忙,请稍后"; }}
使用的 API
为 SphO
,限流后返回的值为 boolean
类型。注意:SphO.entry
必须和 SphO.exit
成对出现 否则会报错
@GetMapping("/boolean")public boolean returnBoolean() { // 使用限流规则 if (SphO.entry("Sentinel-boolean")){ try { System.out.println("Hello Sentinel"); return true; }finally { // 限流的出口 SphO.exit(); } } else { // 限流后进行的操作 System.out.println("系统繁忙,请稍后再试"); return false; }}
@EnableAsync
,表示 SpringBoo
t 项目开启异步调用支持@SpringBootApplication@EnableAsyncpublic class SentinelQuickStartApplication { public static void main(String[] args) { SpringApplication.run(SentinelQuickStartApplication.class, args); }}
AsyncService
编写异步调用的方法@Servicepublic class AsyncService { // Async表示方法为异步调用 @Async public void hello(){ System.out.println("异步调用开始======"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("异步调用结束====="); }}
Controller
层@Autowiredprivate AsyncService asyncService;@GetMapping("/async")public void async() { // 1.进行限流控制 AsyncEntry asyncEntry = null; try { asyncEntry = SphU.asyncEntry("Sentinel_Async"); // 限流入口 asyncService.hello(); // 异步调用方法 System.out.println("异步测试"); } catch (BlockException e) { e.printStackTrace(); System.out.println("系统繁忙请稍后再试"); } finally { if (asyncEntry != null) { asyncEntry.exit(); // 限流出口 } }}
Sentinel
支持通过 @SentinelResource
注解来定义资源并配置 blockHandler
函数来进行限流之后的处理
Maven
依赖com.alibaba.csp sentinel-annotation-aspectj 1.8.0
AspectJ
的配置类@Configurationpublic class SentinelAspectConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect(){ return new SentinelResourceAspect(); }}
Controller
层@Controllerpublic class Test2Controller { // value:资源名称 blockHandler:设置限流或降级处理的类 @SentinelResource(value = "sentinel_springcloud", blockHandler = "exceptionHandler") @ResponseBody @RequestMapping(path = "/ann", method = RequestMethod.GET) public String ann() { // 使用限流规则 return "Hello Sentinel"; } public String exceptionHandler(@NotNull BlockException e) { e.printStackTrace(); return "系统繁忙,请稍后再试"; }}
转载地址:http://ssinz.baihongyu.com/