Circuit Breaker implemented

circuit-breaker
atancito 2022-10-04 18:28:30 +02:00
parent 7609216b03
commit dfcd62457b
4 changed files with 73 additions and 2 deletions

View File

@ -7,4 +7,33 @@ eureka:
service-url:
default-zone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
resilience4j.circuitbreaker:
instances:
carsCB:
registerHealthIndicator: true
slidingWindowSize: 10
permittedNumberOfCallsInHalfOpenState: 3
slidingWindowType: TIME_BASED
minimumNumberOfCalls: 4
waitDurationInOpenState: 5s
failureRateThreshold: 50
eventConsumerBufferSize: 10
bikesCB:
registerHealthIndicator: true
slidingWindowSize: 10
permittedNumberOfCallsInHalfOpenState: 3
slidingWindowType: TIME_BASED
minimumNumberOfCalls: 4
waitDurationInOpenState: 5s
failureRateThreshold: 50
eventConsumerBufferSize: 10
allCB:
registerHealthIndicator: true
slidingWindowSize: 10
permittedNumberOfCallsInHalfOpenState: 3
slidingWindowType: TIME_BASED
minimumNumberOfCalls: 4
waitDurationInOpenState: 5s
failureRateThreshold: 50
eventConsumerBufferSize: 10

View File

@ -5,7 +5,7 @@ spring:
config:
server:
git:
defaultLabel: gateway
defaultLabel: circuit-breaker
uri: https://git.gzcode.xyz/atancito/MicroservicesTutorial
searchPaths: config-data
application:

View File

@ -57,6 +57,20 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.7.4</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -19,6 +20,8 @@ import com.example.userservice.model.Car;
import com.example.userservice.repository.UserRepository;
import com.example.userservice.service.UserService;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
@RestController
@RequestMapping("/users")
public class UserController {
@ -52,6 +55,7 @@ public class UserController {
return ResponseEntity.ok(userService.save(user));
}
@CircuitBreaker(name = "carsCB", fallbackMethod = "fallbackGetCars")
@GetMapping("/{userId}/cars")
public ResponseEntity<List<Car>> getCars(@PathVariable("userId") int userId) {
User user = userService.getUserById(userId);
@ -63,6 +67,11 @@ public class UserController {
}
}
private ResponseEntity<List<Car>> fallbackGetCars(@PathVariable("userId") int userId, Exception e) {
return new ResponseEntity("El usuairo " + userId + " tiene los coches en el taller.", HttpStatus.OK);
}
@CircuitBreaker(name = "bikesCB", fallbackMethod = "fallbackGetBikes")
@GetMapping("/{userId}/bikes")
public ResponseEntity<List<Bike>> getBikes(@PathVariable("userId") int userId) {
User user = userService.getUserById(userId);
@ -74,6 +83,11 @@ public class UserController {
}
}
private ResponseEntity<List<Bike>> fallbackGetBikes(@PathVariable("userId") int userId, Exception e) {
return new ResponseEntity("El usuairo " + userId + " tiene las motos en el taller.", HttpStatus.OK);
}
@CircuitBreaker(name = "carsCB", fallbackMethod = "fallbackSaveCar")
@PostMapping("/savecar/{userId}")
public ResponseEntity<Car> saveCar(@PathVariable("userId") int userId, @RequestBody Car car) {
User user = userService.getUserById(userId);
@ -86,6 +100,11 @@ public class UserController {
}
private ResponseEntity<Car> fallbackSaveCar(@PathVariable("userId") int userId, @RequestBody Car car, Exception e) {
return new ResponseEntity("El usuairo " + userId + " no tiene dinero para comprar un coche.", HttpStatus.OK);
}
@CircuitBreaker(name = "bikesCB", fallbackMethod = "fallbackSaveBike")
@PostMapping("/savebike/{userId}")
public ResponseEntity<Bike> saveBike(@PathVariable("userId") int userId, @RequestBody Bike bike) {
User user = userService.getUserById(userId);
@ -97,10 +116,19 @@ public class UserController {
}
}
private ResponseEntity<List<Bike>> fallbackSaveBike(@PathVariable("userId") int userId, @RequestBody Bike bike , Exception e) {
return new ResponseEntity("El usuairo " + userId + " no tiene dinero para comprar una moto.", HttpStatus.OK);
}
@CircuitBreaker(name = "allCB", fallbackMethod = "fallbackGetAll")
@GetMapping("/getAll/{userId}")
public ResponseEntity<Map<String, Object>> getAllVehicules(@PathVariable("userId") int userId) {
Map<String, Object> result = userService.getUserAndVehicules(userId);
return ResponseEntity.ok(result);
}
public ResponseEntity<Map<String, Object>> fallbackGetAll(@PathVariable("userId") int userId) {
return new ResponseEntity("El usuairo " + userId + " tiene los vehículos en el taller.", HttpStatus.OK);
}
}