|
| 1 | +package io.vertx.book.openshift; |
| 2 | + |
| 3 | +import io.vertx.circuitbreaker.CircuitBreakerOptions; |
| 4 | +import io.vertx.core.json.JsonObject; |
| 5 | +import io.vertx.rxjava.ext.healthchecks.HealthCheckHandler; |
| 6 | +import io.vertx.ext.healthchecks.Status; |
| 7 | +import io.vertx.rxjava.circuitbreaker.CircuitBreaker; |
| 8 | +import io.vertx.rxjava.core.AbstractVerticle; |
| 9 | +import io.vertx.rxjava.ext.web.Router; |
| 10 | +import io.vertx.rxjava.ext.web.RoutingContext; |
| 11 | +import io.vertx.rxjava.ext.web.client.HttpRequest; |
| 12 | +import io.vertx.rxjava.ext.web.client.HttpResponse; |
| 13 | +import io.vertx.rxjava.ext.web.client.WebClient; |
| 14 | +import io.vertx.rxjava.ext.web.codec.BodyCodec; |
| 15 | +import io.vertx.rxjava.servicediscovery.ServiceDiscovery; |
| 16 | +import io.vertx.rxjava.servicediscovery.types.HttpEndpoint; |
| 17 | +import rx.Single; |
| 18 | + |
| 19 | +public class HelloConsumerWithCircuitBreakerHttpVerticle extends AbstractVerticle { |
| 20 | + |
| 21 | + private WebClient hello; |
| 22 | + private CircuitBreaker circuit; |
| 23 | + |
| 24 | + private boolean ready; |
| 25 | + |
| 26 | + @Override |
| 27 | + public void start() { |
| 28 | + |
| 29 | + circuit = CircuitBreaker.create("my-circuit", vertx, |
| 30 | + new CircuitBreakerOptions() |
| 31 | + .setFallbackOnFailure(true) // Call the fallback on failures |
| 32 | + .setTimeout(2000) // Set the operation timeout |
| 33 | + .setMaxFailures(3) // Number of failures before switching to the 'open' state |
| 34 | + .setResetTimeout(5000)); |
| 35 | + |
| 36 | + |
| 37 | + Router router = Router.router(vertx); |
| 38 | + router.get("/").handler(this::invokeHelloMicroservice); |
| 39 | + router.get("/health").handler(HealthCheckHandler.create(vertx).register("http-server-running", |
| 40 | + future -> future.complete(ready ? Status.OK() : Status.KO()))); |
| 41 | + |
| 42 | + // Create the service discovery instance |
| 43 | + ServiceDiscovery.create(vertx, discovery -> { |
| 44 | + // Look for a HTTP endpoint named "hello-microservice" |
| 45 | + Single<WebClient> single = HttpEndpoint.rxGetWebClient(discovery, |
| 46 | + rec -> rec.getName().equalsIgnoreCase("hello-microservice")); |
| 47 | + |
| 48 | + single.subscribe( |
| 49 | + client -> { |
| 50 | + // the configured hello to call our microservice |
| 51 | + this.hello = client; |
| 52 | + // start the HTTP server |
| 53 | + vertx.createHttpServer() |
| 54 | + .requestHandler(router::accept) |
| 55 | + .listen(8080, ar -> ready = ar.succeeded()); |
| 56 | + }, |
| 57 | + err -> System.out.println("Oh no, no service") |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + private void invokeHelloMicroservice(RoutingContext rc) { |
| 65 | + circuit.rxExecuteCommandWithFallback( |
| 66 | + future -> { |
| 67 | + HttpRequest<JsonObject> request1 = hello.get("/Luke").as(BodyCodec.jsonObject()); |
| 68 | + |
| 69 | + HttpRequest<JsonObject> request2 = hello.get("/Leia").as(BodyCodec.jsonObject()); |
| 70 | + |
| 71 | + Single<HttpResponse<JsonObject>> s1 = request1.rxSend(); |
| 72 | + Single<HttpResponse<JsonObject>> s2 = request2.rxSend(); |
| 73 | + |
| 74 | + Single |
| 75 | + .zip(s1, s2, (luke, leia) -> { |
| 76 | + // We have the result of both request in Luke and Leia |
| 77 | + return new JsonObject() |
| 78 | + .put("luke", luke.body().getString("message") |
| 79 | + + " " + luke.body().getString("served-by")) |
| 80 | + .put("leia", leia.body().getString("message") |
| 81 | + + " " + leia.body().getString("served-by")); |
| 82 | + }) |
| 83 | + .subscribe(future::complete, future::fail); |
| 84 | + }, |
| 85 | + error -> new JsonObject().put("message", "hello (fallback, " + circuit.state().toString() + ")") |
| 86 | + ).subscribe( |
| 87 | + x -> rc.response().end(x.encodePrettily()), |
| 88 | + t -> rc.response().end(t.getMessage())); |
| 89 | + } |
| 90 | +} |
0 commit comments