Search in sources :

Example 1 with CircuitBreakerOptions

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());
    });
}
Also used : CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions) Launcher(io.vertx.core.Launcher) AbstractVerticle(io.vertx.core.AbstractVerticle) CircuitBreaker(io.vertx.circuitbreaker.CircuitBreaker) Future(io.vertx.core.Future) CircuitBreaker(io.vertx.circuitbreaker.CircuitBreaker) CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions)

Example 2 with CircuitBreakerOptions

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);
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) Router(io.vertx.rxjava.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions)

Example 3 with CircuitBreakerOptions

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));
}
Also used : Failover(io.gravitee.definition.model.Failover) CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions)

Aggregations

CircuitBreakerOptions (io.vertx.circuitbreaker.CircuitBreakerOptions)3 Failover (io.gravitee.definition.model.Failover)1 CircuitBreaker (io.vertx.circuitbreaker.CircuitBreaker)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Future (io.vertx.core.Future)1 Launcher (io.vertx.core.Launcher)1 JsonObject (io.vertx.core.json.JsonObject)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 Router (io.vertx.rxjava.ext.web.Router)1