Building microservices with vert.x 3.0

35
BUILDING MICROSERVICES WITH VERT.X 3.0 AGRAJ MANGAL

Transcript of Building microservices with vert.x 3.0

Page 1: Building microservices with vert.x 3.0

BUILDING MICROSERVICES WITH VERT.X 3.0

AGRAJ MANGAL

Page 2: Building microservices with vert.x 3.0

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

Page 3: Building microservices with vert.x 3.0

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

Page 4: Building microservices with vert.x 3.0
Page 5: Building microservices with vert.x 3.0

MICROSERVICES PATTERN

• Split into Smaller, Interconnected Services• Loose Coupling• Service == Functional Area• Service exposes APIs – consumed by other services & clients

Page 6: Building microservices with vert.x 3.0
Page 7: Building microservices with vert.x 3.0

FINE GRAINED VIEW

Page 8: Building microservices with vert.x 3.0

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 )

Page 9: Building microservices with vert.x 3.0

SCALING MICROSERVICES

Page 10: Building microservices with vert.x 3.0

SERVICES COMMUNICATING

Page 11: Building microservices with vert.x 3.0

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

Page 12: Building microservices with vert.x 3.0

• 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

Page 13: Building microservices with vert.x 3.0

CONCEPTS• Event Loop • Verticles• Server Verticles• Worker Verticles

• Event Bus• Point to Point• Pub/Sub• Distributed

Page 14: Building microservices with vert.x 3.0

THE FAMOUS EVENT LOOP

Page 15: Building microservices with vert.x 3.0

ARCHITECTURE

Page 16: Building microservices with vert.x 3.0

• 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

Page 17: Building microservices with vert.x 3.0

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

Page 18: Building microservices with vert.x 3.0

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

Page 19: Building microservices with vert.x 3.0

VERT.X HTTP SERVER – HELLO WORLD

Page 20: Building microservices with vert.x 3.0

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

Page 21: Building microservices with vert.x 3.0

VERT.X WEB HELLO WORLD

Page 22: Building microservices with vert.x 3.0

ROUTING

Page 23: Building microservices with vert.x 3.0

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

Page 24: Building microservices with vert.x 3.0

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

Page 25: Building microservices with vert.x 3.0

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

Page 26: Building microservices with vert.x 3.0

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

Page 27: Building microservices with vert.x 3.0

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

Page 28: Building microservices with vert.x 3.0

APPLICATION PACKAGING• Maven & Gradle Tooling Support• Packaging• Use maven-shade-plugin to package as FAT Jar• Run the Jar

Page 29: Building microservices with vert.x 3.0
Page 30: Building microservices with vert.x 3.0

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

Page 31: Building microservices with vert.x 3.0

REAL-LIFE EXAMPLE - PULSE

• Marketing Cloud Core Service• REST API for Notifications• Vertx-Web powered Microservice

Page 32: Building microservices with vert.x 3.0
Page 33: Building microservices with vert.x 3.0

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