728x90
반응형
Gradle
implementation "org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j"
implementation 'org.springframework.boot:spring-boot-starter-actuator'
CircuitBreaker Configuration 빈 등록
@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build()).build());
}
default configuration등록시 위 코드를 이용하면 된다.
동적으로 circuit breaker가 생성 될 경우 추가 등록할 수 있다.
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.consumer.CircularEventConsumer;
import io.github.resilience4j.timelimiter.TimeLimiter;
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import io.github.resilience4j.timelimiter.TimeLimiterRegistry;
import io.vavr.collection.Seq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4JCircuitBreakerFactory;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
import org.springframework.cloud.client.circuitbreaker.Customizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.Duration;
@Slf4j
@Configuration
public class ScgCircuitBreakerConfig {
@Bean
public ReactiveResilience4JCircuitBreakerFactory defaultCustomizer(CircuitBreakerRegistry registry,
TimeLimiterRegistry timeLimiterRegistry) {
ReactiveResilience4JCircuitBreakerFactory factory = new ReactiveResilience4JCircuitBreakerFactory(registry, timeLimiterRegistry);
Seq<CircuitBreaker> circuitBreakerSeq = registry.getAllCircuitBreakers();
for(CircuitBreaker circuit : circuitBreakerSeq) {
log.error("circuitName : " + circuit.getName());
}
Seq<TimeLimiter> allTimeLimiters = timeLimiterRegistry.getAllTimeLimiters();
for(TimeLimiter timeLimiter: allTimeLimiters) {
String timeLimiterName = timeLimiter.getName();
log.error("timeLimiterName : " + timeLimiterName);
factory.configure(resilience4JConfigBuilder -> resilience4JConfigBuilder.timeLimiterConfig(timeLimiterRegistry.timeLimiter(timeLimiterName).getTimeLimiterConfig())
.circuitBreakerConfig(registry.circuitBreaker(timeLimiterName).getCircuitBreakerConfig()), timeLimiterName);
}
return factory;
}
}
ReactiveResilience4JCiruitBreakerFactory를 직접 생성하면 된다.
동적으로 등록시 @RefreshScope을 사용하면 가능하다.
application.yml
resilience4j.circuitbreaker:
configs:
default:
failureRateThreshold: 50
minimumNumberOfCalls: 10
registerHealthIndicator: true
instances:
prod-svc:
registerHealthIndicator: true
slidingWindowSize: 100
permittedNumberOfCallsInHalfOpenState: 3
resilience4j.timelimiter:
configs:
default:
timeoutDuration: 3s
instances:
prod-svc:
timeDuration: 3s
cancelRunningFuture: true
management:
health:
circuitbreakers:
enabled: true
endpoint:
health:
showDetails: always
web:
exposure:
include: "*"
expose: "*"
metrics:
enabled: true
metrics:
enable:
resilience4j:
circuitbreaker:
calls: true
circuitbreaker 설정을 actuator 의 health로 확인하고 싶으면 설정이 필요하다.
management.health.circuitbreakers.enabled: true
resilience4j.circuitbreaker:
configs:
default:
registerHealthIndicator: true
기본적으로 resilience4j.cirbuitbreaker의 registerHealthIndicator가 false이기 때문에 true로 변경한다.
이렇게 해서 /actuator/health를 하면 STATUS만 나온다.
management:
endpoint:
health:
showDetails: always
showDetails를 설정해줘야 상세 내용을 확인할 수 있다.
더보기
{
-
status: "UP",
- components: {
- circuitBreakers: {
-
details: {
-
prod-svc: {
-
status: "UP",
-
details: {
-
failureRate: "-1.0%",
-
failureRateThreshold: "50.0%",
-
slowCallRate: "-1.0%",
-
slowCallRateThreshold: "100.0%",
-
bufferedCalls: 0,
-
slowCalls: 0,
-
slowFailedCalls: 0,
-
failedCalls: 0,
-
notPermittedCalls: 0,
-
state: "CLOSED"
-
-
-
.... -
- circuitBreakers: {
}
위와 같이 확인할 수 있다.
728x90
반응형
'Spring' 카테고리의 다른 글
Spring cloud circuit breaker fallbackUri 사용 (0) | 2022.02.04 |
---|---|
Spring cloud circuit breaker fallback 메소드 테스트 (0) | 2022.02.04 |
Spring Cloud Gateway Global Error Handler (0) | 2022.02.03 |
Spring @Valid 어노테이션으로 파라미터 검증하기 (0) | 2022.02.02 |
Spring boot + mariadb + mybatis + hikaricp 설정 (0) | 2022.02.02 |