Sentinel熔断处理

  • @SentinelResource注解的使用
/**
     * @SentinelResource使用此注解描述的方法,
     * 在此方法被访问时,会在sentinel的簇点链路中显示,
     * 此注解中指定的名字就是资源名,我们可以对这个资源
     * 的访问,按照指定的链路进行限流设计.
     *
     * 此注解中的blockHandlerClass用于指定,出现限流异常时的异常处理类,
     * blockHandler属性用于指定异常处理类中的方法(此方法的返回类型,参数
     * 要与 @SentinelResource注解描述的方法参数一致,可以加BlockException异常
     * 类型参数,而且方法必须是静态.)
     * fallbackClass 用于指定业务异常处理类,fallback用于指向业务处理类
     * 中的异常处理方法(此方法的返回类型,参数要与@SentinelResource注解描
     * 述的方法参数一致,可以加Throwable异常类型参数)
     * @return
     */
@Service
public class ResourceService {
    //此方法被访问时,会在sentinel的簇点链路中显示,访问时按指定链路限流设计
    @SentinelResource(value = "doGetResource",blockHandlerClass = ResourceBlockHandler.class,blockHandler = "call")
    public String doGetResource(){
        return "This is test03";
    }
}
@Component
@Slf4j
public class ResourceBlockHandler {
    //注意:此方法中的异常类型必须是BlockException,是所有限流异常的父类型
    public static String call(BlockException e){
        log.error(e.getMessage());
        return "别点了!";
    }
}
//Controller
@GetMapping("sentinel03")
public String doSentinel03(){
    String s = resourceService.doGetResource();
    return s;
}
  • 自定义限流、降级异常处理对象
//提供线程安全的自增自减操作,模拟慢调用,controller
private AtomicLong atomicLong = new AtomicLong(1);
@GetMapping("sentinel04")
public String doSentinel04() throws InterruptedException {
    long num = atomicLong.getAndIncrement();
    if (num%2==0){
        Thread.sleep(200);
    }
    return "Sentinel Test04~~~";
}
@Component
@Slf4j
public class ServiceBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        response.setCharacterEncoding("utf-8");//设置响应数据编码
        response.setContentType("text/html;charset=utf-8");//设置响应数据类型以及客户端显示内容的编码
        //向客户端响应一个json字符串
        Map<String,Object> map = new HashMap<>();
        map.put("status", 429);
        map.put("message", "频繁访问");
        String jsonstr = new ObjectMapper().writeValueAsString(map);
        //response.setStatus(429);
        //out.print("频繁访问");
        //默认iso,不会显示中文
        PrintWriter out = response.getWriter();
        out.print(jsonstr);
        out.flush();
        out.close();
    }
}
  • 热点规则(参数例外项)
    //Controller
    @GetMapping("/sentinel/findById")
    @SentinelResource("resource")
    public String doFindById(@RequestParam("id") Integer id){
        return "resource id is "+id;
    }

在这里插入图片描述

  • Sentinel授权规则
//http://localhost:8081/provider/sentineltest01?orgin=app1
//当客户端发起一个请求时,这个请求会被sentinel拦截器拦截,拦截器会调用
//对象的方法对请求进行拦截
@Component
public class DefaultRequestOriginParser implements RequestOriginParser {
    @Override
    public String parseOrigin(HttpServletRequest request) {
        //基于请求对象获取http请求行,请求头以及请求体中的数据;
        //这里解析后返回的数据,会与sentinel控制台
        //定义的值进行比对,然后基于黑白名单规则进行限制
		//	String origin = request.getParameter("origin");
		//	return origin;
        //http://176.201.103.11:8081/provider/sentineltest01
        //获取访问请求中的ip地址,基于ip地址进行黑白名单设计
        String ip= request.getRemoteAddr();
        return ip;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值