play framework on google app engine - productivity stack
TRANSCRIPT
smart.biz.plsmart.biz.pl
Play with GAE
1
Productivity stack: Play Framework on Google App EnginePolish Java User GroupKraków 2016-04-05
@marcinstepienwww.smart.biz.pl
smart.biz.plsmart.biz.pl
Marcin Stępień
Developer, [email protected]@marcinstepienlinkedin.com/in/marcinstepien
www.smart.biz.pl
2005
www.whenvi.com
2014
2
smart.biz.plsmart.biz.pl
❖ App Engine intro, scaling & limitations
❖ Play Framework stuff
❖ GAE abstraction
❖ How do this two things work together
❖ Play 2 on GAE?
❖ Trade-offs
3
Play Framework + GAE
smart.biz.pl
PlayFramework
Google AppEngine
5
Productivity
smart.biz.pl
Google Cloud
Compute EngineApp Engine
PaaS IaaS
6
smart.biz.pl
Building blocks
App Engine PaaS
7
Build toolsScalable
Infrastructure
smart.biz.pl
Scaling is simplified
8
One tier of servers
Instances: - capacity- max / min #- manual / autoscale- warm up /
decommission- traffic splitting
Internet
smart.biz.pl
Just a drawing
9
Internet
load balancer frontend servers backed servers data store
You will not deal with that
smart.biz.pl
GAE Limitations
10
Security sandbox
● Java class whitelist
● Java 7
● Servlet API 2.5
○ no real async WS calls
● No raw socket connections
○ no SMTP etc.
○ Url Fetch service instead
Scalability
● Frontend calls are limited to
30s
● Scheduled Jobs limited to
10min
● Url Fetch limited to max 60s
● Do not share object in session
● NoSQL datastore *
● 200 indexes
smart.biz.plsmart.biz.pl
https://cloud.google.com/appengine/docs/the-appengine-environments
11
GAE Environments
smart.biz.pl
Play 1, Play 2, Ninja, Spark … Spring Boot, JHipster
Java micro frameworks
12
smart.biz.pl
Play 1 Play 2
2008 released 2013
Java first service Scala first, Java
Groovy templates Scala
1 GAE module -
by communitymaintenance mode*
developedby authors and community
by 3rd part. commerce supportby authorsand 3rd part.
Play Framework versions
13
smart.biz.pl
There is no HttpServletRequest, HttpSession etc.
Not a JEE Framework
14
GAE module wraps Play app into .war file
smart.biz.pl
There is no server side session
Share Nothing
15
smart.biz.plsmart.biz.pl
#pass value to next request in flash scopeflash.put(“key”, object);
#store in encrypted user cookie, Strings only, 4kbsession.put(“key”, “value”);
#store object in cacheCache.store(“key”, object, “1h”);
16
Share Nothing
smart.biz.pl
hot swapping on dev, just hit F5+
auto scaling on prod
Share Nothing
17
smart.biz.pl
GAE gives 2 types. Transparent use for Play developer.
Distributed Cache
18
smart.biz.pl
But not on GAE with Servlet 2.5 spec
Play is asynchronous
19
smart.biz.plsmart.biz.pl
#com.ning.http.client.AsyncHttpClient
#not on GAE servlet API 2.5Promise<HttpResponse> res1, res2; res1 = WS.url("http://example1").getAsync();res2 = WS.url("http://example2").getAsync();...
#synchronous call works fineWS.url("http://example").get();
#Json parserWS.url("http://example").getJson();
20
WS calls are wrapped in URL fetch
smart.biz.pl
Siena module instead of GAE JPA
Data store: Active Record pattern
21
smart.biz.plsmart.biz.pl
public class Employee extends Model {
@Id public Long id;public String fullName;public Department dep;
}
public class Department extends Model {
@Id public Long id;@Url public String website;public Date launched;
}
22
Data store abstraction
smart.biz.plsmart.biz.pl
Employee adam = new Employee(“Adam”);Adam.dep = department;adam.save();
welcome(adam);
23
Active Record
smart.biz.plsmart.biz.pl
public class Employee extends Model {
@Id public Long id;public String fullName;#no Joins, dep stores only id fieldpublic Department dep;
#retrieves and maps into objectpublic String getDepWebsite() {
#dep.website is null until you call .get()return dep.get().website;
}}
24
Fetching data, no joins
smart.biz.plsmart.biz.pl
public class Employee extends Model {...
public static List<Employee> getAll(Long depId) {return all().filter(“dep”, new Department(depId)).fetch();
}
public static Query<Employee> all() {return all(Employee.class);
}
25
Querying
smart.biz.pl
Controller is tied to HTTP protocol
RESTful
26
smart.biz.plsmart.biz.pl
#http method, app url (regex), controller method
GET /employee/{id} Employees.userPOST /employee/ Employees.createPUT /employee/{id} Employees.updateDELETE /employee/{id} Employees.delete
GET /employee/show-{id} Employees.showGET /employee/list Employees.list
27
Routing
smart.biz.plsmart.biz.pl
#controller methods are static for Play v. < 1.4public class Employees extends App {
public static void create(Employee em) {em.save();show(em.id); #HTTP 302 redirect
}#template is chosen by conventionpublic static void show(Long id) {
#object passed into Employees/show.htmlrender(Employee.get(id));
}
public static void list() {renderJSON(Employee.all());
}}
28
Controller
smart.biz.pl
Data store, Logging, Cache, Gmail, WS calls etc.
GAE Abstraction
29
Is implemented in Play. GAE SDK is hidden.
smart.biz.pl
GAE tooling
30
BigQuery, Security scans, Performance scans, alerts
.
smart.biz.pl
2. Environment (Java 8, Servlet 3.0 +)
Play 2 on GAE ?
31
1. Deployable .war file
smart.biz.plsmart.biz.pl
https://github.com/play2war/play2-war-plugin
32
Play 2 on GAE: war
smart.biz.plsmart.biz.pl
https://cloud.google.com/appengine/docs/flexible/custom-runtimes/
https://cloud.google.com/appengine/docs/flexible/java/dev-jetty9-and-apis
33
Play 2 on GAE: run
Services only via public APIs... Jetty 9 + docker configuration
Flexible runtime Custom runtime
smart.biz.pl
Trade-offs
34
Pros
● Faster deployment
● Scaling
● Google Infrastructure
● maintenance + analytic tools
○ BigQuery etc.
● Abstraction
○ You are not tied with
GAE SDK
Cons
● Sandbox limitations
● Not truly asynchronous on
basic GAE
● Servlet container lowers
application performance
● Watch out for test coverage