Java 断路器设计模式实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30285637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Circuit breaker design pattern implementation
提问by user2585494
回答by Dawid Bugajewski
Regarding the pattern itself
关于模式本身
You can obtain a lot of useful information about this pattern at Martin Fowler's blog. It contains ruby implementation as well as references for implementation in other languages.
您可以在Martin Fowler 的博客上获得许多关于此模式的有用信息。它包含 ruby 实现以及其他语言的实现参考。
Regarding the java spring implementation
关于java spring的实现
Please check the JRugged library. It contains the Circuit Breaker implementation in spring as well as other design patterns.
请检查JRugged 库。它包含 spring 中的断路器实现以及其他设计模式。
回答by Guillaume
Spring cloudprovides some interesting integration with Hystrix. You should probably have a look into it...
Spring cloud提供了一些与Hystrix 的有趣集成。你可能应该看看它......
回答by Jonathan
For a simple, straightforward circuit breaker implementation, check out Failsafe. Ex:
CircuitBreaker breaker = new CircuitBreaker()
.withFailureThreshold(5)
.withSuccessThreshold(3)
.withDelay(1, TimeUnit.MINUTES);
Failsafe.with(breaker).run(() -> connect());
Doesn't get much simpler.
并没有变得更简单。
回答by Mark McLaren
You don't actually need to be using Spring cloud or Spring boot to use Hystrix.
Using hystrix-javanicamakes it easy to use Hystrix with plain old Spring too.
您实际上并不需要使用 Spring cloud 或 Spring boot 来使用 Hystrix。
使用hystrix-javanica也可以很容易地将 Hystrix 与普通的 Spring 一起使用。
Here is an example of fallback methods (both methods, getMessageTimeout and getMessageException, fail by default):
以下是回退方法的示例(两种方法,getMessageTimeout 和 getMessageException,默认情况下都失败):
@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class CircuitBreakingWithHystrix {
@Bean
public HystrixCommandAspect hystrixAspect() {
return new HystrixCommandAspect();
}
public static void main(String[] args) throws Throwable {
ApplicationContext ctx
= new AnnotationConfigApplicationContext(CircuitBreakingWithHystrix.class);
ExampleService ex = ctx.getBean(ExampleService.class);
for (int i = 0; i < 1000; i++) {
System.out.println(ex.getMessageException());
System.out.println(ex.getMessageTimeout());
}
}
@Service
class ExampleService {
/*
* The default Hystrix timeout is 1 second. So the default
* version of this method will always fail.
* Adding the @HystrixProperty will cause
* the method to succeed.
*/
@HystrixCommand(
commandProperties = {
//@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS,
// value = "5000")
},
fallbackMethod = "messageFallback"
)
public String getMessageTimeout() {
try {
//Pause for 4 seconds
Thread.sleep(4000);
} catch (InterruptedException ex) {
// Do something clever with this
}
return "result";
}
@HystrixCommand(
fallbackMethod = "messageFallback")
public String getMessageException() {
throw new RuntimeException("Bad things happened");
}
private String messageFallback(Throwable hre) {
return "fallback";
}
}
You can also examine the throwable sent to the fallback method to identify why the method call failed.
您还可以检查发送到回退方法的 throwable 以确定方法调用失败的原因。
回答by walkeros
You can have a look at JCircuitBreaker. The implementation there implements circuit breaker like approach.
你可以看看JCircuitBreaker。那里的实现实现了类似断路器的方法。
Please note that this is not 1:1 implementation of the pattern because it does not define fixed states like "half-open". Instead it makes the decision (if the breaker should be open or closed) basing on current application state (using so called "break strategy"). Nevertheless it should be possible to define such a "break strategy" which evaluates failures thresholds - so it should be possible to also implement original pattern using JCircuitBreaker.
请注意,这不是模式的 1:1 实现,因为它没有定义像“半开”这样的固定状态。相反,它根据当前的应用程序状态(使用所谓的“中断策略”)做出决定(断路器应该打开还是关闭)。尽管如此,应该可以定义这样一个评估失败阈值的“中断策略” - 因此也应该可以使用 JCircuitBreaker 实现原始模式。
回答by svarog
Apache commonshas some implementations for several types of lightweight circuit breakers, here's a link to the docs
Apache commons为几种类型的轻量级断路器提供了一些实现,这里是文档的链接
The project provides the EventCountCircuitBreaker
and ThresholdCircuitBreaker
classes, and an abstract AbstractCircuitBreaker
so you could implement your own.
该项目提供EventCountCircuitBreaker
和ThresholdCircuitBreaker
类,以及一个抽象,AbstractCircuitBreaker
以便您可以实现自己的。
The code is open sources and is hosted at github, so anyone attempting to implement the pattern should at least take a peek.
代码是开源的并托管在 github 上,因此任何尝试实现该模式的人都至少应该看一看。