-
Notifications
You must be signed in to change notification settings - Fork 189
/
BackendAService.java
177 lines (150 loc) · 5.8 KB
/
BackendAService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package io.github.robwin.service;
import io.github.resilience4j.bulkhead.BulkheadFullException;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.CallNotPermittedException;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import io.github.robwin.exception.BusinessException;
import io.vavr.control.Try;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeoutException;
import static io.github.resilience4j.bulkhead.annotation.Bulkhead.Type;
/**
* This Service shows how to use the CircuitBreaker annotation.
*/
@Component(value = "backendAService")
public class BackendAService implements Service {
private static final String BACKEND_A = "backendA";
@Override
@CircuitBreaker(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
@Retry(name = BACKEND_A)
public String failure() {
throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "This is a remote exception");
}
@Override
@CircuitBreaker(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
public String ignoreException() {
throw new BusinessException("This exception is ignored by the CircuitBreaker of backend A");
}
@Override
@CircuitBreaker(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
@Retry(name = BACKEND_A)
public String success() {
return "Hello World from backend A";
}
@Override
@CircuitBreaker(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
public String successException() {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "This is a remote client exception");
}
@Override
@CircuitBreaker(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
@Retry(name = BACKEND_A)
public Flux<String> fluxFailure() {
return Flux.error(new IOException("BAM!"));
}
@Override
@TimeLimiter(name = BACKEND_A)
@CircuitBreaker(name = BACKEND_A, fallbackMethod = "fluxFallback")
public Flux<String> fluxTimeout() {
return Flux.
just("Hello World from backend A")
.delayElements(Duration.ofSeconds(10));
}
@Override
@TimeLimiter(name = BACKEND_A)
@CircuitBreaker(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
@Retry(name = BACKEND_A)
public Mono<String> monoSuccess() {
return Mono.just("Hello World from backend A");
}
@Override
@CircuitBreaker(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
@Retry(name = BACKEND_A)
public Mono<String> monoFailure() {
return Mono.error(new IOException("BAM!"));
}
@Override
@TimeLimiter(name = BACKEND_A)
@Bulkhead(name = BACKEND_A)
@CircuitBreaker(name = BACKEND_A, fallbackMethod = "monoFallback")
public Mono<String> monoTimeout() {
return Mono.just("Hello World from backend A")
.delayElement(Duration.ofSeconds(10));
}
@Override
@TimeLimiter(name = BACKEND_A)
@CircuitBreaker(name = BACKEND_A)
@Retry(name = BACKEND_A)
public Flux<String> fluxSuccess() {
return Flux.just("Hello", "World");
}
@Override
@CircuitBreaker(name = BACKEND_A, fallbackMethod = "fallback")
public String failureWithFallback() {
return failure();
}
@Override
@Bulkhead(name = BACKEND_A, type = Type.THREADPOOL)
@TimeLimiter(name = BACKEND_A)
@CircuitBreaker(name = BACKEND_A)
@Retry(name = BACKEND_A)
public CompletableFuture<String> futureSuccess() {
return CompletableFuture.completedFuture("Hello World from backend A");
}
@Override
@Bulkhead(name = BACKEND_A, type = Type.THREADPOOL)
@TimeLimiter(name = BACKEND_A)
@CircuitBreaker(name = BACKEND_A)
@Retry(name = BACKEND_A)
public CompletableFuture<String> futureFailure() {
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new IOException("BAM!"));
return future;
}
@Override
@Bulkhead(name = BACKEND_A, type = Type.THREADPOOL)
@TimeLimiter(name = BACKEND_A)
@CircuitBreaker(name = BACKEND_A, fallbackMethod = "futureFallback")
public CompletableFuture<String> futureTimeout() {
Try.run(() -> Thread.sleep(5000));
return CompletableFuture.completedFuture("Hello World from backend A");
}
private String fallback(HttpServerErrorException ex) {
return "Recovered HttpServerErrorException: " + ex.getMessage();
}
private String fallback(Exception ex) {
return "Recovered: " + ex.toString();
}
private CompletableFuture<String> futureFallback(TimeoutException ex) {
return CompletableFuture.completedFuture("Recovered specific TimeoutException: " + ex.toString());
}
private CompletableFuture<String> futureFallback(BulkheadFullException ex) {
return CompletableFuture.completedFuture("Recovered specific BulkheadFullException: " + ex.toString());
}
private CompletableFuture<String> futureFallback(CallNotPermittedException ex) {
return CompletableFuture.completedFuture("Recovered specific CallNotPermittedException: " + ex.toString());
}
private Mono<String> monoFallback(Exception ex) {
return Mono.just("Recovered: " + ex.toString());
}
private Flux<String> fluxFallback(Exception ex) {
return Flux.just("Recovered: " + ex.toString());
}
}