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 直接抛出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值