+ All Categories
Home > Software > Vert.x for Microservices Architecture

Vert.x for Microservices Architecture

Date post: 12-Apr-2017
Category:
Upload: idan-fridman
View: 149 times
Download: 7 times
Share this document with a friend
49
Vert.x for Microservices Architecture Idan Fridman [email protected] www.idanfridman.com
Transcript
Page 1: Vert.x for Microservices Architecture

Vert.x for Microservices Architecture

Idan Fridman

[email protected]

www.idanfridman.com

Page 2: Vert.x for Microservices Architecture

Agenda• The C10k problem

• Understanding the Reactor pattern

• The good (and the bad) of Vert.x and when we should use it.

• Vertx and Websockets

• RxJava And Vertx

• Vert.x into production

Page 3: Vert.x for Microservices Architecture

Traffic, Traffic Traffic...!Lots of machines

http://www.internetlivestats.com/one-second/

Page 4: Vert.x for Microservices Architecture

One machine scenario

Simple use case: Response Request

Datasource

Page 5: Vert.x for Microservices Architecture

Conventional Technology Stack

Page 6: Vert.x for Microservices Architecture

Current Setup Opening a new connection for every single request/response pair

Default HTTP connector is blocking and follows a one thread per connection model

to serve 1000 concurrent users, it requires 1000 active threads

Page 7: Vert.x for Microservices Architecture

C10K ProblemPrevented servers from handling more than 10,000 concurrent connections.

The Apache problem is the more connections the worse the performance.

Let’s assume we execute 1000 “quick” transactions

then you’ll only have about a 1000 concurrent connections to the server

Change the length of each transaction to 10 seconds

now at 1000 transactions/sec - you’ll have 10K connections open

Page 8: Vert.x for Microservices Architecture

“Why like this?” Webserver gets a connection

it starts a worker/thread

1. Memory limit

Each Thread takes min memory(default 1024k == 1mb)

2. Operating system limit

The operating system by default can't open 10k threads.

10 concurrent requests==10k threads==10k * 1MB==10GB

Page 9: Vert.x for Microservices Architecture

Reactor pattern

From wiki:

The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs.

The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers.

Page 10: Vert.x for Microservices Architecture

Reactor pattern(Revisited)

We already understand the The disadvantage of using a separate thread for each event listener

The reactor pattern is one implementation technique of event-driven architecture

it uses a single threaded event loop blocking on resource-emitting events and dispatches them to corresponding handlers and callbacks.

Page 11: Vert.x for Microservices Architecture

Reactor pattern modelReactor

A Reactor runs in a separate thread, and its job is to react to IO events by dispatching the work to the appropriate handler.

It’s like a telephone operator in a company who answers calls from clients and transfers the line to the appropriate contact.

Page 12: Vert.x for Microservices Architecture

Reactor pattern model

Handlers

A Handler performs the actual work to be done with an I/O event, similar to the actual officer in the company the client wants to speak to.

A reactor responds to I/O events by dispatching the appropriate handler. Handlers perform non-blocking actions.

Page 13: Vert.x for Microservices Architecture

Event-LoopStandard reactor implementation:

single event loop thread which runs around in a loop delivering all events to all handlers as they arrive.

Page 14: Vert.x for Microservices Architecture

Who implementing this?

Page 15: Vert.x for Microservices Architecture

Don’t call us, we’ll call you

Main thread should works very quickly

No long jobs in the loop

Schedule a call asynchronously

Operations in event loop should just schedule all asynchronous operations with callbacks and go to next request without awaiting any results.

Page 16: Vert.x for Microservices Architecture

We love the JVM - Let’s have vert.x1. Microservices toolkit for the JVM

2. Asynchronous

3. Scalable

4. Concurrent services development model

5. Polyglot language development with first class support for JavaScript, Ruby, Groovy, Scala, and of course Java.

Vertx is a toolkit not a framework or/container

(means you can use it within your existing application to give it the Vert.x super powers)

For example embedding it inside Spring

Page 17: Vert.x for Microservices Architecture

Vertx & Friends

Page 18: Vert.x for Microservices Architecture

Vertx main server types

Httpserver

Websockets server

TCP server

Page 19: Vert.x for Microservices Architecture

Easy as that

Page 20: Vert.x for Microservices Architecture

Building Vertx with VerticlesVerticle is the building blocks of Vert.X which reminds an Actor-like approach for concurrency model and avoiding mutable shared data

Each Verticle can communicate with another using EventBus(explain later)

Verticle types:

Standard Verticles

Worker Verticles

Page 21: Vert.x for Microservices Architecture

Verticle Types

Standard verticlesAssigned an event loop thread when they are created

All the code in your verticle instance is always executed on the same event loop

You can write all the code in your application as single threaded and let Vert.x worrying about the threading and scaling.

Page 22: Vert.x for Microservices Architecture

How to run blocking-code anyway?

Worker verticlesExecuted not using an event loop, but using a thread from the Vert.x worker thread pool.

Worker verticles are designed for calling blocking code, as they won’t block any event loops.

*Unlike Nodejs where you need to use promises with vertx you can handle blocking-code out of the box

Page 23: Vert.x for Microservices Architecture

The Event Bus

Eventbus is build-in tunnel which providing a messaging mechanism to all Vertx components.

Different parts of your application able communicate with each other irrespective of what language they are written in.

The event bus supports publish/subscribe, point to point, and request-response messaging.

Eventbus features:

registering handlers

unregistering handlers and

sending messages

publishing messages.

Page 24: Vert.x for Microservices Architecture

Eventbus - Event Driven built-In

Eventbus will be help you to scale up your Vert.x Cluster.

Page 25: Vert.x for Microservices Architecture

Eventbus and the Microservices way

Page 26: Vert.x for Microservices Architecture

ClusteringDiscovery and group membership of Vert.x nodes in a cluster

Maintaining cluster with topic subscriber lists (so we know which nodes are interested in which event bus addresses)

Distributed Map support

Distributed Locks

EventBus can be clustered:

When clustering different vert.x instances on the network they going to use the same single distributed

eventbus

Page 27: Vert.x for Microservices Architecture

Hazelcast

Vertx can use different cluster-managers. The default one is Hazelcast

Hazelcast is in-memory operational platform

Easy Discovery service

Page 28: Vert.x for Microservices Architecture

Adding JWT capabilities

Security is very important within microservices.

Vert.x Has Built-in JWT support.

You can issue JWT tokens and validate them within your Vert.x Endpoints

Page 29: Vert.x for Microservices Architecture

Websockets

Web technology that allows a full duplex socket-like connection between HTTP

servers and HTTP clients (typically browsers).

Once established:

“Push” messages

Data frames can be sent back and forth between the client and the server in full-

duplex mode

Native to Browsers

Page 30: Vert.x for Microservices Architecture

WebSocket connection remains open so there is no need to send another request to the server

Page 31: Vert.x for Microservices Architecture

Websockets and Vertx

Vertx enable out of the box support using Websockets.

Vert.x supports WebSockets on both the client and server-side.

Page 32: Vert.x for Microservices Architecture

Websockets Handler

server.websocketHandler(websocket -> { System.out.println("WebSocket is On!");});

Handler will be called when connection established:

Page 33: Vert.x for Microservices Architecture

SockJS

Client side JavaScript library and protocol which provides a simple WebSocket-like interface

Make connections to SockJS servers irrespective of whether the actual browser or network will allow real

WebSockets.

Supporting various different transports between browser and server, and choosing one at run-time according

to browser and network capabilities.

Transparent

Page 34: Vert.x for Microservices Architecture

Eventbus using sockJS bridgeClient side allows you to send and publish messages to the event bus and register handlers to receive messages

Router router = Router.router(vertx);

SockJSHandler sockJSHandler = SockJSHandler.create(vertx);BridgeOptions options = new BridgeOptions();sockJSHandler.bridge(options);

router.route("/eventbus/*").handler(sockJSHandler);

Page 35: Vert.x for Microservices Architecture

Reactive And VertxNatural couple

Page 36: Vert.x for Microservices Architecture

Open many workers in eventloop? bad

Open too many workers won't makes you any different

Work asynchronously

Who said callback (hell)?

Vertx Supports the popular RxJava library that allows you to write observers that react to sequences of events.

Page 37: Vert.x for Microservices Architecture

Scenario Example

1. Client calls to our app’s web service ->

2. our webservice need to request multiple micro-services->

3. uses a callbacks interface to pass the successful result to the next web service call

4. define another success callback- >

5. and then moves on to the next web service request.

Orchestrator

A B C F

Client Request

Page 38: Vert.x for Microservices Architecture

Looks like that->

Also known as:“The Callback Hell”

Page 39: Vert.x for Microservices Architecture

//The "Nested Callbacks" Way

public void fetchUserDetails() { //first, request the users...

mService.requestUsers(new Callback<GithubUsersResponse>() { @Override

public void success(final GithubUsersResponse githubUsersResponse, final Response response) {

Timber.i(TAG, "Request Users request completed"); final List<GithubUserDetail> githubUserDetails = new ArrayList<GithubUserDetail>();

//next, loop over each item in the response

for (GithubUserDetail githubUserDetail : githubUsersResponse) { //request a detail object for that user

mService.requestUserDetails(githubUserDetail.mLogin, new Callback<GithubUserDetail>() {

@Override public void success(GithubUserDetail githubUserDetail,

Response response) { Log.i("User Detail request completed for user : " + githubUserDetail.mLogin);

githubUserDetails.add(githubUserDetail); if (githubUserDetails.size() == githubUsersResponse.mGithubUsers.size()) {

//we've downloaded'em all - notify all who are interested!

mBus.post(new UserDetailsLoadedCompleteEvent(githubUserDetails)); }

}

@Override public void failure(RetrofitError error) {

Log.e(TAG, "Request User Detail Failed!!!!", error); } });

} }

Page 40: Vert.x for Microservices Architecture

Async our microservices call(Using Reactor)

A library for composing asynchronous and event-based programs by using observable sequences.

● Allow you to compose sequences together declaratively ● Abstracting away :

o low-level threadingo synchronizationo thread-safetyo concurrent data structureso non-blocking I/O.

Page 41: Vert.x for Microservices Architecture

RxJava for the rescue● RxJava is single jar lightweight library.

● Using the Observable abstraction and related higher-order functions. (Support Java6+)

The following external libraries can work with RxJava:

● Camel RX provides an easy way to reuse any of the

Apache Camel components, protocols, transports and data formats with the RxJava API

● rxjava-http-tail allows you to follow logs over HTTP, like tail -f

● mod-rxvertx - Extension for VertX that provides support for Reactive Extensions (RX) using the RxJava library

Page 42: Vert.x for Microservices Architecture

Zipping Observables (Without blocking)

public Observable<Boolean> registerRequest(..) { return Observable.zip( createNewRoomNode(), createNewWaveNode(), logRecordOnMysql(), sendWelcomeMessage() , (r1, r2, r3, r4) -> r1 && r2 && r3 && r4); }

private Observable<Boolean> createNewRoomNode(..) { ... return } private Observable<Boolean> createNewWaveNode(..) { ... return } private Observable<Boolean> logRecordOnMysql(..) { ... return }

Page 43: Vert.x for Microservices Architecture

Vertx going to production(Deployment)

Vertx can be deployed to AWS with Hazelcast

Discovery is taking placing using the same security groups

Page 44: Vert.x for Microservices Architecture

Vertx going to production

Enterprise ready solution which can be deployed on amazon

Combining powerful technology with complete production solution

Empower Spring configurations, env’s, defaults, libs with vertx technology

Page 45: Vert.x for Microservices Architecture

Spring Into Vertx(Benefits)

DI

Have a deployment ready container

Configurations setup

Endpoints and JMX ready

Spring Data and more

Page 46: Vert.x for Microservices Architecture

Practical implementation Spring into vert.x

● On starter Verticle initiate Spring context

● Pass Spring context to each verticle as param

● Gotchas: Make sure you always share the same spring context

Page 47: Vert.x for Microservices Architecture

Vertx Into Spring

Leverage Spring-MVC while expose controllers endpoints

Send Operational events via Eventbus(e.g send commands to connected endpoints, websockets, etc..

Deploy Verticles inside Spring

Page 48: Vert.x for Microservices Architecture

My Vert.X Case-Study

Page 49: Vert.x for Microservices Architecture

Thank you:)

Idan Fridman

[email protected]

www.idanfridman.com


Recommended