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

Java Microservices with Spring Boot and Spring ...

Matt Raible
December 11, 2019

Java Microservices with Spring Boot and Spring Cloud

A lightning talk I gave at the Denver JUG meetup on December 11, 2019. This presentation shows how to build a secure microservices architecture with Spring Boot and Spring Cloud. It also shows you how you can generate the same architecture using JHipster. It's based on three blog posts I wrote:

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

Matt Raible

December 11, 2019
Tweet

More Decks by Matt Raible

Other Decks in Programming

Transcript

  1. Java Microservices with Spring Boot and Spring Cloud December 11,

    2019 Matt Raible | @mraible Photo by Trish McGinity https://www.mcginityphoto.com
  2. 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
  3. 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 -
  4. 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 -
  5. 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 -
  6. 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; }
  7. 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); }; }
  8. Consume Cars API in Gateway @EnableFeignClients @EnableCircuitBreaker @EnableDiscoveryClient @SpringBootApplication public

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

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

    final CarClient carClient; public CoolCarController(CarClient carClient) { this.carClient = carClient; } // code on next slide }
  11. 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()); }
  12. 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"); }
  13. 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
  14. 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 }
  15. 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 }
  16. 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) }
  17. 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} }
  18. 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
  19. Generated with JDL: Docker Compose deployment { deploymentType docker-compose appsFolders

    [gateway, blog, store] dockerRepositoryName "jmicro" consoleOptions [zipkin] }
  20. What’s Next for JHipster? Full Reactive with WebFlux and Spring

    Cloud Gateway Spring Boot 2.2 GraphQL and Micro Frontends