美文网首页
五.Sentinel SentinelResource注解 属性

五.Sentinel SentinelResource注解 属性

作者: G__yuan | 来源:发表于2021-03-25 11:40 被阅读0次

SentinelResource注解

1.value

作用:指定资源名称
是否必须:是

@SentinelResource(value = "test/get1")

2.entryType

作用:entry类型,标记流量的方向,指明是出口流量,还是入口流量;取值 IN/OUT ,默认是OUT。
是否必须:否

@SentinelResource(value = "test/get1",entryType = EntryType.IN)

3.blockHandler

作用:处理BlockException的函数名称,函数要求为:

  • 必须是public
  • 返回类型与原方法一致
  • 参数类型需要和原方法相匹配,并在最后加上BlockException类型的参数
  • 默认需和原方法在同一个类中,如果希望使用其他类的函数,可配置blockHandlerClass,并指定blockHandlerClass里面的方法
    是否必须:否
@GetMapping("/get")
    @SentinelResource(value = "test/get1",blockHandler = "testGetBlockHandler")
    public Object getInstanceInfo(){
        ServiceInstanceInfo serviceInstance = neofaithDiscoveryClient.getServiceInstance("neofaith-wechat-web");
        String s = JSONObject.toJSONString(serviceInstance);
        System.out.println("neofaithDiscoveryClient:"+s);
        List<ServiceInstance> instances = discoveryClient.getInstances("neofaith-wechat-web");
        String ss = JSONObject.toJSONString(instances);
        System.out.println("discoveryClient:"+ss);
        return  ss;
    }
    public Object testGetBlockHandler(BlockException e){
        e.printStackTrace();
        return "被限流或者降级了";
    }

4.blockHandlerClass

作用:存放blockHandler的类。对应的处理函数必须static修饰,否则无法解析。
函数要求为:

  • 必须是public
  • 返回类型与原方法一致
  • 参数类型需要和原方法相匹配,并在最后加上BlockException类型的参数
    是否必须:否
@GetMapping("/get")
    @SentinelResource(value = "test/get1",entryType = EntryType.IN, blockHandler = "testGetBlockHandler",
            blockHandlerClass = BlockExceptionHandler.class
    )
    public Object getInstanceInfo(){
        ServiceInstanceInfo serviceInstance = neofaithDiscoveryClient.getServiceInstance("neofaith-wechat-web");
        String s = JSONObject.toJSONString(serviceInstance);
        System.out.println("neofaithDiscoveryClient:"+s);
        List<ServiceInstance> instances = discoveryClient.getInstances("neofaith-wechat-web");
        String ss = JSONObject.toJSONString(instances);
        System.out.println("discoveryClient:"+ss);
        return  ss;
    }

public class BlockExceptionHandler {
    public static Object testGetBlockHandler(BlockException e){
        e.printStackTrace();
        return "被限流或者降级了";
    }
}

5.fallback

作用:用于在抛出异常的时候提供fallback处理逻辑。fallback函数可以针对所有类型的异常(除了execptionsToIgnore 里面排除掉的异常类型)进行处理。函数要求:

  • 返回类型与原方法一致
  • 参数类型需要和原方法相匹配,Sentinel 1.6版本之后,也可在方法最后加上Throwable类型的参数
  • 默认需和原方法在同一个类中,若希望使用其他类的函数,可配置fallbackClass,并指定fallbackClass里面的方法
    是否必须:否
    @GetMapping("/get")
    @SentinelResource(value = "test/get1", blockHandler = "testGetBlockHandler",
            blockHandlerClass = BlockExceptionHandler.class,fallback = "testFallbackHandler"
    )
    public Object getInstanceInfo(){
        throw  new RuntimeException("抛出异常");
    }
    public Object testFallbackHandler(Throwable e){
        System.out.println(e.getMessage());
        System.out.println("-------------fallbackhandler----------");
        return "捕获所有异常,并处理";
    }

6.fallbackClass

作用:存放fallback的类。对应的处理函数必须static修饰,否则无法解析,其他要求:同fallback。
是否必须:否
代码与blockHandlerClass类似,就不贴出来了。

7.defaultFallback

作用:用于通用的 fallback 逻辑。默认fallback函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,以fallback为准。函数要求:

  • 返回类型与原方法一致
  • 方法参数列表为空,或者有一个Throwable类型的参数
  • 默认需要和原方法在同一个类中,若希望使用其他类的函数,可配置fallbackclass,并指定fallbackClass里面的方法。
    是否必须:否

8.exceptionsToIgnore

作用:指定排除掉哪些异常。排除的异常不会计入异常统计,也不会进入fallback逻辑,而是原样抛出
是否必须:否

9.exceptionsToTrace

作用:需要trace的异常

相关文章

网友评论

      本文标题:五.Sentinel SentinelResource注解 属性

      本文链接:https://www.haomeiwen.com/subject/wpxhhltx.html