Slide 1

Slide 1 text

Kai Tödter

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

Show Hands! 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 3

Slide 4

Slide 4 text

Demo  https://microservice-60min.herokuapp.com 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 4

Slide 5

Slide 5 text

5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 5

Slide 6

Slide 6 text

start.spring.io 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Spring Data REST: Repository public interface ThingRepository extends CrudRepository { } 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 8

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

API Documentation 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Swagger 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 12

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Spring REST Docs Example Result 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 16

Slide 17

Slide 17 text

5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 17

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Angular CLI  Installation: npm install -g @angular/cli  Create a new project and run it: ng new cd ng serve  Create new component: ng g component 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 20

Slide 21

Slide 21 text

Thing Service @Injectable() export class ThingsService { constructor(private http: HttpClient) {} public getThings(): Observable { let observable: Observable = 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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 23

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 26 Build Polyglot Projects

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

.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

Slide 32

Slide 32 text

Cloud Deployment 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 32

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Heroku Dashboard 5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 34

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 36 Terraform

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

5/27/2018 © Kai Tödter, Licensed under a Creative Commons Attribution 4.0 International License. 40 Discussion

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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