我是靠谱客的博主 苗条糖豆,最近开发中收集的这篇文章主要介绍sentinel整合springboot,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

sentinel整合springboot

一、添加依赖

 <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
  • 当注册不到sentinel控制界面,可进行版本的替换

二、配置文件

spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
//sentinel界面的地址

三、下载启动控制台

  • https://github.com/alibaba/Sentinel/releases sentinel控制台下载地址,用户名/密码为sentinel
  • java -jar sentinel-dashboard-1.8.0.jar 启动控制台

四、将项目注册到控制台

@RestController
public class TestSentinelController {
@GetMapping(value = "/test")
@SentinelResource(value = "testSentinel")
public String test(){
return "test sentinel";
}
}
  • 如果您在控制台没有找到应用,请调用一下进行了 Sentinel 埋点(加了@SentinelResource注解)的 URL 或方法,因为 Sentinel 使用了 lazy load 策略。

五、配置限流规则

5.1 默认限流异常处理
  • URL 限流触发后默认处理逻辑是,直接返回 “Blocked by Sentinel (flow limiting)”。
5.2 自定义限流处理
  • 在同一个类中
 @GetMapping(value = "/test")
@SentinelResource(value = "testSentinel",blockHandler = "handleException")
public String test(@RequestParam Integer id){
return "test sentinel";
}
public String handleException(Integer id,BlockException ex){
//可进行不同异常的处理逻辑
return "访问次数频繁,请稍后处理";
}
  • 外置类(常用)
 @GetMapping(value = "/test")
@SentinelResource(value = "testSentinel",blockHandler = "handleException",blockHandlerClass = TestException.class)
public String test(@RequestParam Integer id){
return "test sentinel";
}
@Component
public class TestException {
public static String handleException(Integer id,BlockException ex){
return "访问次数频繁,请稍后处理";
}
}
  • 需要加上static修饰符才可被识别到

六、服务降级的处理

  • 在同一个类中
 @GetMapping(value = "/test")
@SentinelResource(value = "testSentinel",fallback = "fail")
public String test(@RequestParam Integer id){
if (id == 2){
throw new RuntimeException("出现了异常");
}
return "test sentinel";
}
public String fail(Integer id,Throwable e){
//针对不同异常不同处理
if (e instanceof RuntimeException){
System.out.println("运行时异常");
}
return "服务进行降级处理";
}
  • 外置类
 @GetMapping(value = "/test")
@SentinelResource(value = "testSentinel",fallback = "fail",fallbackClass =TestException.class)
public String test(@RequestParam Integer id){
if (id == 2){
throw new RuntimeException("出现了异常");
}
return "test sentinel";
}
@Component
public class TestException {
public static String fail(Integer id,Throwable e){
//针对不同异常不同处理
if (e instanceof RuntimeException){
System.out.println("运行时异常");
}
return "服务进行降级处理";
}
}
  • 需要加上static修饰符才可被识别到

七、 fallback和blockHandler的区别

fallback:若本接口出现未知异常,则调用fallback指定的接口。

blockHandler:若本次访问被限流或服务降级,则调用blockHandler指定的接口。

@GetMapping(value = "/test")
@SentinelResource(value = "testSentinel",fallback = "fail",fallbackClass = TestException.class,blockHandler = "handleException",blockHandlerClass = TestException.class)
public String test(@RequestParam Integer id){
if (id == 2){
throw new RuntimeException("出现了异常");
}
return "test sentinel";
}
//限流和降级处理类
@Component
public class TestException {
public static String handleException(Integer id,BlockException ex){
return "访问次数频繁,请稍后处理";
}
public static String fail(Integer id,Throwable e){
if (e instanceof RuntimeException){
System.out.println("运行时异常");
}
return "服务进行降级处理";
}
}

总结: fallback是针对方法出现异常了,则会进入fallback方法。blockhandler是针对流控设置,超出规则,则会进入blockhandler方法。若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出BlockException时只会进入 blockHandler 处理逻辑。若未配置 blockHandler、fallback 和 defaultFallback,则被限流降级时会将 BlockException 直接抛出。

最后

以上就是苗条糖豆为你收集整理的sentinel整合springboot的全部内容,希望文章能够帮你解决sentinel整合springboot所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部