play framework on google app engine - productivity stack

35
smart.biz.pl smart.biz.pl Play with GAE 1 Productivity stack: Play Framework on Google App Engine Polish Java User Group Kraków 2016-04-05 @ marcinstepien www.smart.biz.pl

Upload: marcin-stepien

Post on 13-Apr-2017

2.030 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Play Framework on Google App Engine - Productivity Stack

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

Page 3: Play Framework on Google App Engine - Productivity Stack

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

Page 4: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

PlayFramework

Google AppEngine

4

Page 5: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

PlayFramework

Google AppEngine

5

Productivity

Page 6: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Google Cloud

Compute EngineApp Engine

PaaS IaaS

6

Page 7: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Building blocks

App Engine PaaS

7

Build toolsScalable

Infrastructure

Page 8: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Scaling is simplified

8

One tier of servers

Instances: - capacity- max / min #- manual / autoscale- warm up /

decommission- traffic splitting

Internet

Page 9: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Just a drawing

9

Internet

load balancer frontend servers backed servers data store

You will not deal with that

Page 10: Play Framework on Google App Engine - Productivity Stack

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

Page 12: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Play 1, Play 2, Ninja, Spark … Spring Boot, JHipster

Java micro frameworks

12

Page 13: Play Framework on Google App Engine - Productivity Stack

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

Page 14: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

There is no HttpServletRequest, HttpSession etc.

Not a JEE Framework

14

GAE module wraps Play app into .war file

Page 15: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

There is no server side session

Share Nothing

15

Page 16: Play Framework on Google App Engine - Productivity Stack

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

Page 17: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

hot swapping on dev, just hit F5+

auto scaling on prod

Share Nothing

17

Page 18: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

GAE gives 2 types. Transparent use for Play developer.

Distributed Cache

18

Page 19: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

But not on GAE with Servlet 2.5 spec

Play is asynchronous

19

Page 20: Play Framework on Google App Engine - Productivity Stack

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

Page 21: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Siena module instead of GAE JPA

Data store: Active Record pattern

21

Page 22: Play Framework on Google App Engine - Productivity Stack

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

Page 23: Play Framework on Google App Engine - Productivity Stack

smart.biz.plsmart.biz.pl

Employee adam = new Employee(“Adam”);Adam.dep = department;adam.save();

welcome(adam);

23

Active Record

Page 24: Play Framework on Google App Engine - Productivity Stack

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

Page 25: Play Framework on Google App Engine - Productivity Stack

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

Page 26: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Controller is tied to HTTP protocol

RESTful

26

Page 27: Play Framework on Google App Engine - Productivity Stack

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

Page 28: Play Framework on Google App Engine - Productivity Stack

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

Page 29: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

Data store, Logging, Cache, Gmail, WS calls etc.

GAE Abstraction

29

Is implemented in Play. GAE SDK is hidden.

Page 30: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

GAE tooling

30

BigQuery, Security scans, Performance scans, alerts

.

Page 31: Play Framework on Google App Engine - Productivity Stack

smart.biz.pl

2. Environment (Java 8, Servlet 3.0 +)

Play 2 on GAE ?

31

1. Deployable .war file

Page 33: Play Framework on Google App Engine - Productivity Stack

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

Page 34: Play Framework on Google App Engine - Productivity Stack

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