Slide 1

Slide 1 text

Java Microservices with Spring Boot and Spring Cloud December 11, 2019 Matt Raible | @mraible Photo by Trish McGinity https://www.mcginityphoto.com

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

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 -

Slide 7

Slide 7 text

Add JAXB for Java 11 org.glassfish.jaxb jaxb-runtime

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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 -

Slide 10

Slide 10 text

Enable Discovery & Configure application.properties server.port=8090 spring.application.name=car-service @EnableDiscoveryClient

Slide 11

Slide 11 text

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 -

Slide 12

Slide 12 text

Enable Discovery & Configure application.properties spring.application.name=api-gateway @EnableDiscoveryClient

Slide 13

Slide 13 text

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; }

Slide 14

Slide 14 text

Build a REST API in Car Service @RepositoryRestResource interface CarRepository extends JpaRepository { }

Slide 15

Slide 15 text

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); }; }

Slide 16

Slide 16 text

Consume Cars API in Gateway @EnableFeignClients @EnableCircuitBreaker @EnableDiscoveryClient @SpringBootApplication public class ApiGatewayApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayApplication.class, args); } }

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Consume Cars API in Gateway @RestController class CoolCarController { private final CarClient carClient; public CoolCarController(CarClient carClient) { this.carClient = carClient; } // code on next slide }

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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"); }

Slide 21

Slide 21 text

Start everything with ./mvnw

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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 }

Slide 27

Slide 27 text

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 }

Slide 28

Slide 28 text

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) }

Slide 29

Slide 29 text

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} }

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Generated with JDL: Docker Compose deployment { deploymentType docker-compose appsFolders [gateway, blog, store] dockerRepositoryName "jmicro" consoleOptions [zipkin] }

Slide 32

Slide 32 text

JDL Studio

Slide 33

Slide 33 text

What’s Next for JHipster? Full Reactive with WebFlux and Spring Cloud Gateway Spring Boot 2.2 GraphQL and Micro Frontends

Slide 34

Slide 34 text

JHipster Reactive Microservices in Alpha github.com/jhipster/generator-jhipster/issues/7608

Slide 35

Slide 35 text

Reactive Microservices with Spring Cloud Gateway developer.okta.com/blog/2019/08/28/reactive-microservices-spring-cloud-gateway

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

developer.okta.com/blog @oktadev

Slide 38

Slide 38 text

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