一个注解搞懂 Sentinel,@SentinelResource总结
@SentinelResource可以說是Sentinel學(xué)習(xí)的突破口,搞懂了這個注解的應(yīng)用,基本上就搞清楚了 Sentinel 的大部分應(yīng)用場景。
一、@SentinelResource 解析
Sentinel 提供了 @SentinelResource 注解用于定義資源,并提供了 AspectJ 的擴展用于自動定義資源、處理 BlockException 等。
1、SentinelResource源碼
查看 Sentinel的源碼,可以看到 SentinelResource 定義了value,entryType,resourceType,blockHandler,fallback,defaultFallback等屬性。
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface SentinelResource {/*** @return */String value() default "";/*** @return the entry type (inbound or outbound), outbound by default*/EntryType entryType() default EntryType.OUT;/*** @return the classification (type) of the resource* @since 1.7.0*/int resourceType() default 0;/*** @return name of the block exception function, empty by default*/String blockHandler() default "";/*** The {@code blockHandler} is located in the same class with the original method by default.* However, if some methods share the same signature and intend to set the same block handler,* then users can set the class where the block handler exists. Note that the block handler method* must be static.* @return the class where the block handler exists, should not provide more than one classes*/Class<?>[] blockHandlerClass() default {};/*** @return name of the fallback function, empty by default*/String fallback() default "";/*** The {@code defaultFallback} is used as the default universal fallback method.* It should not accept any parameters, and the return type should be compatible* @return name of the default fallback method, empty by default* @since 1.6.0*/String defaultFallback() default "";/*** The {@code fallback} is located in the same class with the original method by default.* However, if some methods share the same signature and intend to set the same fallback,* then users can set the class where the fallback function exists. Note that the shared fallback method* @return the class where the fallback method is located (only single class)* @since 1.6.0*/Class<?>[] fallbackClass() default {};/*** @return the list of exception classes to trace, {@link Throwable} by default* @since 1.5.1*/Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};/*** Indicates the exceptions to be ignored. Note that {@code exceptionsToTrace} should* not appear with {@code exceptionsToIgnore} at the same time, or {@code exceptionsToIgnore}* will be of higher precedence.** @return the list of exception classes to ignore, empty by default* @since 1.6.0*/Class<? extends Throwable>[] exceptionsToIgnore() default {}; }2、屬性說明
參考源碼的注釋,逐個解釋下這幾個屬性的作用。
value
資源名稱,必需項,因為需要通過resource name找到對應(yīng)的規(guī)則,這個是必須配置的。
entryType
entry 類型,可選項,有IN和OUT兩個選項,默認(rèn)為 EntryType.OUT。
public enum EntryType {IN("IN"),OUT("OUT"); }blockHandler
blockHandler 對應(yīng)處理 BlockException 的函數(shù)名稱,可選項。
blockHandler 函數(shù)訪問范圍需要是 public,返回類型需要與原方法相匹配,參數(shù)類型需要和原方法相匹配并且最后加一個額外的參數(shù),類型為 BlockException。
blockHandlerClass
blockHandler 函數(shù)默認(rèn)需要和原方法在同一個類中,如果希望使用其他類的函數(shù),則需要指定 blockHandlerClass 為對應(yīng)的類的 Class 對象,注意對應(yīng)的函數(shù)必需為 static 函數(shù),否則無法解析。
fallback
fallback 函數(shù)名稱,可選項,用于在拋出異常的時候提供 fallback 處理邏輯。fallback 函數(shù)可以針對所有類型的異常(除了 exceptionsToIgnore 里面排除掉的異常類型)進(jìn)行處理。
fallbackClass
fallbackClass的應(yīng)用和blockHandlerClass類似,fallback 函數(shù)默認(rèn)需要和原方法在同一個類中。
若希望使用其他類的函數(shù),則可以指定 fallbackClass 為對應(yīng)的類的 Class 對象,注意對應(yīng)的函數(shù)必需為 static 函數(shù),否則無法解析。
defaultFallback(since 1.6.0)
如果沒有配置defaultFallback方法,默認(rèn)都會走到這里來。
默認(rèn)的 fallback 函數(shù)名稱,可選項,通常用于通用的 fallback 邏輯。
默認(rèn) fallback 函數(shù)可以針對所有類型的異常(除了 exceptionsToIgnore 里面排除掉的異常類型)進(jìn)行處理。若同時配置了 fallback 和 defaultFallback,則只有 fallback 會生效。
exceptionsToIgnore(since 1.6.0)
用于指定哪些異常被排除掉,不會計入異常統(tǒng)計中,也不會進(jìn)入 fallback 邏輯中,而是會原樣拋出。
二、Sentinel切面配置
應(yīng)用 @SentinelResource 注解,必須開啟對應(yīng)的切面,引入SentinelResourceAspect。
1、AspectJ方式
Sentinel支持 AspectJ 的擴展用于自動定義資源、處理 BlockException 等,如果應(yīng)用中開啟了 AspectJ,那么需要在 aop.xml 文件中引入對應(yīng)的 Aspect:
<aspects><aspect name="com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect"/> </aspects>2、Spring AOP 方式
如果應(yīng)用中使用了 Spring AOP,需要在代碼中添加SentinelResourceAspect的Bean,通過配置的方式將 SentinelResourceAspect 注冊為一個 Spring Bean:
@Configuration public class SentinelAspectConfiguration {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();} }三、總結(jié)
這節(jié)內(nèi)容學(xué)習(xí)了@SentinelResource的相關(guān)屬性,以及在項目中通過切面開啟SentinelResource的方式,不過為什么使用@SentinelResource必須開啟切面?
作者:邴越
鏈接:https://www.jianshu.com/p/b61727df6ea5
來源:簡書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
總結(jié)
以上是生活随笔為你收集整理的一个注解搞懂 Sentinel,@SentinelResource总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java实体映射工具MapStruct
- 下一篇: 携程基于Quasar协程的NIO实践