BAEL-3649 Quick Guide to Spring Cloud Circuit Breaker (#8819)
This commit is contained in:
parent
f9ac384b85
commit
88cc732dc7
|
@ -0,0 +1,5 @@
|
|||
## Spring Cloud Circuit Breaker
|
||||
|
||||
This module contains articles about Spring Cloud Circuit Breaker
|
||||
|
||||
### Relevant Articles:
|
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-cloud-circuit-breaker</artifactId>
|
||||
<name>spring-cloud-gateway</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/libs-snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>2.2.4.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
|
||||
<version>1.0.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.circuitbreaker;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
|
||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Service
|
||||
public class AlbumService {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AlbumService.class);
|
||||
|
||||
@Autowired
|
||||
private CircuitBreakerFactory circuitBreakerFactory;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
private String getDefaultAlbumList() {
|
||||
try {
|
||||
return new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("fallback-album-list.json").toURI())));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("error occurred while reading the file", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.circuitbreaker;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class Controller {
|
||||
|
||||
@Autowired
|
||||
private AlbumService service;
|
||||
|
||||
@GetMapping("/albums")
|
||||
public String albums() {
|
||||
return service.getAlbumList();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.circuitbreaker;
|
||||
|
||||
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
|
||||
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory;
|
||||
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
|
||||
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringApp.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Customizer<Resilience4JCircuitBreakerFactory> globalCustomConfiguration() {
|
||||
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
|
||||
.timeoutDuration(Duration.ofSeconds(4))
|
||||
.build();
|
||||
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
|
||||
.failureRateThreshold(50)
|
||||
.waitDurationInOpenState(Duration.ofMillis(1000))
|
||||
.slidingWindowSize(2)
|
||||
.build();
|
||||
|
||||
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
|
||||
.timeLimiterConfig(timeLimiterConfig)
|
||||
.circuitBreakerConfig(circuitBreakerConfig)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Customizer<Resilience4JCircuitBreakerFactory> specificCustomConfiguration1() {
|
||||
|
||||
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
|
||||
.timeoutDuration(Duration.ofSeconds(4))
|
||||
.build();
|
||||
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
|
||||
.failureRateThreshold(50)
|
||||
.waitDurationInOpenState(Duration.ofMillis(1000))
|
||||
.slidingWindowSize(2)
|
||||
.build();
|
||||
|
||||
return factory -> factory.configure(builder -> builder.circuitBreakerConfig(circuitBreakerConfig)
|
||||
.timeLimiterConfig(timeLimiterConfig).build(), "circuitBreaker");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Customizer<Resilience4JCircuitBreakerFactory> specificCustomConfiguration2() {
|
||||
|
||||
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
|
||||
.timeoutDuration(Duration.ofSeconds(4))
|
||||
.build();
|
||||
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
|
||||
.failureRateThreshold(50)
|
||||
.waitDurationInOpenState(Duration.ofMillis(1000))
|
||||
.slidingWindowSize(2)
|
||||
.build();
|
||||
|
||||
return factory -> factory.configure(builder -> builder.circuitBreakerConfig(circuitBreakerConfig)
|
||||
.timeLimiterConfig(timeLimiterConfig).build(),
|
||||
"circuitBreaker1", "circuitBreaker2", "circuitBreaker3");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
[
|
||||
{
|
||||
"userId": 1,
|
||||
"id": 1,
|
||||
"title": "quidem molestiae enim"
|
||||
},
|
||||
{
|
||||
"userId": 1,
|
||||
"id": 2,
|
||||
"title": "sunt qui excepturi placeat culpa"
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue