什么是Vertx,为什么适用于RSHB

如您所知,杀死一条龙的人自己都会成为一条龙。与10年前的Java EE相比,Spring作为通用框架非常出色。但是现在,他已经变得非常庞然大物了,并且在上升。今天,我们将把Vertx视为构建微服务的框架。



什么是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