Spring cloud - Hystrix源碼

其實只是Hystrix初始化部分,我們從源碼的角度分析一下@EnableCircuitBreaker以及@HystrixCommand注解的初始化過程。

從@EnableCircuitBreaker入手

我們是通過在啟動類添加@EnableCircuitBreaker注解啟用Hystrix的,所以,源碼解析也要從這個注解入手。

該注解已經被標了@Deprecated了,不過,擋不住…

Spring關于@Enablexxxx注解的套路:通過@Import注解引入了EnableCircuitBreakerImportSelector.class:

@Deprecated
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableCircuitBreakerImportSelector.class)
public @interface EnableCircuitBreaker {}

跟蹤EnableCircuitBreakerImportSelector,繼承自SpringFactoryImportSelector,注意SpringFactoryImportSelector類的泛型類型為EnableCircuitBreaker:

@Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableCircuitBreakerImportSelector extends SpringFactoryImportSelector<EnableCircuitBreaker> {@Overrideprotected boolean isEnabled() {return getEnvironment().getProperty("spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE);}}

看一下類圖:
在這里插入圖片描述

屬性annotationClass通過GenericTypeResolver.resolveTypeArgument方法獲取當前對象的泛型參數的具體類型,現在我們知道是EnableCircuitBreaker(在org.springframework.cloud.client.circuitbreaker包下)。

public abstract class SpringFactoryImportSelector<T>implements DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {private final Log log = LogFactory.getLog(SpringFactoryImportSelector.class);private ClassLoader beanClassLoader;private Class<T> annotationClass;private Environment environment;@SuppressWarnings("unchecked")protected SpringFactoryImportSelector() {this.annotationClass = (Class<T>) GenericTypeResolver.resolveTypeArgument(this.getClass(),SpringFactoryImportSelector.class);}

@Import注解我們前面做過詳細分析了,實現了DeferredImportSelector接口表示延遲加載全限定名稱為selectImports方法返回的類。看selectImports方法:

@Overridepublic String[] selectImports(AnnotationMetadata metadata) {//Enable參數沒有打開的話就不加載if (!isEnabled()) {return new String[0];}AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(this.annotationClass.getName(), true));Assert.notNull(attributes, "No " + getSimpleName() + " attributes found. Is " + metadata.getClassName()+ " annotated with @" + getSimpleName() + "?");// Find all possible auto configuration classes, filtering duplicates//SPI機制調用spring.factories文件下的EnableCircuitBreakerList<String> factories = new ArrayList<>(new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(this.annotationClass, this.beanClassLoader)));if (factories.isEmpty() && !hasDefaultFactory()) {throw new IllegalStateException("Annotation @" + getSimpleName()+ " found, but there are no implementations. Did you forget to include a starter?");}if (factories.size() > 1) {// there should only ever be one DiscoveryClient, but there might be more than// one factorythis.log.warn("More than one implementation " + "of @" + getSimpleName()+ " (now relying on @Conditionals to pick one): " + factories);}return factories.toArray(new String[factories.size()]);}

selectImports方法的主要功能就是調用SpringFactoriesLoader.loadFactoryNames方法,該方法的目的是通過SPI機制讀取spring.factories文件中的org.springframework.cloud.client.circuitbreaker相關配置,返回。

返回的信息是類全限定名,從selectImports方法作用可知,這些類會加載到Spring Ioc容器中。

org.springframework.cloud.client.circuitbreaker在spring-cloud-netflix-hystrix-2.2.10.RELEASE包下:
org.springframework.cloud.client.circuitbreaker
所以該配置下的org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration會被調用并加載到Spring IoC容器中。

HystrixCircuitBreakerConfiguration

跟蹤配置類HystrixCircuitBreakerConfiguration:


/*** @author Spencer Gibb* @author Christian Dupuis* @author Venil Noronha*/
@Configuration(proxyBeanMethods = false)
public class HystrixCircuitBreakerConfiguration {@Beanpublic HystrixCommandAspect hystrixCommandAspect() {return new HystrixCommandAspect();}@Beanpublic HystrixShutdownHook hystrixShutdownHook() {return new HystrixShutdownHook();}

注入了一個叫HystrixCommandAspect 的bean,從名字上看,應該是關于HystrixCommand的切面,用到了AOP。其實也容易理解,加了@HystrixCommand注解的方法的執行邏輯發生了變化:方法增強了執行時長是否超時、執行是否成功(是否返回異常)、超時或異常的情況下調用fallback…方法功能的增強正是AOP的強項。

繼續跟蹤HystrixCommandAspect 類。

HystrixCommandAspect

打開代碼,首先是非常熟悉的@Aspect注解:

@Aspect
public class HystrixCommandAspect {

表明當前類是AOP的切面。

繼續看代碼:

@Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)")public void hystrixCommandAnnotationPointcut() {}@Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)")public void hystrixCollapserAnnotationPointcut() {}@Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()")public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable {

添加了針對注解HystrixCommand的切點,以及針對該切點的環繞增強方法methodsAnnotatedWithHystrixCommand。

最終的熔斷、限流、服務降級功能,都是在這個methodsAnnotatedWithHystrixCommand方法里實現的,繼續向下研究這個方法的代碼邏輯,需要RxJava背景知識做支撐。

有空再聊:) ~!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/161003.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/161003.shtml
英文地址,請注明出處:http://en.pswp.cn/news/161003.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

最新PHP熊貓頭圖片表情斗圖生成源碼

這是一款能生成熊貓頭表情斗圖的自適應系統源碼&#xff0c;無論是在電腦還是手機上都可以正常使用&#xff01;這個源碼集成了搜狗搜索圖片接口&#xff0c;可以輕松地一鍵搜索數百萬張圖片&#xff0c;并且還包含了表情制作等功能模塊。對于一些新站來說&#xff0c;這是一個…

Cloud微服務

當我們談論“云微服務”時&#xff0c;通常是指基于云計算和微服務架構的應用程序開發和部署模型。以下是關于云微服務的一些詳細信息&#xff1a; 微服務架構&#xff1a; 微服務架構是一種軟件設計和開發模式&#xff0c;將應用程序劃分為一組小型、獨立的服務單元。每個服…

c++ LRU(最近最少使用)緩存機制

// LRU(最近最少使用)緩存機制 #ifndef _ZD_LRU_CACHE_H_ #define _ZD_LRU_CACHE_H_#include <unordered_map> #include <list> #include <mutex>class ZDLRUCahce { public:ZDLRUCahce(int capacity): m_capacity(capacity){}~ZDLRUCahce(){}// 1.key不存在…

JavaScript的過濾大師:深度解析Filter用法

JavaScript的過濾大師&#xff1a;深度解析Filter用法 前言基礎篇filter的基本用法語法示例 自定義過濾函數數組對象的過濾復雜條件的篩選常見應用場景性能優化注意性能的建議在大規模數據集下的優化方法 案例分析實際案例&#xff1a;用戶篩選使用 filter 方法解決問題代碼優化…

產品工程師工作的職責十篇(合集)

一、崗位職責的作用意義 1.可以最大限度地實現勞動用工的科學配置; 2.有效地防止因職務重疊而發生的工作扯皮現象; 3.提高內部競爭活力&#xff0c;更好地發現和使用人才; 4.組織考核的依據; 5.提高工作效率和工作質量; 6.規范操作行為; 7.減少違章行為和違章事故的發生…

好視通視頻會議系統(fastmeeting) toDownload.do接口存在任意文件讀取漏洞復現 [附POC]

文章目錄 好視通視頻會議系統(fastmeeting) toDownload.do接口存在任意文件讀取漏洞復現 [附POC]0x01 前言0x02 漏洞描述0x03 影響版本0x04 漏洞環境0x05 漏洞復現1.訪問漏洞環境2.構造POC3.復現 0x06 修復建議 好視通視頻會議系統(fastmeeting) toDownload.do接口存在任意文件…

超詳細!新手必看!STM32-通用定時器簡介與知識點概括

一、通用定時器的功能 在基本定時器功能的基礎上新增功能&#xff1a; 通用定時器有4個獨立通道&#xff0c;且每個通道都可以用于下面功能。 &#xff08;1&#xff09;輸入捕獲&#xff1a;測量輸入信號的周期和占空比等。 &#xff08;2&#xff09;輸出比較&#xff1a;產…

Gradle常用命令與參數依賴管理和版本決議

一、Gradle 常用命令與參數 本課程全程基于 Gradle8.0 環境 1、Gradle 命令 介紹 gradle 命令之前我們先來了解下 gradle 命令怎么在項目中執行。 1.1、gradlew gradlew 即 Gradle Wrapper&#xff0c;在學習小組的第一課時已經介紹過了這里就不多贅述。提一下執行命令&am…

.Net6使用WebSocket與前端進行通信

1. 創建類WebSocketTest&#xff1a; using System.Net.WebSockets; using System.Text;namespace WebSocket.Demo {public class WebSocketTest{//當前請求實例System.Net.WebSockets.WebSocket socket null;public async Task DoWork(HttpContext ctx){socket await ctx.We…

為UE和Unity開發者準備的Godot指南

為UE和Unity開發者準備的Godot指南 ——兩位大哥打架&#xff0c;請帶上我 這兩天游戲行業又開始熱鬧了&#xff0c;昨天兩條信息直接刷爆朋友圈&#xff0c;最大的兩家游戲引擎公司懟起來了。 《為Unity開發者準備的虛幻引擎指南》&#xff1a; 為Unity開發者準備的虛幻引擎指…

sso 四種授權模式

單點登錄 單點登錄&#xff0c;英文是 Single Sign On&#xff08;縮寫為 SSO&#xff09;。即多個站點共用一臺認證授權服務器&#xff0c;用戶在站點登錄后&#xff0c;可以免登錄訪問其他所有站點。而且&#xff0c;各站點間可以通過該登錄狀態直接交互。例如&#xff1a; …

C#編程題分享(3)

n的階乘問題 輸?整數n&#xff0c;輸出n的階乘。 int n Convert.ToInt32(Console.ReadLine()); int jiecheng 1; for (int i 1; i < n 1; i) {jiecheng * i; // 1 * 2 * 3 * .....} Console.WriteLine("{0}的階乘是&#xff1a;{1}", n, jiecheng); q^n次…

Clickhouse設置多磁盤存儲策略

設置多磁盤存儲 clickhouse安裝完成以后&#xff0c;配置了一個默認的存儲空間&#xff0c; 這個只能配置一個目錄&#xff0c;如果要使用多個磁盤目錄&#xff0c;則需要配置磁盤組策略 查看當前的存儲策略 select name, path, formatReadableSize(free_space) as free, fo…

Django DRF版本號的處理

在restful規范中&#xff0c;后端的API中需要體現版本。如果項目比較大&#xff0c;需要些很多的視圖類&#xff0c;在每一個類中都寫一遍會比較麻煩&#xff0c;所以drf中也支持了全局配置。在每個版本處理的類中還定義了reverse方法&#xff0c;他是用來反向生成URL并攜帶相關…

還記得高中生物書上的莫斯密碼嗎?利用Python破解摩斯密碼的代碼示例!

文章目錄 前言摩爾斯電碼Python實現摩斯密碼對照表加密解密測試 完整代碼總結關于Python技術儲備一、Python所有方向的學習路線二、Python基礎學習視頻三、精品Python學習書籍四、Python工具包項目源碼合集①Python工具包②Python實戰案例③Python小游戲源碼五、面試資料六、Py…

Arduino驅動MLX90614紅外溫度傳感器(溫濕度傳感器)

目錄 1、傳感器特性 2、MLX90614發射率補償方法 3、控制器和傳感器連線圖 4、驅動程序 MLX90614紅外測溫模塊,通過探測物體紅外輻射能量的大小和波長的分布來檢測物體的表面溫度。紅外測溫器由光學系統、光電探測器、信號放大器

一文讀懂 Linux mmap

文章目錄 1.簡介2.實現原理3.相關函數4.mmap和常規文件操作的區別5.作用參考文獻 1.簡介 mmap&#xff08;memory map&#xff09;即內存映射&#xff0c;用于將一個文件或設備映射到進程的地址空間。 實現這樣的映射關系后&#xff0c;進程虛擬地址空間中一段內存地址將與文…

TorchScript C++ 自定義運算符 cpucuda

參考 在 C 中注冊調度運算符 使用自定義 C 運算符擴展 TorchScript 環境&#xff1a; NVIDIA Driver Version : 545.23.08CUDA Version: 12.1Python Version: 3.11Pytorch Version: 2.1Cmake version : 3.18.1工作目錄&#xff1a;workspace/test 一、 C 自定義運算符 創建…

逐字節講解 Redis 持久化(RDB 和 AOF)的文件格式

前言 相信各位對 Redis 的這兩種持久化機制都不陌生&#xff0c;簡單來說&#xff0c;RDB 就是對數據的全量備份&#xff0c;AOF 則是增量備份&#xff0c;而從 4.0 版本開始引入了混合方式&#xff0c;以 7.2.3 版本為例&#xff0c;會生成三類文件&#xff1a;RDB、AOF 和記…

2014年5月28日 Go生態洞察:GopherCon 2014大會回顧

&#x1f337;&#x1f341; 博主貓頭虎&#xff08;&#x1f405;&#x1f43e;&#xff09;帶您 Go to New World?&#x1f341; &#x1f984; 博客首頁——&#x1f405;&#x1f43e;貓頭虎的博客&#x1f390; &#x1f433; 《面試題大全專欄》 &#x1f995; 文章圖文…