Circuit Breaker implemented
parent
7609216b03
commit
dfcd62457b
|
@ -7,4 +7,33 @@ eureka:
|
||||||
service-url:
|
service-url:
|
||||||
default-zone: http://localhost:8761/eureka
|
default-zone: http://localhost:8761/eureka
|
||||||
instance:
|
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
|
|
@ -5,7 +5,7 @@ spring:
|
||||||
config:
|
config:
|
||||||
server:
|
server:
|
||||||
git:
|
git:
|
||||||
defaultLabel: gateway
|
defaultLabel: circuit-breaker
|
||||||
uri: https://git.gzcode.xyz/atancito/MicroservicesTutorial
|
uri: https://git.gzcode.xyz/atancito/MicroservicesTutorial
|
||||||
searchPaths: config-data
|
searchPaths: config-data
|
||||||
application:
|
application:
|
||||||
|
|
|
@ -57,6 +57,20 @@
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
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.repository.UserRepository;
|
||||||
import com.example.userservice.service.UserService;
|
import com.example.userservice.service.UserService;
|
||||||
|
|
||||||
|
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/users")
|
@RequestMapping("/users")
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
@ -52,6 +55,7 @@ public class UserController {
|
||||||
return ResponseEntity.ok(userService.save(user));
|
return ResponseEntity.ok(userService.save(user));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CircuitBreaker(name = "carsCB", fallbackMethod = "fallbackGetCars")
|
||||||
@GetMapping("/{userId}/cars")
|
@GetMapping("/{userId}/cars")
|
||||||
public ResponseEntity<List<Car>> getCars(@PathVariable("userId") int userId) {
|
public ResponseEntity<List<Car>> getCars(@PathVariable("userId") int userId) {
|
||||||
User user = userService.getUserById(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")
|
@GetMapping("/{userId}/bikes")
|
||||||
public ResponseEntity<List<Bike>> getBikes(@PathVariable("userId") int userId) {
|
public ResponseEntity<List<Bike>> getBikes(@PathVariable("userId") int userId) {
|
||||||
User user = userService.getUserById(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}")
|
@PostMapping("/savecar/{userId}")
|
||||||
public ResponseEntity<Car> saveCar(@PathVariable("userId") int userId, @RequestBody Car car) {
|
public ResponseEntity<Car> saveCar(@PathVariable("userId") int userId, @RequestBody Car car) {
|
||||||
User user = userService.getUserById(userId);
|
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}")
|
@PostMapping("/savebike/{userId}")
|
||||||
public ResponseEntity<Bike> saveBike(@PathVariable("userId") int userId, @RequestBody Bike bike) {
|
public ResponseEntity<Bike> saveBike(@PathVariable("userId") int userId, @RequestBody Bike bike) {
|
||||||
User user = userService.getUserById(userId);
|
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}")
|
@GetMapping("/getAll/{userId}")
|
||||||
public ResponseEntity<Map<String, Object>> getAllVehicules(@PathVariable("userId") int userId) {
|
public ResponseEntity<Map<String, Object>> getAllVehicules(@PathVariable("userId") int userId) {
|
||||||
Map<String, Object> result = userService.getUserAndVehicules(userId);
|
Map<String, Object> result = userService.getUserAndVehicules(userId);
|
||||||
return ResponseEntity.ok(result);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue