Apa itu Vertx dan mengapa ini cocok untuk RSHB

Seperti yang Anda ketahui, siapa pun yang membunuh naga menjadi naga sendiri. Spring sebagai framework tujuan umum sangat bagus dibandingkan dengan java EE 10 tahun lalu. Tapi sekarang dia telah menjadi sangat mengerikan dan berat saat bangkit. Hari ini kami akan mempertimbangkan Vertx sebagai kerangka kerja untuk membangun layanan mikro.



Apa itu 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