日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个注解搞懂 Sentinel,@SentinelResource总结

發布時間:2024/1/23 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个注解搞懂 Sentinel,@SentinelResource总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@SentinelResource可以說是Sentinel學習的突破口,搞懂了這個注解的應用,基本上就搞清楚了 Sentinel 的大部分應用場景。

一、@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找到對應的規則,這個是必須配置的。

entryType

entry 類型,可選項,有IN和OUT兩個選項,默認為 EntryType.OUT。

public enum EntryType {IN("IN"),OUT("OUT"); }

blockHandler

blockHandler 對應處理 BlockException 的函數名稱,可選項。
blockHandler 函數訪問范圍需要是 public,返回類型需要與原方法相匹配,參數類型需要和原方法相匹配并且最后加一個額外的參數,類型為 BlockException。

blockHandlerClass

blockHandler 函數默認需要和原方法在同一個類中,如果希望使用其他類的函數,則需要指定 blockHandlerClass 為對應的類的 Class 對象,注意對應的函數必需為 static 函數,否則無法解析。

fallback

fallback 函數名稱,可選項,用于在拋出異常的時候提供 fallback 處理邏輯。fallback 函數可以針對所有類型的異常(除了 exceptionsToIgnore 里面排除掉的異常類型)進行處理。

fallbackClass

fallbackClass的應用和blockHandlerClass類似,fallback 函數默認需要和原方法在同一個類中。
若希望使用其他類的函數,則可以指定 fallbackClass 為對應的類的 Class 對象,注意對應的函數必需為 static 函數,否則無法解析。

defaultFallback(since 1.6.0)

如果沒有配置defaultFallback方法,默認都會走到這里來。
默認的 fallback 函數名稱,可選項,通常用于通用的 fallback 邏輯。
默認 fallback 函數可以針對所有類型的異常(除了 exceptionsToIgnore 里面排除掉的異常類型)進行處理。若同時配置了 fallback 和 defaultFallback,則只有 fallback 會生效。

exceptionsToIgnore(since 1.6.0)

用于指定哪些異常被排除掉,不會計入異常統計中,也不會進入 fallback 邏輯中,而是會原樣拋出。

二、Sentinel切面配置

應用 @SentinelResource 注解,必須開啟對應的切面,引入SentinelResourceAspect。

1、AspectJ方式

Sentinel支持 AspectJ 的擴展用于自動定義資源、處理 BlockException 等,如果應用中開啟了 AspectJ,那么需要在 aop.xml 文件中引入對應的 Aspect:

<aspects><aspect name="com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect"/> </aspects>

2、Spring AOP 方式

如果應用中使用了 Spring AOP,需要在代碼中添加SentinelResourceAspect的Bean,通過配置的方式將 SentinelResourceAspect 注冊為一個 Spring Bean:

@Configuration public class SentinelAspectConfiguration {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();} }

三、總結

這節內容學習了@SentinelResource的相關屬性,以及在項目中通過切面開啟SentinelResource的方式,不過為什么使用@SentinelResource必須開啟切面?



作者:邴越
鏈接:https://www.jianshu.com/p/b61727df6ea5
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

總結

以上是生活随笔為你收集整理的一个注解搞懂 Sentinel,@SentinelResource总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。