Hystrix Javanica后备不在Spring Cloud 1.0中工作

时间:2015-04-08 19:25:24

标签: spring-boot spring-cloud hystrix

我基于@spencergibb feign-eureka spring cloud starter示例构建了一个超简单的Hystrix短路示例。起初我以为我无法得到hystrix javanica默认的fallbackMethod因为假装触发..现在,删除假,hystrix默认fallbackMethod仍然没有捕获异常。

的pom.xml

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>1.0.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    :
</dependencies>

主档案:

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@RestController
public class HelloClientApplication {

  @Autowired
  HelloClientComponent helloClientComponent;

  @RequestMapping("/")
  public String hello() {
    return helloClientComponent.makeMultipleCalls();
  }

  public static void main(String[] args) {
    SpringApplication.run(HelloClientApplication.class, args);
  }

}

HelloClientComponent.java(因为我知道javanica希望在spring托管组件或服务中创建):

@Component
public class HelloClientComponent {

@Autowired
RestTemplate restTemplate;

public String makeMultipleCalls() {
    int cnt=20;
    StringBuilder sb = new StringBuilder();
    while (cnt-- > 0) {
        String response = theServerRequestRoot();
        sb.append(response).append("  ");
    }
    return sb.toString();
}

public String theServersRequestRootFallback() {
    System.out.println("BOMB!!!!!!");
    return "BOMB!!!!!!";
}

@HystrixCommand(fallbackMethod = "theServersRequestRootFallback", commandKey = "callToServers")
public String theServerRequestRoot() {
        ResponseEntity<String> result = restTemplate.getForEntity("http://HelloServer", String.class);
        System.out.println(result.getBody());
        return result.getBody();
}
}

我启动了2台服务器,一台总是成功并响应,另一台将在30%的时间内失败并出现500错误。当我卷曲这个客户端(到'/')时,非强制失败调用正常。循环赛也很好。当第二个服务器确实返回500错误时,不会调用fallbackMethod并且'/'的curl结束并返回错误。

根据Spencer和Dave的建议更新解决方案。更改为以下内容:

主要申请文件:

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@RestController
public class HelloClientApplication {

  @Autowired
  HelloClientComponent helloClientComponent;

@RequestMapping("/")
public String hello() {
    int cnt=20;
    StringBuilder sb = new StringBuilder();
    while (cnt-- > 0) {
        String response = helloClientComponent.theServerRequestRoot();     // call directly to @Component in order for @HystrixCommand to intercept via AOP
        sb.append(response).append("  ");
    }
    return sb.toString();
}

  public static void main(String[] args) {
    SpringApplication.run(HelloClientApplication.class, args);
  }
  }

HelloClientComponent.java:

@Component
public class HelloClientComponent {

@Autowired
RestTemplate restTemplate;

public String theServersRequestRootFallback() {
    System.out.println("BOMB!!!!!!");
    return "BOMB!!!!!!";
}

@HystrixCommand(fallbackMethod = "theServersRequestRootFallback", commandKey = "callToServers")
public String theServerRequestRoot() {
        ResponseEntity<String> result = restTemplate.getForEntity("http://HelloServer", String.class);
        System.out.println(result.getBody());
        return result.getBody();
}
}

1 个答案:

答案 0 :(得分:6)

@HystrixCommand只能起作用,因为Spring会调用该方法调用该方法。如果从代理中调用该方法,它不会通过拦截器。您需要从另一个@HystrixCommand调用@Component(或使用AspectJ)。