ratpack - springone2gx 2015

56
SPRINGONE2GX WASHINGTON, DC Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Ratpack Web Framework By Dan Woods @danveloper

Upload: daniel-woods

Post on 16-Apr-2017

2.223 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Ratpack - SpringOne2GX 2015

SPRINGONE2GXWASHINGTON, DC

Unless otherwise ind icated, these sl ides are © 2013-2015 Pivotal Sof tware, Inc. and l icensed under a Creat ive Commons Att r ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Ratpack Web FrameworkBy Dan Woods

@danveloper

Page 2: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 2

About Me• Member of the Ratpack Web core team• Author of “Learning Ratpack”, O’Reilly• http://shop.oreilly.com/product/0636920037545.do• Senior Engineer, The Groundwork• https://github.com/danveloper

1st Four Chapters Available Today!

Page 3: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 3

Ratpack Team on TwitterFollow this crew!

• @ldaley• @varzof• @rus_hart• @beckje01• @marcinerdmann• @zedar185• @Lspacewalker• @johnrengelman

Page 4: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 4

Brief History of Ratpack• Ratpack started out as a Groovy DSL implementation example (2010)• Evolved steadily into a JVM Sinatra clone (2010-2011)• Dumped Sinatra legacy and started focusing on NIO/performance

(2012)• Under steady development in its modern form for 3 full years!

Groovy DSL Implem

entation

JVM Sinatra Clone

Drop “Sinatra”, NIO/Perf

Drops Servlets for Vert.x

Core rewritten in JavaM

oves to org.ratpack packages

Execution Model Introduced

Bootstraping, Config

Registry is introducedM

ove from Vert.x to Netty

Stable, Production Ready – 1.0

Page 5: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 5

Overview of Ratpack• Ratpack is now at version 1.0.0• Production-ready, API-stable• Can safely adopt without fear of breaking API changes• Great option for building microservices• Can also be integrated into legacy apps through its robust test

fixtures

Page 6: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 6

Overview of Ratpack• A high performance web framework• Built on a non-blocking network stack• Provides low-level constructs for working with async APIs• Core is written in Java (requires Java 8)• Integrated support for building Groovy-based applications• Emphasis on developer productivity• As few opinions as possible• Fast, self-contained, light-weight deployables

Page 7: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 7

Simplest Hello World Example• Just a few lines of Groovy code required!

Page 8: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 8

Simplest Hello World Example• Totally Tweetable! https://twitter.com/danveloper/status/

608298173208100864

Page 9: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 9

Java Hello World Example• Still, not very much needed at all…

Page 10: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 10

Overview of Ratpack• Support for HTML templates with Groovy and Handlebars

Page 11: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 11

Overview of Ratpack• Great for microservices!• First-class support for language agnostic transport

Page 12: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 12

Overview of Ratpack• Non-blocking HTTP client

Page 13: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 13

Overview of Ratpack• Fault tolerance for building distributed systems

Page 14: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 14

Overview of Ratpack• Support for metric reporting with Dropwizard Metrics

Page 15: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 15

Overview of Ratpack• Comprehensive configuration model

Page 16: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 16

Overview of Ratpack• Support for persistent sessions

Page 17: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 17

Overview of Ratpack• Robust security with Pac4j

Page 18: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 18

Overview of Ratpack• Out of the box SSL support

Page 19: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 19

Performance• Like most frameworks, Ratpack is CPU-bound (the more CPUs, the

better)• Unlike most frameworks, Ratpack efficiently processes on each CPU

https://gist.github.com/danveloper/db888be3519966976368

Page 20: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 20

Execution Model• The execution model in Ratpack is borne from the fact that the JVM

has no inherent support for Continuations• Asynchronous programming is difficult• Async processing introduces non-determinism• Web applications require deterministic processing

Page 21: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 21

Execution Model• Ratpack wants to make Async processing and programming a reliable

and usable model for the JVM• By fitting into the execution model, applications garner confidence

from a deterministic processing flow

Page 22: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 22

TPR/TPC Processing Model• How most JVM web frameworks work

Internet Web App

new Thread().start()BlockingProcessin

g

Process and wait

Page 23: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 23

TPR/TPC Processing Model• In the thread-per-request (or thread-per-connection) model, you are

limited by the amount of threads that you can create• Bringing the data from a request-taking thread and placing it into a

processing thread incurs a context switch• The processing thread is where all your work is done, and is

established until you respond• This is not performant at all, but it is an easy model to program in,

and that’s why people use it

Page 24: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 24

Ratpack Processing Model• How Ratpack works (4 CPU example)

Internet Event Loop

Event LoopEvent LoopEvent Loop

HandlerWaiting for something? (db call, call to remote)

Return thread to request-taking pool while we wait

Respond

One thread per CPU core

Page 25: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 25

Ratpack Processing Model• Only establishes a single thread per CPU core• Processing doesn’t block the thread (if it needs to, then we can

schedule it to the blocking thread pool)• While we wait for async responses to be fulfilled, the thread is able to

process other requests• When we get the async response, processing continues on the same

thread• No context switching in computation handlers, async processing

responds when its work is done

Page 26: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 26

Problems with Async Programming• Callbacks are obnoxious to work with• No good way to detect when waiting for async responses• Represents a temporal disconnect from the processing flow• Most async web frameworks start a timer when a request comes in,

and if you don’t respond by the time that’s over, then it assumes you’re not going to

Page 27: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 27

Problems with Async Programming

Callback HELL

Page 28: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 28

Problems with Async ProgrammingWith Ratpack Promises, you get guaranteed execution order without the need scoped callbacks.

<1> - definitely happens 1st

<2> - definitely happens 2nd

<3> - definitely happens 3rd

<4> - definitely happens 4th

<5> - definitely happens 5th

Page 29: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 29

Problems with Async Programming

In other async web frameworks:The clock is ticking for you to get that response out before the framework shuts you down

Page 30: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 30

Problems with Async ProgrammingExecution|__stream |__marker |__flatMap(..) |__findByUsername|__stream |__marker |__flatMap(..) |__loadProfile|__stream |__marker |__flatMap(..) |__loadFriends|__stream |__marker |__flatMap(..) |__loadFriendPhotos|__stream |__marker |__render(..) |__then(..)

Page 31: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 31

Execution Model• The overall execution is a parallel to the construct of a

continuation• Each of the calls to a Promise represent a frame in the execution• Each of the Promise types represents a new processing stream in the

execution• While async processing is taking place (ie. Promise is not fulfilled), the

execution is suspended• When the execution is suspended, the processing thread is given back

to the event loop to continue processing requests• When an async call returns, the execution is resumed

Page 32: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 32

Execution Model• Most importantly, we know that when we hit the marker on a

processing stream that the stream is done computing• The processing streams are supervised by Ratpack to know that a

request is still processing• If your code doesn’t respond to a request, we don’t need to wait until

the request times out to inform the client• If all the streams are done computing but no response has been sent,

we are aware of that and can send an appropriate error back (no response sent) to the client

Page 33: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 33

Execution Model• Ratpack’s execution model gives you much higher confidence in

working with async programming• The Promise type makes async programming easier• Deterministic async processing gives added benefits for concurrency• You have better confidence when programming to async APIs with

Ratpack• Fun fact: any API can be made asynchronous in Ratpack

Page 34: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 34

Blocking Done Right• To make use of most 3rd party Java libraries, blocking will need to take

place at some point• A prime example here is using JDBC• Since you can’t block on the request-taking thread, you will need to

do blocking on a separate thread• Any non-trivial application will need to do blocking at some point…• Ratpack provides easy fixtures for Promise types to be scheduled for

computation or blocking

Page 35: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 35

Blocking Done Right

Page 36: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 36

Dependency Injection• Ratpack is not tied to any particular dependency injection framework• DI is an abstract concept in Ratpack, components are registered via a

Registry• Ratpack apps can be built entirely without any DI• Framework modularity is accomplished via Guice• 1st class support for Guice and Spring Boot

Page 37: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 37

Registry• There are multiple registries in Ratpack• Provide components at different layers of the framework

Server Registry

User Registry

Context Registry

Request Registry

Page 38: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 38

Registry• Components can be bound to the user registry at start time

Page 39: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 39

Registry• Registries can be built and cascaded at request time, and according to

request attributes

Page 40: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 40

Registry• Registries can be backed by Guice…

Page 41: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 41

Registry• … or by Spring Boot…

Page 42: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 42

Registry• … or both …

Page 43: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 43

Dependency Injection• In Groovy, dependency injection can be accomplished by simply

specifying the type as a closure argument

Page 44: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 44

Dependency Injection• Taking a completely unopinionated approach to dependency injection

allows implementations to be flexible• Can support nearly every component-providing backend• Gives you the ability to leverage the best parts of all available

ecosystems• New Ratpack apps can readily fit into legacy infrastructures that do

have opinions about this stuff

Page 45: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 45

Build System Support• Ratpack is just a set of libraries, so any Java build system can build

your project• Advanced integration with Gradle is provided through the ratpack-

gradle plugin• Using Gradle is the easiest way to get started• Gradle plugin provides version-proper dependency resolution, so you

don’t need to update versions as you upgrade your project

Page 46: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 46

Build System Support• Just a set of libraries…

Page 47: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 47

Build System Support• Advanced integration with Gradle…

Page 48: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 48

Build System Support• Ratpack apps can be easily packaged for deployment with Gradle• Multiple options: fat jar, tarball, zip file• Gradle plugin applies the application plugin, so distributions and

dependencies can easily be built and shipped with os start scripts• Fat JAR building is accomplished via the ShadowJar plugin

Page 49: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 49

Developer Productivity• Development time hot reloading is provided via Gradle’s “continuous

build” mode• Standalone Ratpack Groovy scripts are able to be reloaded when the

development serverConfig option is true

Page 50: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 50

The Handler Chain• Denotes the edge of your application• Requests flow through the chain until reaching a handler than can

provide a response• Two types of handlers: request flow and terminal• Request Flow handlers manage the direction the request takes

through the chain• Terminal handlers process a request and respond to it

Page 51: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 51

The Handler Chain• Request flow handler

Page 52: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 52

The Handler Chain• Terminal handler

Page 53: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 53

The Handler Chain• Chain API provides semantic methods to bind a handler to an HTTP

verb and optionally a path• get(..), post(..), put(..), patch(..), delete(..) methods will bind

accordingly• Can also bind a handler that will be called for every incoming request

with the all(..) and path(..) chain methods

Page 54: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 54

Live Coding Demo Time

Page 55: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 55

Questions?

Page 56: Ratpack - SpringOne2GX 2015

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/l icenses/by-nc/3.0/ 56

Join the Community

http://slack-signup.ratpack.io/