Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Presentation - RESTful Web Services with Kotlin...

SD Kotlin
October 03, 2018

Presentation - RESTful Web Services with Kotlin and Spring Boot by Steve Contreras

Presentation slides for SD Kotlin's October 2018 meeting.

SD Kotlin

October 03, 2018
Tweet

More Decks by SD Kotlin

Other Decks in Programming

Transcript

  1. Part I: Motivation - Definition: Microservices SOA What is a

    microservice? According to Wikipedia, “A microservice is a software development technique—a variant of the service-oriented architecture (SOA) architectural style that structures an application as a collection of loosely coupled services. In a microservices architecture, services are fine-grained and the protocols are lightweight. The benefit of decomposing an application into different smaller services is that it improves modularity and makes the application easier to understand, develop, and test and more resilient to architecture erosion. It parallelizes development by enabling small autonomous teams to develop, deploy and scale their respective services independently. It also allows the architecture of an individual service to emerge through continuous refactoring. Microservices-based architectures enable continuous delivery and deployment.”
  2. Part I: Motivation - The Jist: Microservices SOA vs. Monolithic

    Application (Antipattern) • Orthogonality ◦ Development ◦ Deployment • Best Practices ◦ Separation of Concerns ◦ Narrowly Scoped Functionality ◦ Single Purpose • Result ◦ Modularity ◦ Reduced Maintenance Complexity ◦ Increased Scalability ◦ Focused Continuous Integration and Deployment ◦ The Price: Increased Complexity in... ▪ Implementation Files (common complaint for clean architecture) - more are required ▪ Deployment Infrastructure ▪ DevOps
  3. Part I: Motivation - Definition: Monolithic Application According to Wikipedia,

    A monolithic application is self-contained, and independent from other computing applications. The design philosophy is that the application is responsible not just for a particular task, but can perform every step needed to complete a particular function. Today, some personal finance applications are monolithic in the sense that they help the user carry out a complete task, end to end, and are "private data silos" rather than parts of a larger system of applications that work together. Some word processors are monolithic applications. These applications are sometimes associated with mainframe computers. In software engineering, a monolithic application describes a software application which is designed without modularity. Modularity is desirable, in general, as it supports reuse of parts of the application logic and also facilitates maintenance by allowing repair or replacement of parts of the application without requiring wholesale replacement.
  4. Part I: Motivation - Microservices On the Rise • Industry

    Adoption Rates - Record growth… see https://appdevelopermagazine.com/record-growth-in-microservices/ • Salary Growth - see https://www.itjobswatch.co.uk/jobs/uk/microservices.do ◦ Every metric indicates positive growth ◦ UK median salary equivalent to $84,446.10 USD
  5. Part I: Motivation - RESTful Microservices Why RESTful? • API

    Mindset ◦ Resources - act like objects in typical API usage ◦ Verbs - act like methods in typical API usage ◦ Stateless - Request/Response paradigm, “feels” like typical API function call usage • Human Readable Content/Payload Easy to Debug ◦ JSON ◦ XML • Mature Frameworks Speed Development ◦ Spring MVC and Spring-Boot (our choice) ◦ Others - see https://en.wikipedia.org/wiki/List_of_web_service_frameworks (filter REST in “Protocols” column) Examples: • GET some.url.com/customers/sacontreras • POST some.url.com/customers (new customer details as JSON or XML)
  6. Part II: Architectural Concerns - Separation of Concerns • Inline

    with “Clean Architecture” movement pushed from Google • Conceptual Boundaries/Modularity • Promotes Orthogonality ◦ In Development ◦ Maintenance/Debugging ◦ DeveOps ▪ Continuous Integration ▪ Continuous Deployment • TDD and Testing in General is Easier - ref. Monolithic App Antipattern
  7. Part II: Architectural Concerns - Patterns We will use: •

    Fowler’s Catalog of Patterns of Enterprise Application Architecture ◦ Web Presentation Patterns: ▪ Model View Controller (Page Controller) ◦ Data Source Architectural Patterns: ▪ Data Mapper ◦ Object-Relational Structural Patterns: ▪ Association Table Mapping ▪ Foreign Key Mapping ◦ Domain Logic Patterns: ▪ Service Layer ◦ Object-Relational Metadata Mapping Patterns: ▪ Repository ◦ Distribution Patterns: ▪ Data Transfer Object • Other ◦ Entity (Gullett, n.d.)
  8. Part II: Architectural Concerns - Dependency Injection We will also

    use Dependency Injection. According to Wikipedia, Dependency injection is one form of the broader technique of inversion of control. As with other forms of inversion of control, dependency injection supports the dependency inversion principle. The client delegates the responsibility of providing its dependencies to external code (the injector). The client is not allowed to call the injector code; it is the injecting code that constructs the services and calls the client to inject them. This means the client code does not need to know about the injecting code, how to construct the services or even which actual services it is using; the client only needs to know about the intrinsic interfaces of the services because these define how the client may use the services. This separates the responsibilities of use and construction.
  9. Part II: Architectural Concerns - Dependency Injection (Continued) Why Dependency

    Injection? • SOLID Principles ◦ Single Responsibility - aka Separation of Concerns ◦ Open/Closed - classes open for extension, but closed for modification ◦ Liskov Substitution - objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program ◦ Interface Segregation - many client-specific interfaces are better than one general-purpose interface ◦ Dependency Injection - depend on abstractions, not concretions • Code Against Interfaces • Loose Coupling • Mock Objects in Testing • Client-Code Agnostic to Changes in Implementation • Inline with Separation of Concerns/Clean Architecture
  10. Part II: Architectural Concerns - Spring MVC/Spring-Boot… The Obvious Choice

    • MOST IMPORTANTLY: provides ALL architectural aspects mentioned thus far • JVM Framework = Kotlin support in most cases (but not all); in this case, Kotlin IS supported • Very Mature JVM Framework - first version released in 2003! • HUGE Feature Set ◦ Java Persistence API (“Spring JPA”) - Spring’s Object Relational Mapper ◦ Dependency Injection - for a detailed discussion, please see https://stormpath.com/blog/spring-boot-dependency-injection ◦ Enterprise Security - HTTP Auth in our case ◦ etc. • Enterprise-Development Centric • Spring-Boot ◦ Provides Rapid Development for RESTful Web Services ◦ Embedded Tomcat Server in jar for local development and testing • Spring Developers are in HIGH Demand
  11. Part III: Environment • MySQL • TomCat 8 to Host

    Web Service • AWS EC2 for Production
  12. Part III: Environment - Development Tools: • Github Repo: https://github.com/sacontreras/tm-gpbifo-restful-api

    • Maven Dependency Management ◦ Spring Framework supports Maven ◦ Pom.xml for Maven Builds ◦ use Spring Initializr to generate pom.xml to use any IDE that supports Maven build - see https://start.spring.io • IDE ◦ Spring STS - see https://spring.io/tools ▪ the “official” IDE for the Spring Framework ▪ A variant of Eclipse ◦ IntelliJ (our choice) • Postman ◦ RAW HTTP client (for testing) ◦ See https://www.getpostman.com
  13. Part IV: Implementation - Web Service Functional Requirements See https://github.com/sacontreras/tm-gpbifo-restful-api/blob/master/RESTfulApiRequirements.t

    xt Summary: • What is Tegola Mobile? (demo, part 1) • What is a GeoPackage (Bundle)? (demo, part 2) • GeoPackage Bundle App Web Service ◦ Provide “lookup” (the “R” in “CRUD”) service of published canonical GeoPackage Bundles to Tegola Mobile app users ▪ Read (HTTP GET) is public - i.e. not secured ◦ Provide “CRUD” functionality for GeoPackage Bundles to admin ▪ Create, Update, and Delete operations/methods must be secured - require authentication
  14. Part IV: Implementation - Source Code Review Notes: 1. Pom.xml

    a. Local development vs. Remote Deployment - “packaging” value “jar” vs. “war” i. “jar” for local development - use “spring-boot-starter-web” dep to run in default tomcat embedded container; comment out “spring-boot-starter-tomcat” ii. “war” for remote (production) deployment - use “spring-boot-starter-tomcat” dep to deploy to remote to tomcat instance (on EC2, for example); comment out “spring-boot-starter-web” b. See /build/plugins/kotlin-maven-plugin/dependencies/ i. org.jetbrains.kotlin:kotlin-maven-allopen - effect is all classes open by default; see https://www.baeldung.com/kotlin-allopen-spring
  15. Part IV: Implementation - Source Code Review (Continued) Notes (Continued):

    2. Application.properties a. Spring app properties - configuration b. Base url context c. Spring-Data JPA connection (to MySQL) values d. Date format string (for JSON/XML) e. JSON Web Token value (env var) and time-to-live - will be used to produce the “Authorization” HTTP header value f. GeoPackage Bundle Admin user credentials - retrieved from env vars local to deployment target - local development or remote deployment machine
  16. Part IV: Implementation - Source Code Review (Continued) Notes (Continued):

    3. Spring Annotations a. @RequestController i. Receive HTTP requests ii. Corresponds to Fowler’s PageController P of EAA pattern iii. @RequestMapping 1. specifies URL endpoint mapping 2. at controller class level (with @RequestController) iv. @GetMapping - annotates function handler for HTTP GET (endpoint) v. @PostMapping - annotates function handler for HTTP POST (endpoint)
  17. Part IV: Implementation - Source Code Review (Continued) Notes (Continued):

    3. Spring Annotations (Continued) b. @Service i. Spring Service component ii. Corresponds to Fowler’s ServiceLayer P of EAA pattern iii. Functional interface for Controllers c. @Autowired i. annotates a target variable as having a dependency-injected value, by a Spring “bean” or some other Spring Component ii. variable must be declared “lateinit var” in Kotlin since it is late bound - i.e. allocated once the Spring flow begins
  18. Part IV: Implementation - Source Code Review (Continued) Notes (Continued):

    3. Spring Annotations (Continued) d. @Entity i. Corresponds to Entity enterprise pattern ii. Database table mapping e. @Bean - Spring Component - concretion provision for injectees (@Autowired vars)
  19. Appendix - Page 1 1. Part I: Motivation - The

    Jist: Microservices SOA vs. Monolithic Application (Antipattern) a. Details and Examples: http://www.codingthearchitecture.com/2014/11/19/what_is_a_monolith.html b. Old but enlightening and amusing debate: http://highscalability.com/blog/2014/7/28/the-great-microservices-vs-monolithic-apps-twitter- melee.html 2. Part I: Motivation - Microservices On the Rise a. Kotlin Job Metrics, Enterprise Development and Web Services Context - see https://www.itjobswatch.co.uk/jobs/uk/kotlin.do
  20. Appendix - Page 2 3. Part I: Motivation - RESTful

    Microservices a. Old but quality tutorial/overview: http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069 b. Alternative (to REST) Protocols for Microservices i. Front Runner (in my opinion): gRPC 1. PROs: performance (high speed), HTTP2 multiplexing, protobufs 2. CONs: protobufs (binary) not human readable = harder to debug ii. Others: see https://en.wikipedia.org/wiki/List_of_web_service_frameworks
  21. References - Page 1 Microservices. (2018, September 27). Retrieved from

    https://en.wikipedia.org/wiki/Microservices Monolithic application. (2018, February 04). Retrieved from https://en.wikipedia.org/wiki/Monolithic_application Slocum, M. (2018, March 01). Microservices: A quick and simple definition. Retrieved from https://www.oreilly.com/ideas/a-quick-and-simple-definition-of-microservices Coding the Architecture. (n.d.). What is a Monolith? Retrieved from http://www.codingthearchitecture.com/2014/11/19/what_is_a_monolith.html
  22. References - Page 2 The Great Microservices vs Monolithic Apps

    Twitter Melee - High Scalability -. (n.d.). Retrieved from http://highscalability.com/blog/2014/7/28/the-great-microservices-vs-monolithic-apps-twitter -melee.html Harris, R. (2018, May 02). Record growth in microservices. Retrieved from https://appdevelopermagazine.com/record-growth-in-microservices/ Microservices Jobs. (n.d.). Retrieved from https://www.itjobswatch.co.uk/jobs/uk/microservices.do Vaqqas, M. (n.d.). RESTful Web Services: A Tutorial. Retrieved from http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069
  23. References - Page 3 List of web service frameworks. (2018,

    September 12). Retrieved from https://en.wikipedia.org/wiki/List_of_web_service_frameworks GRPC open-source universal RPC framework. (n.d.). Retrieved from https://grpc.io/ Catalog of Patterns of Enterprise Application Architecture. (n.d.). Retrieved from https://www.martinfowler.com/eaaCatalog/ Gullett, M. (n.d.). The Entity Design Pattern. Retrieved from https://www.codeproject.com/Articles/4293/The-Entity-Design-Pattern Dependency injection. (2018, October 01). Retrieved from https://en.wikipedia.org/wiki/Dependency_injection
  24. References - Page 4 Spring Projects. (n.d.). Retrieved from https://spring.io/projects/spring-boot

    SOLID. (2018, October 02). Retrieved from https://en.wikipedia.org/wiki/SOLID RESTful Web Services, Java, Spring Boot, Spring MVC and JPA. (2018, September 09). Retrieved from https://www.udemy.com/share/100ecy/