Spring boot Hystrix Javanica回退在Spring Cloud 1.0中不起作用
我基于@spencergib-feign-eureka-spring-cloud启动器示例构建了一个超级简单的Hystrix短路示例。起初我认为我无法触发hystrix javanica默认的Fallback方法,因为它是假的。。现在,除去faign,hystrix默认的fallback方法仍然不能捕获异常 pom.xmlSpring boot Hystrix Javanica回退在Spring Cloud 1.0中不起作用,spring-boot,spring-cloud,hystrix,Spring Boot,Spring Cloud,Hystrix,我基于@spencergib-feign-eureka-spring-cloud启动器示例构建了一个超级简单的Hystrix短路示例。起初我认为我无法触发hystrix javanica默认的Fallback方法,因为它是假的。。现在,除去faign,hystrix默认的fallback方法仍然不能捕获异常 pom.xml <parent> <groupId>org.springframework.cloud</groupId> <art
<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>
}
HelloClientComponent.java(之所以创建,是因为我知道javanica希望位于spring管理的组件或服务中):
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();
}
}
@组件
公共类HelloClient组件{
@自动连线
rest模板rest模板;
服务器请求回退()的公共字符串{
System.out.println(“炸弹!!!!!!!”;
返回“炸弹!!!!!!!”;
}
@HystrixCommand(fallbackMethod=“TheServersRequestRotFallback”,commandKey=“callToServers”)
ServerRequestRoot()的公共字符串{
ResponseEntity结果=restTemplate.getForEntity(“http://HelloServer“,String.class);
System.out.println(result.getBody());
返回result.getBody();
}
}
@HystrixCommand
只起作用,因为Spring生成了一个代理来调用该方法。如果从代理中调用该方法,它不会通过拦截器。您需要从另一个@组件(或使用AspectJ)调用@HystrixCommand
。@HystrixCommand
只起作用,因为Spring生成了一个代理来调用该方法。如果从代理中调用该方法,它不会通过拦截器。您需要从另一个@组件(或使用AspectJ)调用@HystrixCommand
。是的,将makeMultipleCalls
移动到HelloClientApplication。您好
正是我的问题。现在单击了为什么需要在组件或服务中使用HystrixCommand注释的方法。跟我说了很多。同样糟糕的是,Javanica文档没有进一步澄清这一点,尽管它现在提到了配置方面的选项。我将为未来的读者发布代码更新。是的,将makeMultipleCalls
移动到HelloClientApplication。您好
正是我的问题。现在单击了为什么需要在组件或服务中使用HystrixCommand注释的方法。跟我说了很多。同样糟糕的是,Javanica文档没有进一步澄清这一点,尽管它现在提到了配置方面的选项。我将为未来的读者发布代码的更新。
@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();
}
}
@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);
}
}
@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();
}
}