كما تعلم ، من يقتل تنينًا يصبح تنينًا بنفسه. كان الربيع كإطار للأغراض العامة جيدًا جدًا مقارنةً بـ java EE منذ 10 سنوات. لكنه الآن أصبح وحشيًا جدًا وثقيلًا في صعوده. سننظر اليوم إلى 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 -> {....
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. , -. – .