question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circuit breaker with Annotation and with Spring Cloud gateway does not work the same way

See original GitHub issue

Circuit Breaker when used with annotation it works perfectly. After minimum no of call, circuit breaker opens

circuitbreaker:
   instances:
     mainService:
        minimum-number-of-calls: 5
        permitted-number-of-calls-in-half-open-state: 2
        wait-duration-in-open-state: 10s
        failure-rate-threshold: 50
        event-consumer-buffer-size: 10
        automatic-transition-from-open-to-half-open-enabled: true
        register-health-indicator: true
        sliding-window-size: 10
        sliding-window-type: COUNT_BASED
@GetMapping(value = "/checkrequest")
	@CircuitBreaker(name = "mainService", fallbackMethod = "fallback")
	@RateLimiter(name = "mainService1", fallbackMethod = "fallback")
	public Mono<String> checkrequest() {

	
	Mono<String> mono2 = ms2WebClient.get().uri("/checkrequestcall").retrieve().bodyToMono(String.class);
	return mono2;
}

ms2WebClient will always throws exception, and after 5th call Circuit beaker will open, as per the configuration

But when the same configuration is used with Spring Cloud gateway, circuit breaker does not opens

resilience4j.circuitbreaker:
  configs:
    default:
      register-health-indicator: true
      sliding-window-size: 10
      sliding-window-type: COUNT_BASED
      minimum-number-of-calls: 5 
      event-consumer-buffer-size: 5
      ring-buffer-size-in-closed-state: 5
      ring-buffer-size-in-half-open-state: 5
      writable-stack-trace-enabled: true     
      permitted-number-of-calls-in-half-open-state: 2
      automatic-transition-from-open-to-half-open-enabled: true
      wait-duration-in-open-state: 10s
      failure-rate-threshold: 50
  instances:
    backendA:
      baseConfig: default

Here Route /ms2/get-data will throw Exception, even after 100 calls circuit breaker does not opens, wants to know why the Circuit Breaker behavior is different with Annotation and with Spring Cloud Gateway

- id: ms2
          uri: http://localhost:9092/
          predicates:
            - Path=/ms2/**
          filters:
            - RewritePath=/ms2/(?<path>.*), /$\{path}
            - name: RequestRateLimiter
              args: 
               redis-rate-limiter.replenishRate: 2000
               redis-rate-limiter.burstCapacity: 4000
               key-resolver: "#{@userRemoteAddressResolver}"
            - name: CircuitBreaker
              args:
                name: backendA
                fallbackUri: forward:/fallback/ms2
@GetMapping(value = "/get-data")
public ResponseEntity<Mono<String>> getData(ServerHttpRequest request, ServerHttpResponse response) throws Exception {
		
	HttpHeaders headers = request.getHeaders();
	
	int nxt = ran.nextInt(10); 
	if(true) {
		 byte[] body =null;
		 Charset charset = getCharset(response);
		 throw HttpServerErrorException.create(HttpStatus.INTERNAL_SERVER_ERROR, "Error", headers, body, charset);
	}
	return null;
}

pom.xml for Spring Cloud Gateway

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.5.BUILD-SNAPSHOT</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.portal</groupId>
	<artifactId>gateway</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>gateway</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.BUILD-SNAPSHOT</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-gateway</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>io.github.resilience4j</groupId>
			<artifactId>resilience4j-spring-boot2</artifactId>
			<version>1.5.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>io.projectreactor</groupId>
			<artifactId>reactor-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>
</project>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RobWincommented, May 25, 2021

spring-cloud-resilience4j uses some Spring Cloud specific annotations so that you can use Spring Cloud Config to change the configuration at runtime.

0reactions
amirensitcommented, May 24, 2021

Hi.

Sorry for commenting on closed issue, but I need to understand what is the difference between spring-boot-resilience4j and spring-cloud-resilience4j ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Cloud Gateway with Resilience4J Circuit Breaker did ...
While attempting to integrate Resilience4J with Spring Cloud Gateway, some features such as retry appear to be working properly, ...
Read more >
Quick Guide to Spring Cloud Circuit Breaker - Baeldung
In this tutorial, we'll introduce the Spring Cloud Circuit Breaker project and learn how we can make use of it.
Read more >
Getting Started | Spring Cloud Circuit Breaker Guide
Like most Spring Getting Started guides, you can start from scratch and complete each step or you can bypass basic setup steps that...
Read more >
Circuit Breaking In Spring Cloud Gateway With Resilience4J
Of course, you can still use Hystrix as circuit breaker implementation, however it is deprecated and probably won't be available in the future ......
Read more >
Circuit Breaker And Retry with Spring Cloud Resiliance4j
Any problems while communicating with the upstream services, will ... as the retry and circuit breaker mechanism works using the Spring AOP ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found