What is Vertx and why is it suitable for RSHB

As you know, whoever kills a dragon becomes a dragon himself. Spring as a general purpose framework was very good compared to java EE 10 years ago. But now he has become very monstrous and heavy on the rise. Today we will consider Vertx as a framework for building microservices.



What is Vertx?





- : ; , . , , RedHat Bosch.



verticle(). . java (event bus).



Vertx - โ€“ , . : Java, Kotlin, JavaScript, Groovy, Ruby, Scala.



Vertx , , git maven .



http://vertx.io. http://vertx.io/docs .



Vertx?



, , ยซ ยป. , Vertx?



. , , , . Vertx , Kubernetes verticle. docker .



, . Vetrtx . Vertx โ€” .





rest api Prometheus web socket.



(2 rest endpoint). rest , . micrometer web socket.

, .

http://start.vertx.io, 4 :



  • Vert.x Web
  • Vert.x Web Client
  • Metrics using Micrometer
  • SockJS Service Proxies


โ€“ .

:



./mvnw clean compile exec:java


http://localhost:8888 : Hello from Vert.x!



-, . ยซยป jar 7,5 !



, , .



1. HTTP



Vertx http Router. :



Router router = Router.router(vertx); // 
router.get("/hello").handler(rc -> { //   GET    /hello
  rc.response()
    .putHeader(HttpHeaders.CONTENT_TYPE, "text/plain")
    .end("Hello from Vert.x!");
});

vertx.createHttpServer()
  .requestHandler(router) // http      
  .listen(8888, http -> {....


http://localhost:8888/hello.



2. REST API



API 2- api :





, :



private Router createRestRouter(WebClient webClient) {
  Router restApi = Router.router(vertx);
  restApi.get("/rshb_bonds").handler(rc -> {
    webClient
      .get(80, "iss.moex.com", "/iss/securities.json")
      .addQueryParam("q", "")
      .send(response -> {
        rc.response()
          .putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
          .end(processMoexBondsRequest(response.result().bodyAsJsonObject()).encodePrettily());
      });
  });
  restApi.get("/rshb_bonds/:bondId").handler(rc -> { // url   
    String bondId = rc.request().getParam("bondId");
      webClient
        .get("/iss/securities/"+ bondId +".json")
        .send(response -> {
          rc.response()
            .putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
            .end(
                processMoexBondDescriptionRequest(response.result().bodyAsJsonObject())
            );
        });
    });
  return restApi;
}


processMoexBondsRequest, processMoexBondDescriptionRequest github. . .



c api โ€œ/rest/api/v1โ€.



router.mountSubRouter("/rest/api/v1/", createRestRouter(webClient));


, http rest http . Vertx, , : ssl, , .. :



WebClientOptions webClientOptions = new WebClientOptions();
webClientOptions //  ,        
    .setDefaultPort(80)
    .setDefaultHost("iss.moex.com");
WebClient webClient = WebClient.create(vertx, webClientOptions);


! api, url:

http://localhost:8888/rest/api/v1/rshb_bonds

http://localhost:8888/rest/api/v1/rshb_bonds/RU000A101WQ2



3.



, Micrometer metrics, , , Vertx. io.vertx.core.Launcher, . , , pom.xml



public class LauncherWithMetrics extends Launcher {
  public static void main(String[] args) {
    new LauncherWithMetrics().dispatch(args); //     
  }

  @Override
  public void beforeStartingVertx(VertxOptions options) {
    options.setMetricsOptions(
      new MicrometerMetricsOptions()
        .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true))
        .setEnabled(true));
  }
}


/metrics



router.route("/metrics").handler(PrometheusScrapingHandler.create());


. Prometheus . , โ€“ .



4.



, . , :



public class MetricsBusPublisher extends AbstractVerticle {
  @Override
  public void start(Promise<Void> startPromise) {
    MetricsService metricsService = MetricsService.create(vertx);
    vertx.setPeriodic(
      1000,
      h ->
        vertx.eventBus().publish("metrics", metricsService.getMetricsSnapshot().encode())
    );
    startPromise.complete();
  }
}


:



SockJSBridgeOptions opts = new SockJSBridgeOptions()
  .addOutboundPermitted(new PermittedOptions()
    .setAddress("metrics")); //     ,     ,       .      .
router.mountSubRouter("/eventbus", SockJSHandler.create(vertx).bridge(opts));


webSocket.html .





github:

https://github.com/RshbExample/VertxFirstSteps.git





, , Vertx. , -. โ€“ .




All Articles