hystrix-javanica - 微服务弹性框架


未知
跨平台
Java

软件简介

javanica 是 Hystrix 开源社区贡献的一个类库。

Java 语言相比其他语言有一些比较 great 的优点,那就是反射(refleaction)和注解(annotation)。在传统的使用 Hystrix
时,你需要编写大量的代码,这显然对开发者并不友好,也会制约 Hystrix 未来的发展。这种模式下,你需要花很长时间编写一些 Hystrix
commands。Javanica 项目的想法就是想通过引入 annotation 让你更容易地使用 Hystrix。

使用

要使用 hystrix-javanica,首先要在项目中加入 hystrix-javanica 的依赖。

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>x.y.z</version>
</dependency>

为了实现 AOP 的功能,如果项目中已经使用了 AspectJ,那么还需要在 aop.xml 中添加 hystrix 的切面:

<aspects>
        ...
        <aspect name="com.netflix.hystrix.contrib.javanica.aop.
aspectj.HystrixCommandAspect"/>
        ...
</aspects>

更多 AspectJ 的配置你可以点击这里

如果使用 Spring AOP,那么需要通过使用 Spring AOP 的 namespace 来添加指定的配置,这样让 Spring
能够去管理切面,那些你使用 AspectJ 切面,需要像下面这样声明HystrixCommandAspect 作为 Spring 的 bean:

<aop:aspectj-autoproxy/>
    <bean id="hystrixAspect" class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect"></bean>

如果使用Spring的代码配置方式的话,则像下面这样:

@Configuration
public class HystrixConfiguration {  
  @Bean
  public HystrixCommandAspect hystrixAspect() {    
       return new HystrixCommandAspect();
  }

}

无论使用哪种方式来创建 proxy,javanica 都可以和 JDK 以及 CGLIB proxy 配合得很好。