use of io.vertx.circuitbreaker.CircuitBreakerOptions in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() {
CircuitBreakerOptions options = new CircuitBreakerOptions().setMaxFailures(5).setTimeout(5000).setFallbackOnFailure(true);
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx, options).openHandler(v -> {
System.out.println("Circuit opened");
}).closeHandler(v -> {
System.out.println("Circuit closed");
});
Future<String> result = breaker.executeWithFallback(future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
response.exceptionHandler(future::fail).bodyHandler(buffer -> {
future.complete(buffer.toString());
});
}
});
}, v -> {
// Executed when the circuit is opened
return "Hello (fallback)";
});
result.setHandler(ar -> {
// Do something with the result
System.out.println("Result: " + ar.result());
});
}
use of io.vertx.circuitbreaker.CircuitBreakerOptions in project vertx-openshift-it by cescoffier.
the class GreetingServiceVerticle method start.
@Override
public void start() throws Exception {
circuit = CircuitBreaker.create("circuit-breaker", vertx, new CircuitBreakerOptions().setFallbackOnFailure(true).setMaxFailures(3).setResetTimeout(5000).setNotificationAddress("circuit-breaker").setTimeout(1000));
client = WebClient.create(vertx, new WebClientOptions().setDefaultHost("name-service").setDefaultPort(8080));
Router router = Router.router(vertx);
router.get("/health").handler(rc -> rc.response().end("OK"));
router.get("/eventbus/*").handler(getSockJsHandler());
// The address is the circuit breaker notification address configured above.
router.get("/metrics").handler(HystrixMetricHandler.create(vertx, "circuit-breaker"));
router.get("/api/greeting").handler(this::greeting);
router.get("/api/cb-state").handler(rc -> rc.response().putHeader(CONTENT_TYPE.toString(), APPLICATION_JSON.toString()).end(new JsonObject().put("state", circuit.state()).encodePrettily()));
router.get("/*").handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
use of io.vertx.circuitbreaker.CircuitBreakerOptions in project gravitee-gateway by gravitee-io.
the class FailoverInvoker method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
Failover failover = api.getProxy().getFailover();
circuitBreaker = CircuitBreaker.create("cb-" + api.getId(), vertx, new CircuitBreakerOptions().setMaxRetries(// number of failure before opening the circuit
failover.getMaxAttempts()).setTimeout(// consider a failure if the operation does not succeed in time
failover.getRetryTimeout()).setResetTimeout(// time spent in open state before attempting to re-try
10000L).setNotificationAddress(null));
}
Aggregations