Java Microservices with Spring Boot and Spring Cloud - Lightning Talk at Denver Microservices May 2019

Java Microservices with Spring Boot and Spring Cloud - Lightning Talk at Denver Microservices May 2019

A lightning talk I gave at the Denver Microservices meetup on May 22, 2019. It's based off two blog posts I recently wrote:

* Java Microservices with Spring Boot and Spring Cloud
* Java Microservices with Spring Cloud Config and JHipster
* Secure Reactive Microservices with Spring Cloud Gateway

72a2082c6a4dd79ad68befb3db911616?s=128

Matt Raible

May 22, 2019
Tweet

Transcript

  1. Java Microservices with Spring Boot and Spring Cloud May 22,

    2019 Matt Raible | @mraible Photo by Scott Ingram flickr.com/photos/scingram/332232160
  2. None
  3. Blogger on raibledesigns.com and developer.okta.com/blog Web Developer and Java Champion

    Father, Husband, Skier, Mountain Biker, Whitewater Rafter Open Source Connoisseur Hi, I’m Matt Raible! Bus Lover Okta Developer Advocate
  4. None
  5. Create Java Microservices using start.spring.io http https://start.spring.io/starter.zip javaVersion==11 \ artifactId==discovery-service

    name==eureka-service \ dependencies==cloud-eureka-server baseDir==discovery-service \ | tar -xzvf -
  6. Add JAXB for Java 11 <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> </dependency>

  7. Enable Eureka Server & Configure application.properties server.port=8761 eureka.client.register-with-eureka=false @EnableEurekaServer

  8. Create Car Service http https://start.spring.io/starter.zip \ artifactId==car-service name==car-service baseDir==car-service \

    dependencies==actuator,cloud-eureka,data-jpa,h2,data- rest,web,devtools,lombok | tar -xzvf -
  9. Enable Discovery & Configure application.properties server.port=8090 spring.application.name=car-service @EnableDiscoveryClient

  10. Create API Gateway http https://start.spring.io/starter.zip \ artifactId==api-gateway name==api-gateway baseDir==api-gateway \

    dependencies==cloud-eureka,cloud-feign,data-rest,web,cloud- hystrix,lombok | tar -xzvf -
  11. Enable Discovery & Configure application.properties spring.application.name=api-gateway @EnableDiscoveryClient

  12. Build a REST API in Car Service @Data @NoArgsConstructor @Entity

    class Car { public Car(String name) { this.name = name; } @Id @GeneratedValue private Long id; @NonNull private String name; }
  13. Build a REST API in Car Service @RepositoryRestResource interface CarRepository

    extends JpaRepository<Car, Long> { }
  14. Build a REST API in Car Service @Bean ApplicationRunner init(CarRepository

    repository) { return args -> { Stream.of("Ferrari", "Jaguar", "Porsche", "Lamborghini", "Bugatti", "AMC Gremlin", "Triumph Stag", "Ford Pinto", "Yugo GV").forEach(name -> { repository.save(new Car(name)); }); repository.findAll().forEach(System.out::println); }; }
  15. Consume Cars API in Gateway @EnableFeignClients @EnableCircuitBreaker @EnableDiscoveryClient @SpringBootApplication public

    class ApiGatewayApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayApplication.class, args); } }
  16. Consume Cars API in Gateway @Data class Car { private

    String name; } @FeignClient("car-service") interface CarClient { @GetMapping("/cars") @CrossOrigin Resources<Car> readCars(); }
  17. Consume Cars API in Gateway @RestController class CoolCarController { private

    final CarClient carClient; public CoolCarController(CarClient carClient) { this.carClient = carClient; } // code on next slide }
  18. Consume Cars API in Gateway private Collection<Car> fallback() { return

    new ArrayList<>(); } @GetMapping("/cool-cars") @CrossOrigin @HystrixCommand(fallbackMethod = "fallback") public Collection<Car> goodCars() { return carClient.readCars() .getContent() .stream() .filter(this::isCool) .collect(Collectors.toList()); }
  19. Consume Cars API in Gateway private boolean isCool(Car car) {

    return !car.getName().equals("AMC Gremlin") && !car.getName().equals("Triumph Stag") && !car.getName().equals("Ford Pinto") && !car.getName().equals(“Yugo GV"); }
  20. Start everything with ./mvnw

  21. Access https://localhost:8080/cool-cars

  22. Java Microservices with Spring Boot and Spring Cloud developer.okta.com/blog/2019/05/22/java-microservices-spring-boot-spring-cloud

  23. Java Microservices with Spring Cloud Config & JHipster developer.okta.com/blog/2019/05/23/java-microservices-spring-cloud-config

  24. Generated with JDL: Gateway application { config { baseName gateway,

    packageName com.okta.developer.gateway, applicationType gateway, authenticationType oauth2, prodDatabaseType postgresql, serviceDiscoveryType eureka, testFrameworks [protractor] } entities Blog, Post, Tag, Product } jhipster.tech/jdl/#application_options
  25. Generated with JDL: Blog with PostgreSQL application { config {

    baseName blog, packageName com.okta.developer.blog, applicationType microservice, authenticationType oauth2, prodDatabaseType postgresql, serverPort 8081, serviceDiscoveryType eureka } entities Blog, Post, Tag }
  26. Generated with JDL: Store with MongoDB application { config {

    baseName store, packageName com.okta.developer.store, applicationType microservice, authenticationType oauth2, databaseType mongodb, devDatabaseType mongodb, prodDatabaseType mongodb, enableHibernateCache false, serverPort 8082, serviceDiscoveryType eureka } entities Product }
  27. Generated with JDL: Entities for Blog entity Blog { name

    String required minlength(3), handle String required minlength(2) } entity Post { title String required, content TextBlob required, date Instant required } entity Tag { name String required minlength(2) }
  28. Generated with JDL: Entities for Store + Relationships entity Product

    { title String required, price BigDecimal required min(0), image ImageBlob } relationship ManyToOne { Blog{user(login)} to User, Post{blog(name)} to Blog } relationship ManyToMany { Post{tag(name)} to Tag{post} }
  29. Generated with JDL: Pagination and Microservice paginate Post, Tag with

    infinite-scroll paginate Product with pagination microservice Product with store microservice Blog, Post, Tag with blog
  30. Generated with JDL: Docker Compose deployment { deploymentType docker-compose appsFolders

    [gateway, blog, store] dockerRepositoryName "jmicro" consoleOptions [zipkin] }
  31. JDL Studio

  32. git clone https://github.com/oktadeveloper/okta-spring-webflux-react- example.git github.com/oktadeveloper/java-microservices-examples Use the Source, Luke!

  33. developer.okta.com/blog @oktadev

  34. Thanks! Keep in Touch raibledesigns.com @mraible Presentations speakerdeck.com/mraible Code github.com/oktadeveloper

    developer.okta.com