亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring?cloud?Hystrix注解初始化源碼過程解讀

 更新時間:2023年12月21日 14:34:53   作者:福  
這篇文章主要為大家介紹了Hystrix初始化部分,我們從源碼的角度分析一下@EnableCircuitBreaker以及@HystrixCommand注解的初始化過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助

從@EnableCircuitBreaker入手

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

該注解已經(jīng)被標了@Deprecated了,不過,擋不住......

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

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

}

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

@Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableCircuitBreakerImportSelector extends SpringFactoryImportSelector&lt;EnableCircuitBreaker&gt; {

    @Override
    protected boolean isEnabled() {
        return getEnvironment().getProperty("spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE);
    }

}

看一下類圖:

屬性annotationClass通過GenericTypeResolver.resolveTypeArgument方法獲取當前對象的泛型參數(shù)的具體類型,現(xiàn)在我們知道是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注解我們前面做過詳細分析了,實現(xiàn)了DeferredImportSelector接口表示延遲加載全限定名稱為selectImports方法返回的類??磗electImports方法:

@Override
    public String[] selectImports(AnnotationMetadata metadata) {
        //Enable參數(shù)沒有打開的話就不加載
        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文件下的EnableCircuitBreaker
        List<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 factory
            this.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.netflix.hystrix.HystrixCircuitBreakerConfiguration會被調用并加載到Spring IoC容器中。

HystrixCircuitBreakerConfiguration

跟蹤配置類HystrixCircuitBreakerConfiguration:

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

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

繼續(xù)跟蹤HystrixCommandAspect 類。

HystrixCommandAspect

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

@Aspect
public class HystrixCommandAspect {

表明當前類是AOP的切面。

繼續(xù)看代碼:

@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的切點,以及針對該切點的環(huán)繞增強方法methodsAnnotatedWithHystrixCommand。

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

以上就是Spring cloud Hystrix注解初始化源碼過程解讀的詳細內容,更多關于Spring cloud Hystrix注解初始化的資料請關注腳本之家其它相關文章!

相關文章

  • 如何把spring boot應用發(fā)布到Harbor

    如何把spring boot應用發(fā)布到Harbor

    這篇文章主要介紹了如何把spring boot應用發(fā)布到Harbor,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • 對Java中傳值調用的理解分析

    對Java中傳值調用的理解分析

    這篇文章主要介紹了對Java中傳值調用的理解分析,通過分析對比,較為深入的分析了Java中傳值調用的原理與用法,需要的朋友可以參考下
    2015-01-01
  • Java中關于String的兩種賦值方式

    Java中關于String的兩種賦值方式

    這篇文章主要介紹了Java中關于String的兩種賦值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java實現(xiàn)局域網(wǎng)聊天室功能(私聊、群聊)

    Java實現(xiàn)局域網(wǎng)聊天室功能(私聊、群聊)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)局域網(wǎng)聊天室功能,包括私聊、群聊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Springboot整合https的實例代碼

    Springboot整合https的實例代碼

    本文簡單介紹了一些密碼學的基礎和如何通過Springboot整合HTTPS,本文將通過實例代碼給大家詳細介紹整合過程,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • SWT(JFace)體驗之StackLayout布局

    SWT(JFace)體驗之StackLayout布局

    SWT(JFace)體驗之StackLayout布局實現(xiàn)代碼。
    2009-06-06
  • java8使用filter()取出自己所需數(shù)據(jù)

    java8使用filter()取出自己所需數(shù)據(jù)

    這篇文章主要介紹了java8使用filter()取出自己所需數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java實現(xiàn)布隆過濾器的方法步驟

    Java實現(xiàn)布隆過濾器的方法步驟

    布隆過濾器是可以用于判斷一個元素是不是在一個集合里,并且相比于其它的數(shù)據(jù)結構,布隆過濾器在空間和時間方面都有巨大的優(yōu)勢。下面這篇文章主要給大家介紹了關于Java實現(xiàn)布隆過濾器的相關資料,需要的朋友可以參考下
    2018-11-11
  • Springboot Caffeine本地緩存使用示例

    Springboot Caffeine本地緩存使用示例

    這篇文章主要介紹了Springboot Caffeine本地緩存使用示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • IntelliJ IDEA優(yōu)化配置的實現(xiàn)

    IntelliJ IDEA優(yōu)化配置的實現(xiàn)

    這篇文章主要介紹了IntelliJ IDEA優(yōu)化配置的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07

最新評論