¿Qué es Vertx y por qué es adecuado para RSHB?

Como sabes, quien mata a un dragón se convierte él mismo en dragón. Spring, como marco de trabajo de propósito general, fue muy bueno en comparación con Java EE hace 10 años. Pero ahora se ha vuelto muy monstruoso y pesado en ascenso. Hoy consideraremos Vertx como un marco para construir microservicios.



¿Qué es 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