際際滷

際際滷Share a Scribd company logo
Building Reactive Microservices with Vert.x
Claudio Eduardo de Oliveira
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
Agenda
 Microservices
 Definition
 Patterns
 Reactive Manifesto
 Reactive Systems
 Reactive Programming
 Vert.x
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
Example microservices
https://cdn.wp.nginx.com/wp-content/uploads/2016/04/Richardson-microservices-part1-2_microservices-architecture.png
Microservices Patterns
 Circuit Breakers
 Service Discovery
microservices
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
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
reactiveReactive Manifesto
http://www.reactivemanifesto.org/
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
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
We will talk about Reactive Systems.
reactive
Some JVM players reactive
Vertx daitan
Vertx daitan
Definition
Eclipse Vert.x is a toolkit for building
reactive applications on the JVM
vert.x
http://vertx.io/
Highlights
 Non-Blocking (vert.x
core)
 Polyglot
 Event Bus
 General purpose
 Unopinionated
vert.x
NO Black Magic - Spring Way vert.x
@SpringBootApplication
@EnableHystrix
public class MusicRecommendationApplication {
public static void main(String[] args) {
SpringApplication
.run(MusicRecommendationApplication.class, args);
}
}
NO Black Magic vert.x
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
.}
Vert.x Way vert.x
public class EventLoopVerticle extends AbstractVerticle {
private static final Logger LOGGER = LoggerFactory.getLogger(EventLoopVerticle.class);
public void start() {
final Router router = Router.router(vertx);
router.get("/test").handler(req -> {
LOGGER.info(" receiving request");
req.response()
.putHeader("content-type","text/plain")
.end(NormalProcess.process());
});
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
}
Dependencies - Maven vert.x
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
Core Concepts
Verticle
vert.x
Event Bus
Verticle vert.x
 Small Vert Unit
 Regular Verticle
 Worker Verticle
 Multi Threaded Worker
 Automatic node discovery
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
Regular Verticle - Event Loop vert.x
Reactor Pattern - Vert.x vert.x
Demo vert.x
Regular Verticle
 Golden Rule - Dont block me!
 Event Loop (more than one)
 Multi Reactor Pattern
 High throughput (i.e http)
vert.x
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
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);
}
}
Worker Verticle
 Vert.x worker thread pool
 Execute blocking code
 They are never executed by more than one thread
 Background tasks
vert.x
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());
}
}
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
Event Bus
 Nervous system of Vert.x
 Publish / Subscribe
 Point to Point
 Request Response
 Can be distributed
vert.x
Event Bus vert.x
http://allegro.tech/2015/11/real-time-web-application-with-websockets-and-vert-x.html
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);
}
}
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/
Service Discovery
Service provider
Publish / Unpublish service record
Update the status of a service
vert.x
http://vertx.io/docs/vertx-service-discovery/java/
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/
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);
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"));
Service Discovery - Consumer vert.x
final ServiceDiscoveryOptions serviceDiscoveryOptions = new ServiceDiscoveryOptions()
.setBackendConfiguration(
new JsonObject()
.put("host", "127.0.0.1")
.put("port", "6379")
);
ServiceDiscovery discovery = ServiceDiscovery.create(vertx,serviceDiscoveryOptions);
.
HttpEndpoint.rxGetWebClient(discovery, rec -> rec.getName().endsWith("product"))
.flatMap(client -> client.get("/product/" + id).as(BodyCodec.string()).rxSend())
.subscribe(response -> req.response()
.putHeader("content-type", "application/json")
.end(response.body()));
});
Service Discovery
 Consul
 Kubernetes
 Redis
 Docker
vert.x
Circuit Breaker vert.x
https://www.oreilly.com/ideas/microservices-antipatterns-and-pitfalls
breaker.executeWithFallback(
future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
response
.exceptionHandler(future::fail)
.bodyHandler(buffer -> {
future.complete(buffer.toString());
});
}
});
}, v -> {
// Executed when the circuit is opened
return "Hello";
})
.setHandler(ar -> {
// Do something with the result
});
Circuit Breaker vert.x
Relational Database Integrations
 MySQL
 PostgreSQL
 JDBC
vert.x
NoSQL Database Integrations
 Redis
 MongoDB
 Cassandra
 OrientDB
vert.x
Messaging
 Kafka
 MQTT
 ZeroMQ
 RabbitMQ
 AMQP 1.0
vert.x
vert.xDemo
Demo vert.x
Vertx daitan
References
http://vertx.io/docs/guide-for-java-devs/
https://www.oreilly.com/ideas/reactive-programming-vs-reactive-systems
https://dzone.com/articles/understanding-reactor-pattern-thread-based-and-eve
https://developers.redhat.com/promotions/building-reactive-microservices-in-java/
https://github.com/claudioed/travel-helper

More Related Content

Vertx daitan

  • 1. Building Reactive Microservices with Vert.x Claudio Eduardo de Oliveira
  • 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
  • 3. Agenda Microservices Definition Patterns Reactive Manifesto Reactive Systems Reactive Programming Vert.x
  • 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
  • 6. Microservices Patterns Circuit Breakers Service Discovery microservices
  • 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
  • 12. We will talk about Reactive Systems. reactive
  • 13. Some JVM players reactive
  • 16. Definition Eclipse Vert.x is a toolkit for building reactive applications on the JVM vert.x http://vertx.io/
  • 17. Highlights Non-Blocking (vert.x core) Polyglot Event Bus General purpose Unopinionated vert.x
  • 18. NO Black Magic - Spring Way vert.x @SpringBootApplication @EnableHystrix public class MusicRecommendationApplication { public static void main(String[] args) { SpringApplication .run(MusicRecommendationApplication.class, args); } }
  • 19. NO Black Magic vert.x @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { .}
  • 20. Vert.x Way vert.x public class EventLoopVerticle extends AbstractVerticle { private static final Logger LOGGER = LoggerFactory.getLogger(EventLoopVerticle.class); public void start() { final Router router = Router.router(vertx); router.get("/test").handler(req -> { LOGGER.info(" receiving request"); req.response() .putHeader("content-type","text/plain") .end(NormalProcess.process()); }); vertx.createHttpServer().requestHandler(router::accept).listen(8080); } }
  • 21. Dependencies - Maven vert.x <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-dependencies</artifactId> <version>${vertx.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> </dependency>
  • 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
  • 25. Regular Verticle - Event Loop vert.x
  • 26. Reactor Pattern - Vert.x vert.x
  • 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/
  • 38. Service Discovery Service provider Publish / Unpublish service record Update the status of a service 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"));
  • 42. Service Discovery - Consumer vert.x final ServiceDiscoveryOptions serviceDiscoveryOptions = new ServiceDiscoveryOptions() .setBackendConfiguration( new JsonObject() .put("host", "127.0.0.1") .put("port", "6379") ); ServiceDiscovery discovery = ServiceDiscovery.create(vertx,serviceDiscoveryOptions); . HttpEndpoint.rxGetWebClient(discovery, rec -> rec.getName().endsWith("product")) .flatMap(client -> client.get("/product/" + id).as(BodyCodec.string()).rxSend()) .subscribe(response -> req.response() .putHeader("content-type", "application/json") .end(response.body())); });
  • 43. Service Discovery Consul Kubernetes Redis Docker vert.x
  • 45. breaker.executeWithFallback( future -> { vertx.createHttpClient().getNow(8080, "localhost", "/", response -> { if (response.statusCode() != 200) { future.fail("HTTP error"); } else { response .exceptionHandler(future::fail) .bodyHandler(buffer -> { future.complete(buffer.toString()); }); } }); }, v -> { // Executed when the circuit is opened return "Hello"; }) .setHandler(ar -> { // Do something with the result }); Circuit Breaker vert.x
  • 46. Relational Database Integrations MySQL PostgreSQL JDBC vert.x
  • 47. NoSQL Database Integrations Redis MongoDB Cassandra OrientDB vert.x
  • 48. Messaging Kafka MQTT ZeroMQ RabbitMQ AMQP 1.0 vert.x

Editor's Notes

  • #5: Architectural style independently deployable services connected with reactive manifesto Smart endpoints dumb pipes
  • #6: Some challenges in this kind of architecture How to discover a lot of number of services One of this services fails..what happen??
  • #7: Circuit breakers are connected with Resilience Service discovery is connected with Responsive 3.3.0 Vert.x version
  • #8: Hystrix is a famous netflix implementation
  • #9: Eureka, zookeeper, consul and kubernetes
  • #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
  • #15: Vert.x was started by Tim Fox in 2011
  • #16: Vert.x was started by Tim Fox in 2011
  • #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
  • #22: Add your dependencies and that is it
  • #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
  • #33: Remember to set worker TRUE on deploy.
  • #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