ratpack - the core for your microservices for jdd 2015
TRANSCRIPT
01
About me02
Andrey AdamovichBio:
Developer/Architect
"DevOps" guy
Coach
Speaker
Author
Company: Aestas/IT (http://aestasit.com)
••••••
•
03
Contact detailsEmail: [email protected]
Linkedin: http://www.linkedin.com/in/andreyadamovich
Lanyrd: http://lanyrd.com/profile/andreyadamovich
GitHub: https://github.com/aadamovich
SO: http://stackoverflow.com/users/162792/andreyadamovich
Twitter: @codingandrey, @aestasit
••••••
04
Let's start!05
MicroservicesIn short, the microservice architectural style is an approach to
developing a single application as a suite of small services, each
running in its own process and communicating with lightweight
mechanisms, often an HTTP resource API.
James Lewis & Martin Fowler
“06
Quick Start!07
0. PrerequisitesInstall Java 8+
Install Groovy 2.4+
(Optionally) install Gradle 2+ (or just use Gradle Wrapper)
•••
08
1. Type in...@Grab("io.ratpack:ratpack‐groovy:1.0.0")
@Grab("com.fasterxml.jackson.core:jackson‐annotations:2.6.2")
import static ratpack.groovy.Groovy.ratpack
01.
02.
03.
09
1. Continue...ratpack {
handlers {
get {
response.send "Time is on JDD " +
new Date().toString()
}
}
}
01.
02.
03.
04.
05.
06.
07.
08.
10
2. Save as...ratpack.groovy01.
11
3. Start!groovy ratpack.groovy 01.
12
4. Enable some more loggingJAVA_OPTS=‐Dgroovy.grape.report.downloads=true01.
13
Ratpack facts
14
Ratpack factsRatpack is a toolset that combines several Java libraries that allows
efficiently developing performant and testable HTTP applications.
15
Ratpack factsInspired by Sinatra framework
Requires Java 8
Does not require a EE container
Does not implement Servlet API
Goes under Apache 2.0 License
•••••
16
Ratpack factsCore is very minimal and is only based on few abstractions (Handler
and Registry)
Many additional modules exist and it's easily to develop new ones
Modules are injected through DI (there is no specialized plugin
system)
Outofthebox integration with Guice and Spring
•
••
•
17
Stack
18
It's alive andvery active!19
Release history0.5.2 Jul 21, 2012
0.6.1 Nov 29, 2012
0.9.0 Jan 02, 2014
0.9.1 Feb 01, 2014
0.9.2 Mar 01, 2014
0.9.3 Apr 01, 2014
0.9.4 May 01, 2014
0.9.5 Jun 01, 2014
0.9.6 Jul 01, 2014
••••••••• 20
Release history0.9.7 Aug 01, 2014
0.9.8 Sep 01, 2014
0.9.9 Oct 01, 2014
0.9.10 Nov 02, 2014
0.9.11 Dec 01, 2014
0.9.12 Jan 01, 2014
0.9.13 Feb 01, 2015
0.9.14 Mar 01, 2015
••••••••
21
Release history0.9.15 Apr 01, 2015
0.9.16 May 02, 2015
0.9.17 June 02, 2015
0.9.18 July 02, 2015
0.9.19 Aug 02, 2015
1.0.0 Sep 15, 2015
••••••
22
Commit history
23
Statistics
24
Team
25
Top contributor
26
Modules ICommon: config, session
Reactive: rx, remote
Authentication: pac4j
Build/Packaging: gradle
Database: h2, hikari
•••••
27
Modules IIDependency Injection: guice, springboot
JSON: jackson
Language support: groovy, kotlin
Reliability: hystrix, dropwizardmetrics, newrelic
Templates: handlebars, thymeleaf, groovy
Testing: test, groovytest
••••••
28
Java + Groovy = ?Has similar performance to Java when using invokeDynamic
Supports static compilation and compiletime type checking
Useful for defining rich DSLs with type checking via Closure
parameters and @DelegatesTo annotations
•••
29
IDE supportIntelliJ IDEA recommended
Eclipse has poor support for Groovy and @DelegatesTo
NetBeans haven't even tried
•••
30
Diving deeper31
HandlersAll request processing is done via composition of Handler s.
Each Handler in the Chain is asked to respond to a Request until
one actually does.
••
32
A handler canSend a Response based on the Request .
Delegate to the next Handler in the Chain .
Insert Handler s into the Chain and immediately delegate to them.
Change Context , which represents the current state of the
Request processing.
••••
33
Flow
34
Let's writesome code!35
Demo: Dateserver
36
Paths and parametersprefix('api') {
get('user/:id') {
render getUser(pathTokens.id)
}
get('friends') {
render getFriendList()
}
}
01.
02.
03.
04.
05.
06.
07.
08.
37
Verbspath('user') {
byMethod {
get { ... }
post { ... }
put { ... }
delete { ... }
}
}
01.
02.
03.
04.
05.
06.
07.
08.
38
Content typespath('user') {
byContent {
json { ... }
xml { ... }
type("application/vnd.app.org+json;v=1") {
...
}
}
}
01.
02.
03.
04.
05.
06.
07.
08.
09. 39
Static contentfiles {
dir "public"
file "index.html"
}
01.
02.
03.
04.
40
Templates (ala JSP/GSP)Handler code:
get ("admin") {
render groovyTemplate('admin.html', result: "")
}
01.
02.
03.
41
Templates (ala JSP/GSP)admin.html :
<% if (model?.result) { %>
<% model.result.each { %>
<li>${it}</li>
<% } %>
<% } %>
01.
02.
03.
04.
05.
42
Templates (Groovyway)Handler code:
render groovyMarkupTemplate(
"update.gtpl",
"title: "Update Book",
...
"price": book.price
)
01.
02.
03.
04.
05.
06.
43
Templates (Groovyway)update.gtpl :
layout 'layout.gtpl',
title: title,
msg: msg,
bodyContents: contents {
h1('Update Book')
includeGroovy '_book_form.gtpl'
}
01.
02.
03.
04.
05.
06.
07.
44
TestingTest handler implementations with RequestFixture
Functional testing with ApplicationUnderTest and
TestHttpClient
Nobody canceled testing with Geb (Selenium)!
••
•
45
Demo: GebTest
46
Demo:Lazybones +
IDEA47
Let's buildservices!
48
Service setup
49
Quick startlazybones create ratpack <project>
gradlew idea
gradlew run ‐‐continuous
•••
50
Demo:Services
51
Operational aspectsCan't even make WAR! Very peaceful!
Publish JAR/TAR/ZIP in to artifact repository
Or use ospackage plugin to create DEB/RPM package
Integrate with provisioning/configuration management tools
Consider monitoring/metrics
•••••
52
Puppet: defined typedefine service::booking(
$deployment_port,
$deployment_host,
$enabled = true,
$development_mode = false,
$revision = latest,
) {
...
}
01.
02.
03.
04.
05.
06.
07.
08.
09. 53
Puppet: fetchfile { "/services/booking/environments/${name}":
ensure => directory
}
vcsrepo { "/services/booking/environments/${name}":
ensure => $revision,
provider => git,
source => 'https://github.com/jdd/booking.git',
notify => Exec["build booking ${name} api"]
}
01.
02.
03.
04.
05.
06.
07.
08.
09. 54
Puppet: autobuildexec { "build garagetravel ${name} api":
cwd => "/services/booking/environments/${name}",
refreshonly => true,
command =>
"/services/booking/../${name}/gradlew installApp",
notify => Service["booking_${name}"]
}
01.
02.
03.
04.
05.
06.
07.
55
Puppet: servicefile { "booking ${name} service":
path => "/etc/init/booking_${name}.conf",
content => template('booking/service.conf.erb'),
notify => Service["booking_${name}"]
}
01.
02.
03.
04.
05.
56
Puppet: serviceservice { "booking_${name}":
ensure => $enabled ?
{ true => "running",
default => "stopped" },
enable => $enabled,
provider => upstart,
}
01.
02.
03.
04.
05.
06.
07.
57
Combining optionsChef/Ansible
Etcd/Consul/Eureka
Docker
•••
58
MonitoringHystrix
Dropwizard Metrics••
59
What's next?
60
Going further
61
Demo:ExamplesBooks
62
Summary63
TakeawaysRatpack can be used to quickly prototype web APIs and applications.
Learning curve is really small, you can start in seconds.
It can be used to create high performance web applications due to
nonblocking architecture.
Ratpack does not lock you in the way you implement data access,
session handling, logging, etc.
Ratpack has vibrant community and actively evolving code base.
•••
•
•
64
Reading materialhttp://ratpack.io
http://www.slideshare.net/search/slideshow?q=ratpack
https://github.com/ratpack
http://alvarosanchez.github.io/ratpack101/
••••
65
Book
66
Contribute!67
Questions?68
Demo codehttps://github.com/aestasit/talks2015jddratpackthecoreforyour
microservicessetup•
69
Thank you!70
Happycoding!
71