Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
JavaLand
Kristian Kottke
Going ReactiveReactive Microservices
based on Vert.x
©it
era
tec
Whoami
Kristian Kottke
› Lead Software Engineer -> iteratec
Interests
› Software Architecture
› Big Data Technologies
@_kkottke
github.com/kkottke
xing.to/kkottke
speakerdeck.com/kkottke
©iteratec
©iteratec
Photo by Markus Spiske on Unsplash
©iteratec
Microservice
cloud native
fau
lt t
ole
ran
ce
highly responsive
scalable
resilienthigh a
vailabili
ty
multi core
asynchronousflexibleloosely coupled
elastic
Reactive System
©iteratec
Reactive Systems
©iteratec
Reactive Manifesto
Responsive
ResilientElastic
Message Driven
©iteratec
Reactive Programming
!=
Reactive Systems
©iteratec
©iteratec
...a toolkit for building reactive, non-blocking, asynchronous applications
on the JVM
(based on netty with additional modules)
©iteratec
!=
©iteratec
==
©iteratec
!=
©iteratec
©iteratec
Event Bus
Host
JVM
Vert.x Instance
Verticle
(Java)
Verticle
(Java)
Verticle
(JS)
Verticle
(Kotlin)
JVM
Vert.x Instance
Verticle
(Java)
Verticle
(Java)
Verticle
(JS)
Verticle
(Kotlin)
©iteratec
Event Bus
Host
JVM
Vert.x Instance
Verticle
(Java)
Verticle
(Java)
Verticle
(JS)
Verticle
(Kotlin)
Host
JVM
Vert.x Instance
Verticle
(Java)
Verticle
(Java)
Verticle
(JS)
Verticle
(Kotlin)
©iteratec
Event Bus
Host
JVM
Vert.x Instance
Verticle
(Java)
Verticle
(Java)
Verticle
(JS)
Verticle
(Kotlin)Event Bus Bridge
©iteratec
Reactor Pattern
Event Queue
Event LoopHandler
Handler
Handler
©iteratec
Multi-Reactor Pattern
...
©iteratec
Code
©iteratec
Messa
ge
Traders
Generator
Service
Portfolio
Messa
ge
AuditElasticSearch
Kibana
Message
REST
©iteratec
public static void main(String[] args) {
Vertx vertx = new Vertx();
vertx.deployVerticle(new RestApiVerticle());
}
Verticle
©iteratec
public void start(Future<Void> future) {
HttpServer server = vertx.createHttpServer()
.requestHandler(req -> req.response().end(„hello“));
server.listen(8080, res -> {
if (res.succeeded()) {
future.complete();
} else {
future.fail(res.cause());
}});
}
RestApiVerticle
©it
era
tec
Don’t call us, we’ll call you!
› Synchronous (blocking)
compute(1, 2);
› Asynchronous (non-blocking)
compute(1, 2, res -> {…});
› Everything is an event → Handler → Callback Hell → RX
©it
era
tec
Digression: ReactiveX
› API for async programming with observable streams
› Observable
› Evolution of Callback, Promise, Future
› Subscribe: next, error, complete
› Completable, Single, Maybe, Flowable
› Reactive Operators
Observable
Observable
Subcriber
onNext
Produce
©iteratec
public Completable rxStart() {
return vertx.createHttpServer()
.requestHandler(req -> req.response().end(„hello“))
.rxListen(8080)
.ignoreElement();
}
RestApiVerticle (rxified)
©iteratec
public Completable rxStart() {
return OpenAPI3RouterFactory.rxCreate(vertx, „api.yml“)
.map(factory -> {
factory.addHandlerByOperationId(„op“, this:op);
return factory.getRouter();
})
.flatMap(router -> vertx.createHttpServer()
.requestHandler(router)
.rxListen(8080))
.ignoreElement();
}
Web API Contract
©iteratec
public void publish(Message mes) {
vertx.eventBus().publish(„addr“, Json.encode(mes));
vertx.eventBus().rxSend(„addr“, Json.encode(mes));
}
public void handle() {
vertx.eventBus().<String>consumer(„addr“, m -> {
Message mes = Json.decodeValue(m.body(), Message.class);
....
}
}
Event Bus
©iteratec
Messa
ge
Traders
Generator
Service
Portfolio
Messa
ge
AuditElasticSearch
Kibana
Message
REST
©iteratec
@ProxyGen
@VertxGen
public interface TradingService {
@Fluent
TradingService buy(int amount, JsonObject json,
Handler<AsyncResult<Portfolio>> handler);
}
Service Proxy (RPC)
©iteratec
public void publish() {
ServiceDiscovery sd = ServiceDiscovery.create(vertx);
Record http = HttpEndpoint.createRecord(...);
Record message = MessageSource.createRecord(...);
Record service = EventBusService.createRecord(...);
sd.rxPublish(http);
}
Service Discovery
©iteratec
public void consume() {
ServiceDiscovery sd = ServiceDiscovery.create(vertx);
client = HttpEndpoint.rxGetWebClient(sd, json);
consumer = MessageSource.rxGetConsumer(sd, json);
proxy = EventBusService.getServiceProxy(sd, ...);
}
Service Discovery
©it
era
tec
Failure Handling
› Error Handler
› subscribe(success, error)
› Error Operator
› doOnError, onErrorReturn, ...
› Timeout Operator
› timeout
› Retry Operator
› retry, retryWhen
› Circuit Breaker
› breaker.execute(...)
©it
era
tec
Are we reactive now?
› Reactive Programming
› Asynchronous, non-blocking
› Event-Driven
› Reactive System
› Responsive
› Resilient
› Elastic
› Message-Driven
©iteratec
©it
era
tec
Modules
› Config
› Health Check
› Web (SockJS)
› Unit / Junit 5
› Web API Service
› gRPC
› Sync
©it
era
tec
When/Why Vert.x?
› Small Services
› Reactive Application
› Scalability
› Cloud Environment
› Polyglot
©iteratec
Demo
©it
era
tec
Wrap Up
› Toolkit for building reactive, non-blocking applications
› Multi-Reactor Pattern & Event Bus
› Asynchronous
› Event-Driven
› Performance & Scalability
©iteratec
@_kkottke
github.com/kkottke
xing.to/kkottke
speakerdeck.com/kkottke
https://github.com/kkottke/stock-trading.git
www.iteratec.de
Contact
Kristian Kottke
@_kkottke
github.com/kkottke
xing.to/kkottke
speakerdeck.com/kkottke