Hystrix 和 Spring @Async 组合
Hystrix and Spring @Async in combination
我正在为 Spring 引导项目 (spring-cloud-starter-hystrix
) 使用 Hystrix 库。我有一个 @Service
class 注释 @HystrixCommand
并且它按预期工作。
但是,当我在同一服务 class 中添加用 @Async
注释的方法时,Hystrix 不起作用,并且永远不会调用回退方法。什么可能导致此问题以及如何解决?
这是Application
class:
@EnableCircuitBreaker
@EnableHystrixDashboard
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是服务 class:
@Service
public class TemplateService {
@HystrixCommand(
fallbackMethod = "getGreetingFallback",
commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")}
)
public String getGreeting() {
URI uri = URI.create("http://localhost:8090/greeting");
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
if (response.getStatusCode().equals(HttpStatus.OK)) {
return response.getBody();
} else {
return null;
}
}
public String getGreetingFallback(Throwable e) {
return null;
}
@Async
public void async(String message) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.info(MessageFormat.format("Received async message {0}", message));
}
}
@EnableAsync
注释被放置在一个不同的 class 中,用 @Configuration
注释,我在属性文件中设置了一些其他线程执行器选项。
鉴于 TemplateService
的代码( 未实现接口 )并假设 @EnableAsync
上的默认值,可以安全地同意 CGLIB 代理由 spring.
创建
因此 getGreeting()
上的 @HystrixCommand
注释未被 服务代理 class 继承;这解释了报告的行为。
要克服此错误,请将 @HystrixCommand
和 @Async
方法分开放在不同的服务中,因为启用 JDK 代理也无济于事,而且我不确定 AspectJ 模式。
有关 Spring 代理机制的更多信息,请参阅 this。
我正在为 Spring 引导项目 (spring-cloud-starter-hystrix
) 使用 Hystrix 库。我有一个 @Service
class 注释 @HystrixCommand
并且它按预期工作。
但是,当我在同一服务 class 中添加用 @Async
注释的方法时,Hystrix 不起作用,并且永远不会调用回退方法。什么可能导致此问题以及如何解决?
这是Application
class:
@EnableCircuitBreaker
@EnableHystrixDashboard
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是服务 class:
@Service
public class TemplateService {
@HystrixCommand(
fallbackMethod = "getGreetingFallback",
commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")}
)
public String getGreeting() {
URI uri = URI.create("http://localhost:8090/greeting");
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
if (response.getStatusCode().equals(HttpStatus.OK)) {
return response.getBody();
} else {
return null;
}
}
public String getGreetingFallback(Throwable e) {
return null;
}
@Async
public void async(String message) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.info(MessageFormat.format("Received async message {0}", message));
}
}
@EnableAsync
注释被放置在一个不同的 class 中,用 @Configuration
注释,我在属性文件中设置了一些其他线程执行器选项。
鉴于 TemplateService
的代码( 未实现接口 )并假设 @EnableAsync
上的默认值,可以安全地同意 CGLIB 代理由 spring.
因此 getGreeting()
上的 @HystrixCommand
注释未被 服务代理 class 继承;这解释了报告的行为。
要克服此错误,请将 @HystrixCommand
和 @Async
方法分开放在不同的服务中,因为启用 JDK 代理也无济于事,而且我不确定 AspectJ 模式。
有关 Spring 代理机制的更多信息,请参阅 this。