BUILDING MICROSERVICES WITH VERT.X 3.0
AGRAJ MANGAL
AGENDA• Microservices• Why, why not ?• Comparison with Monolithic Architecture
• Vert.x• Concepts: Event Loop, Verticles, Event Bus• Modules: core, web
• Comparisons• Real-life Example - Pulse
MONOLITHIC ARCHITECTURE
• Logically Different Modules• But Packaged & deployed as a Single Unit• Initial Phases of Project
• Simple to Deploy• Vertical Scaling
• Later on• Difficult to Manage & Scale• Longer Startup Times• Slow down Development • CI becomes challenging
MICROSERVICES PATTERN
• Split into Smaller, Interconnected Services• Loose Coupling• Service == Functional Area• Service exposes APIs – consumed by other services & clients
FINE GRAINED VIEW
BENEFITS• Enforces Modularity
• Decomposing complexity• Manageable chunks• Each service – well defined boundary
• Independent Development• Different Teams• Different Technologies• Easy to test
• Scaling is Easy• Each service can be scaled independently• Different Service might have different requirements ( CPU, Memory )
SCALING MICROSERVICES
SERVICES COMMUNICATING
DRAWBACKS
• Partitioned Database Architecture• Have to settle for Eventual Consistency• ACID transactions not possible
• Deploying• Though scalable, but more complex• Many more moving parts• Service Discovery required
• Polyglot (Java, JavaScript, Groovy, Ruby, Python etc.)• Event-driven & Non-blocking programming model• Super simple Concurrency Model• Lightweight ~ 650Kb• “Ideal choice for creating light-weight, high-performance, microservices”
• Public module repository
• Reactive applications
• Asynchronous APIs
CONCEPTS• Event Loop • Verticles• Server Verticles• Worker Verticles
• Event Bus• Point to Point• Pub/Sub• Distributed
THE FAMOUS EVENT LOOP
ARCHITECTURE
• Don’t Block the Event Loop• Workers “can” block• Message Passing using Event Bus• Concurrency Model• A Verticle instance is always Single threaded• No more Locking, synchronized & race conditions• Actor-like concurrency model
• Scaling• By Creating more Verticle Instances• For TCP & HTTP servers, Vert.x does automatic load balancing
• Use FAT Jar for Deployment
VERT.X MODULES• Core• Web• Data Access
• MongoDB, JDBC, Redis, MySQL
• Authentication Modules• JWT, OAuth 2, JDBC Auth, Shiro Auth, MongoDB auth
• Messaging Systems• Kafka, RabbitMQ
• Clustering – Hazelcast
VERT.X CORE• Servers & clients
• TCP/SSL• HTTP/HTTPS• Websocket & SockJS
• EventBus• Shared Maps & Sets• Buffers & Flow Control• Container API – Deploy & Undeploy Verticles• Timers & Files• Logging• Configuration
VERT.X HTTP SERVER – HELLO WORLD
VERT.X WEB• Routing• Regex Matching• Request Body Handling, parameters extraction• Cookie Parsing & Handling• Multipart Form & File Upload• Session support ( sticky & non-sticky )• CORS & CSRF Support• Authentication & Authorization• SockJS Support
VERT.X WEB HELLO WORLD
ROUTING
ROUTING• Chain of Routers• Either “end” it• Or pass it to the “next” one
• Various Options – Route by• HTTP Method Type• Exact Path• Regex Matching• MIME type of request• Decide Routing Order
VERT.X WEB• BodyHandler• Retrieve Request Body• Limit Body Size• Handle File Uploads
• CookieHandler• Get, Add, Delete Cookie
• SessionHandler• Sticky & Non-Sticky Sessions• Vert.x don’t put actual data in Session Cookie – Session UUID is used to
lookup data on the server• Session timeouts
VERT.X WEB• Authentication & Authorization• Support for Basic-Auth, Redirect-Auth, FormLogin• JWT • OAuth2
• Static Resources• StaticHandler• Caching – set headers ( cache-control, last-modified, date )• Configurable webroot, index page• Disable File Caching - .vertx
• Templating Support• Handlebars, Jade, MVEL, Thymeleaf
• CORS & CSFR Handlers
DISTRIBUTED SUPPORT
DISTRIBUTED EVENT BUS• Connect multiple Vert.x instances
across JVMs• Event bus extends to client side
Javascript• Ideal for “real-time” web
applications• vertx-eventbus.js
CLUSTERING• Hazelcast• Shared Data Structures
REALTIME COMMUNICATION - SOCKJS• Excellent Support for Low-latency, full-duplex cross-communication channel• Tries
• Native Websocket• Browser specific transport protocols• Polling for old browsers
• Heartbeats – prevent load balancers & proxies to close long running http requests• Vert.x – built in support for SockJS • SockJS event bus bridge
• Distributed event bus • Extend’s vert.x server side event bus to JavaScript clients• vertx-eventbus.js – publish & register messages
APPLICATION PACKAGING• Maven & Gradle Tooling Support• Packaging• Use maven-shade-plugin to package as FAT Jar• Run the Jar
MESSAGE TYPES – EVENT BUS
• Primitives & their Boxed Types• String• org.vertx.java.core.json.JsonObject• org.vertx.java.core.json.JsonArray• org.vertx.java.core.buffer.Buffer• Custom Type Support – Write your own Serializer
REAL-LIFE EXAMPLE - PULSE
• Marketing Cloud Core Service• REST API for Notifications• Vertx-Web powered Microservice
COMPARISONS• Vert.x Vs Netty• Application Vs Infrastructure• Vert.x provides higher level IO
• Vert.x Vs Jetty• Vert.x Vs NodeJS• Use all available cores
VERT.X VS NODEJS
NodeJS vs Vert.x Performance
RESOURCES• Nginx Microservices Introduction• NodeJS Event Loop• Interview with Tim Fox on Vert.x 3• SockJS