在前面我们对Sentinel做了一个详细的介绍,可以手动的通过Sentinel提供的SphU类来保护资源。
这种做法不好的地方在于每个需要限制的地方都得写代码,从 0.1.1 版本开始,Sentinel 提供了 @SentinelResource 注解的方式,非常方便。
要使用注解来保护资源需要引入下面的Maven依赖:
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> <version>1.4.1</version></dependency>
引入之后我们需要配置SentinelResourceAspect切面让其生效,因为是通过SentinelResourceAspect切面来实现的,我这边以Spring Boot中使用进行配置示列:
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
@Configurationpublic class AopConfiguration {
@Bean public SentinelResourceAspect sentinelResourceAspect() { return new SentinelResourceAspect(); }
}
然后在需要限制的方法上加SentinelResource注解即可:
@SentinelResource(value = "get", blockHandler = "exceptionHandler")@Overridepublic String get(String id) { return "http://cxytiandi.com";}
public String exceptionHandler(String id, BlockException e) { e.printStackTrace(); return "错误发生在" + id;}
表示资源名,必填项
处理 BlockException 的方法名,可选项。若未配置,则将 BlockException 直接抛出。
如果你不想让异常处理方法跟业务方法在同一个类中,可以使用 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
业务方法:
@SentinelResource(value = "get2", blockHandler = "handleException", blockHandlerClass = { ExceptionUtil.class })@Overridepublic String get2() { return "http://cxytiandi.com";}
异常处理类:
import com.alibaba.csp.sentinel.slots.block.BlockException;
public final class ExceptionUtil {
public static String handleException(BlockException ex) { System.err.println("错误发生: " + ex.getClass().getCanonicalName()); return "error"; }
}
我们可以在Spring Boot的启动类中定义规则,然后快速访问接口,就可以看出效果啦,或者用压力测试工具ab等。
@SpringBootApplicationpublic class App { public static void main(String[] args) { initFlowRules(); SpringApplication.run(App.class, args); }
private static void initFlowRules() { List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule(); rule.setResource("get"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); rule.setCount(1); rules.add(rule);
rule = new FlowRule(); rule.setResource("get2"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); rule.setCount(1); rules.add(rule);
FlowRuleManager.loadRules(rules); }}
只需要配置了SentinelResourceAspect就可以使用注解,我们来简单的看下SentinelResourceAspect的源码
@Aspectpublic class SentinelResourceAspect extends AbstractSentinelAspectSupport {
@Pointcut("@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)") public void sentinelResourceAnnotationPointcut() { }
@Around("sentinelResourceAnnotationPointcut()") public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable { // 获取当前访问的方法 Method originMethod = resolveMethod(pjp); // 获取方法上的SentinelResource注解 SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class); if (annotation == null) { // Should not go through here. throw new IllegalStateException("Wrong state for SentinelResource annotation"); } // 获取资源名 String resourceName = getResourceName(annotation.value(), originMethod); EntryType entryType = annotation.entryType(); Entry entry = null; try { entry = SphU.entry(resourceName, entryType, 1, pjp.getArgs()); Object result = pjp.proceed(); return result; } catch (BlockException ex) { // 处理被限制的异常,回调事先配置的异常处理方法 return handleBlockException(pjp, annotation, ex); } catch (Throwable ex) { Tracer.trace(ex); throw ex; } finally { if (entry != null) { entry.exit(); } } }}
上面是整个切面的代码,对所有加了SentinelResource注解的方法进去切入。细节代码在AbstractSentinelAspectSupport中,大家自己去看看。