org.springframework.cloud.client.circuitbreaker.CircuitBreaker Java Examples

The following examples show how to use org.springframework.cloud.client.circuitbreaker.CircuitBreaker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringRetryCircuitBreakerTest.java    From spring-cloud-circuitbreaker with Apache License 2.0 6 votes vote down vote up
@Test
public void testFallback() {
	SpringRetryCircuitBreakerFactory factory = new SpringRetryCircuitBreakerFactory();
	Supplier<String> spyedSup = spy(new Supplier<String>() {
		@Override
		public String get() {
			throw new RuntimeException("boom");
		}
	});
	CircuitBreaker cb = factory.create("foo");
	for (int i = 0; i < 10; i++) {
		cb.run(spyedSup, t -> "fallback");
	}
	assertThat((String) cb.run(spyedSup, t -> "fallback")).isEqualTo("fallback");
	// This will only be called 3 times because the SimpleRetryPolicy will trip the
	// circuit after the
	// 3rd attempt.
	verify(spyedSup, times(3)).get();
}
 
Example #2
Source File: SentinelCircuitBreakerTest.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDirectlyThenRun() {
	// Create a circuit breaker without any circuit breaking rules.
	CircuitBreaker cb = new SentinelCircuitBreaker(
			"testSentinelCreateDirectlyThenRunA");
	assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel");
	assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunA"))
			.isFalse();

	CircuitBreaker cb2 = new SentinelCircuitBreaker(
			"testSentinelCreateDirectlyThenRunB",
			Collections.singletonList(
					new DegradeRule("testSentinelCreateDirectlyThenRunB")
							.setCount(100).setTimeWindow(10)));
	assertThat(cb2.run(() -> "Sentinel")).isEqualTo("Sentinel");
	assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunB"))
			.isTrue();
}
 
Example #3
Source File: SpringRetryCircuitBreakerFactory.java    From spring-cloud-circuitbreaker with Apache License 2.0 5 votes vote down vote up
@Override
public CircuitBreaker create(String id) {
	Assert.hasText(id, "A circuit breaker must have an id");
	SpringRetryConfigBuilder.SpringRetryConfig config = getConfigurations()
			.computeIfAbsent(id, defaultConfig);
	return new SpringRetryCircuitBreaker(id, config,
			Optional.ofNullable(retryTemplateCustomizers.get(id)));
}
 
Example #4
Source File: Resilience4JCircuitBreaker.java    From spring-cloud-circuitbreaker with Apache License 2.0 5 votes vote down vote up
public Resilience4JCircuitBreaker(String id,
		io.github.resilience4j.circuitbreaker.CircuitBreakerConfig circuitBreakerConfig,
		TimeLimiterConfig timeLimiterConfig,
		CircuitBreakerRegistry circuitBreakerRegistry,
		ExecutorService executorService,
		Optional<Customizer<io.github.resilience4j.circuitbreaker.CircuitBreaker>> circuitBreakerCustomizer) {
	this.id = id;
	this.circuitBreakerConfig = circuitBreakerConfig;
	this.registry = circuitBreakerRegistry;
	this.timeLimiterConfig = timeLimiterConfig;
	this.executorService = executorService;
	this.circuitBreakerCustomizer = circuitBreakerCustomizer;
}
 
Example #5
Source File: Resilience4JCircuitBreakerTest.java    From spring-cloud-circuitbreaker with Apache License 2.0 5 votes vote down vote up
@Test
public void runWithFallback() {
	CircuitBreaker cb = new Resilience4JCircuitBreakerFactory().create("foo");
	assertThat((String) cb.run(() -> {
		throw new RuntimeException("boom");
	}, t -> "fallback")).isEqualTo("fallback");
}
 
Example #6
Source File: SentinelCircuitBreakerFactory.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public CircuitBreaker create(String id) {
	Assert.hasText(id, "A CircuitBreaker must have an id.");
	SentinelConfigBuilder.SentinelCircuitBreakerConfiguration conf = getConfigurations()
			.computeIfAbsent(id, defaultConfiguration);
	return new SentinelCircuitBreaker(id, conf.getEntryType(), conf.getRules());
}
 
Example #7
Source File: SentinelCircuitBreakerTest.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithNullRule() {
	String id = "testCreateCbWithNullRule";
	CircuitBreaker cb = new SentinelCircuitBreaker(id,
			Collections.singletonList(null));
	assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel");
	assertThat(DegradeRuleManager.hasConfig(id)).isFalse();
}
 
Example #8
Source File: SentinelCircuitBreakerTest.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunWithFallback() {
	CircuitBreaker cb = new SentinelCircuitBreakerFactory()
			.create("testSentinelRunWithFallback");
	assertThat(cb.<String>run(() -> {
		throw new RuntimeException("boom");
	}, t -> "fallback")).isEqualTo("fallback");
}
 
Example #9
Source File: SpringRetryCircuitBreakerTest.java    From spring-cloud-circuitbreaker with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreate() {
	CircuitBreaker cb = new SpringRetryCircuitBreakerFactory().create("foo");
	assertThat(cb.run(() -> "foo")).isEqualTo("foo");
}
 
Example #10
Source File: Resilience4JCircuitBreaker.java    From spring-cloud-circuitbreaker with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
	TimeLimiter timeLimiter = TimeLimiter.of(timeLimiterConfig);
	Supplier<Future<T>> futureSupplier = () -> executorService.submit(toRun::get);
	Callable restrictedCall = TimeLimiter.decorateFutureSupplier(timeLimiter,
			futureSupplier);

	io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry
			.circuitBreaker(id, circuitBreakerConfig);
	circuitBreakerCustomizer
			.ifPresent(customizer -> customizer.customize(defaultCircuitBreaker));
	Callable<T> callable = io.github.resilience4j.circuitbreaker.CircuitBreaker
			.decorateCallable(defaultCircuitBreaker, restrictedCall);
	return Try.of(callable::call).recover(fallback).get();
}
 
Example #11
Source File: Resilience4JCircuitBreakerTest.java    From spring-cloud-circuitbreaker with Apache License 2.0 4 votes vote down vote up
@Test
public void run() {
	CircuitBreaker cb = new Resilience4JCircuitBreakerFactory().create("foo");
	assertThat(cb.run(() -> "foobar")).isEqualTo("foobar");
}
 
Example #12
Source File: SentinelCircuitBreakerTest.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateFromFactoryThenRun() {
	CircuitBreaker cb = new SentinelCircuitBreakerFactory().create("testSentinelRun");
	assertThat(cb.run(() -> "foobar")).isEqualTo("foobar");
}
 
Example #13
Source File: SleuthCircuitBreakerAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Around("anyCircuitBreakerFactoryCreate()")
public Object wrapFactory(ProceedingJoinPoint pjp) throws Throwable {
	CircuitBreaker circuitBreaker = (CircuitBreaker) pjp.proceed();
	return new TraceCircuitBreaker(circuitBreaker, this.tracer);
}
 
Example #14
Source File: SleuthCircuitBreakerAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
TraceCircuitBreaker(CircuitBreaker delegate, Tracer tracer) {
	this.delegate = delegate;
	this.tracer = tracer;
}
 
Example #15
Source File: AlbumService.java    From tutorials with MIT License 4 votes vote down vote up
public String getAlbumList() {
    CircuitBreaker circuitBreaker = circuitBreakerFactory.create("circuitbreaker");
    String url = "https://jsonplaceholder.typicode.com/albums";

    return circuitBreaker.run(() -> restTemplate.getForObject(url, String.class), throwable -> getDefaultAlbumList());
}