sgce 2015 rest apis
TRANSCRIPT
![Page 1: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/1.jpg)
Enlighten your software
REST API design & dev
Domingo Suárez Torres @domix http://github.com/domix http://domingosuarez.com
Una propuesta de: “Como construir APIs REST para sistemas distribuidos con alta
escalabilidad y resilencia"
1/jul/2015
![Page 2: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/2.jpg)
Agenda• Preamble
• Disclaimer
• Context
• Motivation
• API
• Contract
• Programming model (architecture style)
![Page 3: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/3.jpg)
Preamble
![Page 4: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/4.jpg)
Disclaimer• This talk and its contents are based in my own
experience.
• I’m not trying to say all the following IS the way to do the right thing. Just my opinion. :)
• All I want is to share my experience with the community.
• This talk is huge. Hope I can finish on time.
![Page 5: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/5.jpg)
Context
• I’m not covering Hypermedia REST APIs
• I’m a JVM guy, so you will see lots of JVM references. Sorry.
![Page 6: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/6.jpg)
Motivation• Functional requirements is THE challenge.
• API Documentation is always a PITA, keep sync with the maintenance, new features, fixes, etc.
• Build any API (REST, SOAP, RPC) is hard.
• Development tools choice (programming language, libraries, frameworks, runtime, etc).
• Non functional requirements, quality attributes.
![Page 7: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/7.jpg)
SOAPNo longer an acronym. Since 1.2
![Page 8: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/8.jpg)
SOAP
• Characteristics
• Extensibility (security, routing)
• Neutrality (transport protocol)
• Independence (programming model)
![Page 9: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/9.jpg)
SOAP architecture• Several layers of specifications for:
• Message format
• Message Exchange Patterns
• Underlying transport protocol bindings
• Message processing models
• Protocol extensibility
![Page 10: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/10.jpg)
SOAP
• Web Services Description Language (WSDL)
• Universal Description Discovery and Integration (UDDI)
![Page 11: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/11.jpg)
SOAP
• Complex specification for vendors
• Developers take time to grok
• Sometimes very long time
• Misunderstanding of the guidelines.
![Page 12: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/12.jpg)
I like SOAP intentions
![Page 13: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/13.jpg)
I dislike SOAP complexity
![Page 14: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/14.jpg)
API Contract
![Page 15: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/15.jpg)
API Contract• Always is over there. Implicit/Explicit
• You should have one.
• You should know it.
• No matter if you build it or you consume the API.
• You should give it so much love.
• Learn to love it.
![Page 16: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/16.jpg)
API contract approaches
• Contract last
• Code driven contract
• Contract first
• Upfront design
![Page 17: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/17.jpg)
Contract last• Sadly is the commonest.
• Server-side developers dictate the contract.
• Most of the time with only one perspective.
• Implementator perspective VS consumer perpective
• Flaky. If missing test cases. Fragile.
• The documentation is done at the end.
• Bottleneck.
![Page 18: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/18.jpg)
Contract first• Upfront design API
• Consumer perspective design
• Reusability
• Versioning
• Performance
![Page 19: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/19.jpg)
The contract as corner stone for REST APIs
![Page 20: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/20.jpg)
How to build the contract?
![Page 21: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/21.jpg)
Tools
![Page 22: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/22.jpg)
![Page 23: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/23.jpg)
![Page 24: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/24.jpg)
![Page 25: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/25.jpg)
So many others :)
![Page 26: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/26.jpg)
RAML, my favorite• YAML dialect + JSON schema #ftw (types)
• Readable for humans.
• Can be procesable by machines.
• Design clear, correct, precise & consistent APIs.
• No vendor lock-in.
![Page 27: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/27.jpg)
![Page 28: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/28.jpg)
![Page 29: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/29.jpg)
![Page 30: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/30.jpg)
![Page 31: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/31.jpg)
Design & build
![Page 32: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/32.jpg)
![Page 33: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/33.jpg)
![Page 34: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/34.jpg)
![Page 35: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/35.jpg)
![Page 36: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/36.jpg)
raml + raml-mockupWe can deliver an API in days or hours
![Page 37: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/37.jpg)
RAML & code generation• Server side
• JAX-RS
• Client
• Square Retrofit
• Documentation
• HTML
![Page 38: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/38.jpg)
raml2code• OpenSource project from Grupo Expansión
• Generates Plain Old Java/Groovy Objects
• Generates JAX-RS interfases
• Generates an API client with Retrofit
• Can run in Android also in any JVM application.
![Page 39: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/39.jpg)
![Page 40: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/40.jpg)
![Page 41: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/41.jpg)
Nice, now I know how to create a contract. What’s next?
![Page 42: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/42.jpg)
Programming modelArchitecture
![Page 43: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/43.jpg)
SOA + EDA
![Page 44: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/44.jpg)
Services• Build, deploy, and monitor any kind of services in
agile, efficient way with open standards.
• Deployment on-premise, in the cloud, mix of both.
• Deploy services independently from each other.
• Decoupled & scale linearly across commodity hardware.
![Page 45: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/45.jpg)
Wait, a buzzword is coming…
![Page 46: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/46.jpg)
MicroServices architecture• Service Contracts
• RAML
• Exposing new & existing services
• Enterprise Integration Patterns (integration, routing, transformation)
• Discovery of services
• Service Registry
![Page 47: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/47.jpg)
MicroServices architecture• Coordination across services
• Event Bus, (smart service, dump pipe)
• Managing complex deployments and their scalability
• Build Tool, CI, DevOps (Chef, Puppet), Linux Containers, Cloud, monitoring
• Visibility and correlation across services
• Event correlation, ElasticSearch, Logstash, Kibana.
![Page 48: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/48.jpg)
Sounds nice, but…
![Page 49: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/49.jpg)
![Page 50: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/50.jpg)
Implementation details
![Page 51: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/51.jpg)
Spring Boot is awesome
![Page 52: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/52.jpg)
–Spring Boot reference documentation
“Spring Boot makes it easy to create stand-alone, production-grade Spring based
Applications that you can “just run”. We take an opinionated view of the Spring platform and
third-party libraries so you can get started with minimum fuss. Most Spring Boot applications
need very little Spring configuration.”
![Page 53: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/53.jpg)
Spring Boot• Embedded Servlet container
• Tomcat
• Jetty
• Undertow
• Executable jar file. Key feature for microservices!
• Monitoring capabilities thanks to actuator
• HealthChecks
• Metrics (Dropwizard aka Coda Hale Metrics)
• Jolokia
![Page 54: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/54.jpg)
Spring Boot & JAX-RS
• Jersey 2.x support out of the box
• Just use the Jersey Starter
• spring-boot-starter-jersey
• raml2code generates JAX-RS artifacts, remember?
![Page 55: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/55.jpg)
Spring Cloud• Distributed/versioned configuration
• Service registration and discovery
• Routing
• Service-to-service calls
• Load balancing
• Circuit Breakers
• Global locks
• Leadership election and cluster state
• Distributed messaging
![Page 56: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/56.jpg)
Netflix OSS• Netflix is released tons of good stuff.
• Reactive Extensions for Java
• Hystrix (Circuit breaker)
• Eureka (Service registry)
• Archaius (Configuration management)
• Zuul (Dynamic routing, monitoring, resilience, security)
• And many more…
![Page 57: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/57.jpg)
Spring Boot loves Netflix OSS
![Page 58: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/58.jpg)
Spring Boot & Spring Cloud for impatient developers
![Page 59: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/59.jpg)
![Page 60: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/60.jpg)
Demo
![Page 61: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/61.jpg)
Each circle is a Docker container
read/write
Hystrix send metrics
Turbine listen events
Turbine generates a http stream
![Page 62: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/62.jpg)
Acknowledgments• To all the platform team at Grupo Expansión
• Alvaro Cabrera (@pateketrueke)
• Anallely Olivares (@tsunllly)
• Angel Pimentel (@blzb)
• Eduardo Diaz (@iamedu)
• Tomás Salazar (@atomsfat)
![Page 63: SGCE 2015 REST APIs](https://reader031.vdocuments.mx/reader031/viewer/2022013118/55c3688fbb61eb14248b47c6/html5/thumbnails/63.jpg)
Enlighten your software
¿Preguntas?@domix
[email protected] http://github.com/domix
http://domingosuarez.com