1

我基于@spencergibb feign-eureka spring cloud starter example 构建了一个超级简单的 Hystrix 短路示例。起初我以为我无法触发 hystrix javanica 默认 fallbackMethod 由于 feign.. 现在,删除 feign,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();
}
}
4

1 回答 1

7

@HystrixCommand仅适用于 Spring 制作了一个代理来调用该方法。如果您从代理中调用该方法,它不会通过拦截器。您需要@HystrixCommand从另一个调用@Component(或使用 AspectJ)。

于 2015-04-10T16:32:15.483 回答