The document discusses building reactive microservices using Vert.x. It begins with definitions of microservices and reactive systems. It then covers Vert.x, a toolkit for building reactive applications on the JVM. Key concepts discussed include verticles, the event bus, and service discovery. It provides examples of regular verticles that use the event loop pattern and worker verticles for blocking operations. The document also discusses integrations with databases, messaging systems, and circuit breakers.
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 Manifestois 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
18. NO Black Magic - Spring Way vert.x
@SpringBootApplication
@EnableHystrix
public class MusicRecommendationApplication {
public static void main(String[] args) {
SpringApplication
.run(MusicRecommendationApplication.class, args);
}
}
23. Verticle vert.x
Small Vert Unit
Regular Verticle
Worker Verticle
Multi Threaded Worker
Automatic node discovery
24. 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
28. Regular Verticle
Golden Rule - Dont block me!
Event Loop (more than one)
Multi Reactor Pattern
High throughput (i.e http)
vert.x
29. 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
30. 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);
}
}
31. Worker Verticle
Vert.x worker thread pool
Execute blocking code
They are never executed by more than one thread
Background tasks
vert.x
32. 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());
}
}
33. 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
34. Event Bus
Nervous system of Vert.x
Publish / Subscribe
Point to Point
Request Response
Can be distributed
vert.x
36. 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);
}
}
37. 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/
39. 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/
40. 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);
41. 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"));
#10: Responsive = responsiveness means that problems may be detected quickly and dealt with effectively, they deliver a consistent quality of service. (Event Loop)
Resilient = The system stays responsive in the face of failure. (Circuit Breaker)
Elastic = The system stays responsive under varying workload. Reactive Systems can react to changes in the input rate by increasing or decreasing the resources allocated to service these inputs (Service Discovery)
Message Driven = Reactive Systems rely on asynchronous message-passing to establish a boundary between components that ensures loose coupling, isolation and location transparency. (Event Bus)
#11: Architecture and Design Style
Immutable data
The data maybe are persistent between nodes
In a message-driven system addressable recipients await the arrival of messages
Specific destination
#12: Event is a signal by a component upon reaching a given state
Callback based
Increase vertical scalability ( multi core processors)
Immutable data
Event driven not Message Driven, events are signal and not persistent
Thinking in process or program
#17: isn t a framework or library
Is a open-source and eclipse foundation project
Runnable jar
Distributed by nature
#18: Non-blocking built on top netty library
Java, Scala, Groovy, Kotlin, Ceylon and Javascript
General purpose mean serve a static html files, serve an api, create a long running process or batch applications
We can choose your DI framework, your favorite database or favorite cache for instance
Zero annotation (NO BLACK MAGIC) , you must code and please use lambda is awesome
#19: Non-blocking built on top netty library
Java, Scala, Groovy, Kotlin, Ceylon and Javascript
General purpose mean serve a static html files, serve an api, create a long running process or batch applications
We can choose your DI framework, your favorite database or favorite cache for instance
Zero annotation (NO BLACK MAGIC) , you must code and please use lambda is awesome
#20: Non-blocking built on top netty library
Java, Scala, Groovy, Kotlin, Ceylon and Javascript
General purpose mean serve a static html files, serve an api, create a long running process or batch applications
We can choose your DI framework, your favorite database or favorite cache for instance
Zero annotation (NO BLACK MAGIC) , you must code and please use lambda is awesome
#21: Non-blocking built on top netty library
Java, Scala, Groovy, Kotlin, Ceylon and Javascript
General purpose mean serve a static html files, serve an api, create a long running process or batch applications
We can choose your DI framework, your favorite database or favorite cache for instance
Zero annotation (NO BLACK MAGIC) , you must code and please use lambda is awesome
#23: Verticle can be standard, worker and multi threaded worker
Event Bus can be clustered (hazelcast, zookeeper, infinispan and others)
#24: Verticle has some similarities with the Actor Model
Standard: Verticles based upon the event loop model. As mentioned, Vert.x allows to configure the number of event loop instances.
Worker: Verticles based upon the classical model. A pool of thread is defined, and each incoming request will consume one available thread.
Multi-threaded worker: An extension of the basic worker model. A multi-threaded worker is managed by one single instance but can be executed concurrently by several threads.
Extends AbstractVerticle
#25: Java application servers or web servers are implemented in Thread Based Architecture (blocking)
Main actors are Reactor and Handlers
Reactor - react to event to IO Events and dispatching to correct handlers
Handler - perform the actual works, handlers must be non-blocking
#26: Remember this is one Reactor Pattern, but vert.x is multi reactor, in general is your CPU cores
#27: Remember this is one Reactor Pattern, but vert.x is multi reactor, in general is your CPU cores
#29: Default configuration is the events loops are the number of cores
Similar nodejs but with JVM power
#31: This cause a block in event loop and your application is not answer anymore
#32: About the are never executed by more than one thread means easily to manage state, because runs in one thread
Long running process for instance consumes a kafka stream
#34: Advanced feature and most application will have no need for them
Because of the concurrency in these verticles you have to be very careful to keep the verticle in a consistent state using standard Java techniques for multi-threaded programming
#35: The event bus allows different parts of your application to communicate with each other irrespective of what language they are written in
Can communicate with javascript in a browser
Can be distributed hazelcast , infinispan and others
Simple to use but powerfull, register handler, unregister, sending and publishing messages
#37: Address is a simple string, but keep in a mind in a kind of scheme is make helpfull
Send - point to point messages direct
Publish - all the handlers subscribed
#41: In this case we are using the redis backend for service discovery
#42: In this case we are using the javaRX wrappers for vert.x, for me is more easier than synchronous version
Subscribe must be called to register
#43: Backend Configuration
Get record reactive filtering