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

Microservices with Spring Boot, Angular, Docker and Terraform

Microservices with Spring Boot, Angular, Docker and Terraform

These are the slides for my session about creating, building and deploying a microservice in 60 minutes.

Kai Toedter

May 27, 2018
Tweet

More Decks by Kai Toedter

Other Decks in Technology

Transcript

  1. Who am I?  Principal Key Expert at Siemens Building

    Technologies  Web Technology Fan  Open Source Lover  E-mail: [email protected]  Twitter: @kaitoedter 5/27/2018 2 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License.
  2. Show Hands! 5/27/2018 © Kai Tödter, Licensed under a Creative

    Commons Attribution 4.0 International License. 3
  3. Domain Classes with Project Lombok @Data @Entity @NoArgsConstructor @AllArgsConstructor public

    class Thing { @Id private String id; private String name; private String color; } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 7
  4. Spring Data REST: Repository public interface ThingRepository extends CrudRepository<Thing, String>

    { } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 8
  5. Spring Data Rest: JSON Result { "_embedded" : { "things"

    : [ { "name" : "Hammer", "color" : "Orange", "_links" : { "self" : { "href" : "https://microservice-60min.herokuapp.com/api/things/1" } … "_links" : { "self" : { "href" : "https://microservice-60min.herokuapp.com/api/things" } … } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 9
  6. API Documentation 5/27/2018 © Kai Tödter, Licensed under a Creative

    Commons Attribution 4.0 International License. 10
  7. API Documentation Tools  Swagger (OpenAPI)  Spring REST Docs

     RAML (RESTful API Modeling Language)  … 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 11
  8. Swagger + Hypermedia  Pros  High Adoption  Good

    tooling  Active community  Cons  It’s URI-Centric  Not suitable for hypermedia and links  OpenAPI v3 introduced links, but they are not sufficient for Hypermedia doc  It’s has a pretty big footprint (many libs)  Not recommended for the documentation of Level 3 REST APIs 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 13
  9. Spring REST Docs  Write Tests for the API documentation

     Tests will fail if real behavior and documented behavior are out of sync!  Include generated AsciiDoc snippets in manually written AsciiDoc API documentation  Supports Hypermedia well 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 14
  10. Spring REST Docs Example Test @Test public void shouldDocumentBuildInfo() throws

    Exception { this.mockMvc.perform(get("/api/buildinfo")) .andExpect(status().isOk()) .andDo(document("build-info-get-example", responseFields( fieldWithPath("version").description("The version of this build"), fieldWithPath("timeStamp").description("The creation timestamp"), fieldWithPath("_links.self").description("link to this resource") ))); } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 15
  11. Spring REST Docs Example Result 5/27/2018 © Kai Tödter, Licensed

    under a Creative Commons Attribution 4.0 International License. 16
  12. Angular  Angular is a framework for building client applications

    in HTML  TypeScript, JavaScript, Dart  Modules, Components, Templates, Services  Much more… 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 18
  13. Angular 2 Seed Projects  https://github.com/angular/angular2-seed  https://github.com/mgechev/angular-seed  …

    5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 19
  14. Angular CLI  Installation: npm install -g @angular/cli  Create

    a new project and run it: ng new <project name> cd <project name> ng serve  Create new component: ng g component <new component> 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 20
  15. Thing Service @Injectable() export class ThingsService { constructor(private http: HttpClient)

    {} public getThings(): Observable<Thing[]> { let observable: Observable<Thing[]> = this.http.get('/api/things').pipe( map((response: any) => response._embedded['things']), catchError(this.handleError)); return observable; } } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 21
  16. Thing Component @Component({ selector: 'things', templateUrl: 'things.component.html', providers: [ThingsService], })

    export class ThingsComponent { private things: Thing[]; constructor(private thingsService: ThingsService) {} ngOnInit() { this.thingsService.getThings().subscribe( (things: Thing[]) => this.things = things, error => console.error(‘Cannot get things from ThingsService')); } } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 22
  17. Split App in different Layers  Spring Boot apps are

    fat JARs  Between 40 and 60 MB external dependencies  Put this in an extra layer in the Docker Image In most cases (after updating the Docker image) you push only a few MB to a Docker registry 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 24
  18. Extract Spring Boot Fat JAR  Unzip your app.jar to

    docker/app  Create a Dokerfile like: FROM java:openjdk-8-jre-alpine COPY docker/lib/ /app/BOOT-INF/lib/ COPY docker/app/ /app/ CMD java -cp /app/ \ org.springframework.boot.loader.JarLauncher EXPOSE 8080 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 25
  19. 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution

    4.0 International License. 26 Build Polyglot Projects
  20. Build of Polyglot Projects  Gradle as master  Gradle

    Plugin "com.moowork.node"  Installs Node.js  Wrapper for npm and npm based tasks  Webpack for Angular build 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 27
  21. CORS Filter for fast Development @Configuration @Profile("dev") public class DevCorsConfiguration

    { @Bean public FilterRegistrationBean corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedOrigin("http://localhost:4200"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/api/**", config); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); return bean; } } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 28
  22. Spring Profiles  During development, set the dev profile 

    Start the app with --spring.profiles.active=dev 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 29
  23. Travis CI  Free Builds for Github Projects  Can

     Create Docker images  Push to Docker registries  Provide .travis.yml at top level 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 30
  24. .travis.yml sudo: required language: java jdk: - oraclejdk8 services: -

    docker install: true script: - ./gradlew build … 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 31
  25. Cloud Deployment 5/27/2018 © Kai Tödter, Licensed under a Creative

    Commons Attribution 4.0 International License. 32
  26. Heroku  Popular Cloud Provider  Provides free deployment of

    your (small) apps  Great for testing and demos  www.heroku.com 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 33
  27. Heroku Dashboard 5/27/2018 © Kai Tödter, Licensed under a Creative

    Commons Attribution 4.0 International License. 34
  28. Heroku Docker Push Example docker build -t ms60min docker login

    --username=_ \ --password="$HEROKU_AUTH_TOKEN" \ registry.heroku.com docker tag ms60min \ registry.heroku.com/microservice-60min/web docker push registry.heroku.com/microservice-60min/web 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 35
  29. Terraform  Write  Infrastructure as code  Plan 

    Preview changes before applying  Create  Always reproducible infrastructure 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 37
  30. Terraform for AWS resource "aws_instance" "web" { instance_type = "t2.micro"

    ami = "ami-f4cc1de2" # Ubuntu Server 16.04 security_groups = ["${aws_security_group.ms60min.name}"] key_name = "microservice-60min" tags { Name = "microservice-60min“ } connection { user = "ubuntu" private_key = "${file("microservice-60min.pem")}" } } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 38
  31. Live Demos Sources: https://github.com/toedter/microservice-60min Travis-CI Build: https://travis-ci.org/toedter/microservice-60min Demo: https://microservice-60min.herokuapp.com HAL

    Browser: https://microservice-60min.herokuapp.com/api/ 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 39
  32. Links  Spring Data REST: http://projects.spring.io/spring-data-rest/  Project Lombok: http://projectlombok.org/

     Heroku: http://www.heroku.com  Terraform: https://www.terraform.io/ 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 41
  33. License  This work is licensed under a Creative Commons

    Attribution 4.0 International License.  See http://creativecommons.org/licenses/by/4.0/ 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 42