The document discusses building reactive microservices using Vert.x. It begins with definitions of microservices and reactive systems. It then covers reactive programming and the reactive manifesto. A large portion of the document is dedicated to explaining Vert.x, including that it is a toolkit for building reactive applications on the JVM in a non-blocking way. It covers Vert.x concepts like verticles, the event bus, and service discovery. It provides examples of how to implement various Vert.x patterns and components like regular verticles, worker verticles, the event bus, circuit breakers, and service discovery.
2. About me
Claudio Eduardo de Oliveira
● Developer @ Daitan Group
● Bacharel em Ciência da Computação
● Cursando MBA em Arquitetura de Soluções em
Tecnologia (DeVry/Metrocamp)
● Entusiasta Docker / Spring / Vert.x
Contatos:
Email: claudioed.oliveira@gmail.com
Linkedin: https://br.linkedin.com/in/claudioedoliveira
Twitter: @claudioed
4. Definition
The term "Microservice Architecture" has sprung up over the last
few years to describe a particular way of designing software
applications as suites of independently deployable services.
While there is no precise definition of this architectural style, there
are certain common characteristics around organization around
business capability, automated deployment, intelligence in the
endpoints, and decentralized control of languages and data.
microservices
https://martinfowler.com/articles/microservices.html
7. Circuit Breaker microservices
“The basic idea behind the circuit breaker is very simple. You
wrap a protected function call in a circuit breaker object,
which monitors for failures”
https://martinfowler.com/bliki/CircuitBreaker.html
8. Service Discovery microservices
Service discovery is the automatic detection of devices and
services offered by these devices on a computer network
https://en.wikipedia.org/wiki/Service_discovery
10. Reactive Systems
as defined by the Reactive Manifesto—is a set of
architectural design principles for building modern
systems that are well prepared to meet the
increasing demands that applications face today.
https://www.lightbend.com/reactive-programming-versus-reactive-systems
reactive
11. Reactive Programming
In computing, reactive programming is an
asynchronous programming paradigm
concerned with data streams and the
propagation of change
https://en.wikipedia.org/wiki/Reactive_programming
reactive
19. Verticle vert.x
● Small Vert Unit
● Regular Verticle
● Worker Verticle
● Multi Threaded Worker
● Automatic node discovery
20. Reactor Pattern
The reactor pattern is one implementation technique of
event-driven architecture. In simple terms, it uses a single
threaded event loop blocking on resource-emitting events
and dispatches them to corresponding handlers and
callbacks
vert.x
https://dzone.com/articles/understanding-reactor-pattern-thread-based-and-eve
22. Regular Verticle
● Golden Rule - Don’t block me!
● Event Loop (more than one)
● Multi Reactor Pattern
● High throughput (i.e http)
vert.x
23. public class EventLoopVerticle extends AbstractVerticle {
public void start() {
final Router router = Router.router(vertx);
router.get("/test").handler(req -> {
req.response()
.putHeader("content-type","text/plain")
.end(NormalProcess.process());
});
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
}
Regular Verticle - Example vert.x
24. Don't do this !!!! vert.x
public class EventLoopBlockerVerticle extends AbstractVerticle {
public void start() {
final Router router = Router.router(vertx);
router.get("/test").handler(req -> {
LongRunningProcess.longProcess();
final String date = LocalDateTime.now().toString();
req.response()
.putHeader("content-type","text/plain")
.end(String.format("TDC 2017 %s", date));
});
vertx.createHttpServer().requestHandler(router::accept).listen(8081);
}
}
25. Worker Verticle
● Vert.x worker thread pool
● Execute blocking code
● They are never executed by more than one thread
● Background tasks
vert.x
26. Worker Verticle vert.x
public class EventLoopBusVerticle extends AbstractVerticle {
public void start() {
this.vertx.deployVerticle(new BusVerticle(),
new DeploymentOptions().setWorker(true));
this.vertx.deployVerticle(new RequestResourceVerticle());
}
}
27. Multi Threaded Verticle
● Based on worker verticle
● Can be executed concurrently by different threads
● Hard to code because you need to maintain states
between verticles
● Specific needs
vert.x
28. Event Bus
● Nervous system of Vert.x
● Publish / Subscribe
● Point to Point
● Request Response
● Can be distributed
vert.x
29. Event Bus vert.x
public class RequestResourceVerticle extends AbstractVerticle {
private static final Logger LOGGER = LoggerFactory.getLogger(BusVerticle.class);
public void start() {
final EventBus eventBus = vertx.eventBus();
final Router router = Router.router(vertx);
router.get("/test").handler(req -> {
eventBus.send("data-stream", new JsonObject(), responseBus -> {
if (responseBus.succeeded()) {
req.response()
.putHeader("content-type", "text/plain")
.end(responseBus.result().body().toString());
}
});
});
vertx.createHttpServer().requestHandler(router::accept).listen(8082);
}
}
30. Service Discovery
This component provides an infrastructure to publish and
discover various resources, such as service proxies, HTTP
endpoints, data sources…
vert.x
http://vertx.io/docs/vertx-service-discovery/java/
32. Service Discovery
Service consumer
Lookup service
Bind to a selected service
Release the service
Listen for arrival and departure
vert.x
http://vertx.io/docs/vertx-service-discovery/java/
33. Service Discovery - Backend vert.x
final ServiceDiscoveryOptions serviceDiscoveryOptions = new
ServiceDiscoveryOptions()
.setBackendConfiguration(
new JsonObject()
.put("host", "127.0.0.1")
.put("port", "6379")
);
ServiceDiscovery sd = ServiceDiscovery.create(vertx,serviceDiscoveryOptions);
34. Service Discovery - Publish vert.x
vertx.createHttpServer().requestHandler(router::accept)
.rxListen(8083)
.flatMap(httpServer -> discovery
.rxPublish(HttpEndpoint.createRecord("product", "localhost", 8083, "/")))
.subscribe(rec -> LOGGER.info("Product Service is published"));