Java Microservices with Spring Boot and Spring Cloud
December 11, 2019
Matt Raible | @mraible
Photo by Trish McGinity https://www.mcginityphoto.com
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
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 -
Add JAXB for Java 11
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
Enable Eureka Server & Configure application.properties
server.port=8761
eureka.client.register-with-eureka=false
@EnableEurekaServer
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 -
Enable Discovery & Configure application.properties
server.port=8090
spring.application.name=car-service
@EnableDiscoveryClient
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 -
Enable Discovery & Configure application.properties
spring.application.name=api-gateway
@EnableDiscoveryClient
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;
}
Build a REST API in Car Service
@RepositoryRestResource
interface CarRepository extends JpaRepository<Car, Long> {
}
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);
};
}
Consume Cars API in Gateway
@EnableFeignClients
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
Consume Cars API in Gateway
@Data
class Car {
private String name;
}
@FeignClient("car-service")
interface CarClient {
@GetMapping("/cars")
@CrossOrigin
Resources<Car> readCars();
}
Consume Cars API in Gateway
@RestController
class CoolCarController {
private final CarClient carClient;
public CoolCarController(CarClient carClient) {
this.carClient = carClient;
}
// code on next slide
}
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());
}
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");
}
Start everything with ./mvnw
Access https://localhost:8080/cool-cars
Java Microservices with Spring Boot and Spring Cloud
developer.okta.com/blog/2019/05/22/java-microservices-spring-boot-spring-cloud
Java Microservices with Spring Cloud Config & JHipster
developer.okta.com/blog/2019/05/23/java-microservices-spring-cloud-config
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
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
}
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
}
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)
}
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}
}
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
Generated with JDL: Docker Compose
deployment {
deploymentType docker-compose
appsFolders [gateway, blog, store]
dockerRepositoryName "jmicro"
consoleOptions [zipkin]
}
JDL Studio
What’s Next for JHipster?
Full Reactive with WebFlux
and Spring Cloud Gateway
Spring Boot 2.2
GraphQL and Micro Frontends
JHipster Reactive Microservices in Alpha
github.com/jhipster/generator-jhipster/issues/7608
Reactive Microservices with Spring Cloud Gateway
developer.okta.com/blog/2019/08/28/reactive-microservices-spring-cloud-gateway
git clone https://github.com/oktadeveloper/okta-spring-webflux-react-
example.git
github.com/oktadeveloper/java-microservices-examples
Use the Source, Luke!
developer.okta.com/blog
@oktadev
Thanks!
Keep in Touch
raibledesigns.com
@mraible
Presentations
speakerdeck.com/mraible
Code
github.com/oktadeveloper
developer.okta.com

Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019

  • 1.
    Java Microservices withSpring Boot and Spring Cloud December 11, 2019 Matt Raible | @mraible Photo by Trish McGinity https://www.mcginityphoto.com
  • 3.
    Blogger on raibledesigns.comand 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
  • 6.
    Create Java Microservicesusing 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 -
  • 7.
    Add JAXB forJava 11 <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> </dependency>
  • 8.
    Enable Eureka Server& Configure application.properties server.port=8761 eureka.client.register-with-eureka=false @EnableEurekaServer
  • 9.
    Create Car Service httphttps://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 -
  • 10.
    Enable Discovery &Configure application.properties server.port=8090 spring.application.name=car-service @EnableDiscoveryClient
  • 11.
    Create API Gateway httphttps://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 -
  • 12.
    Enable Discovery &Configure application.properties spring.application.name=api-gateway @EnableDiscoveryClient
  • 13.
    Build a RESTAPI in Car Service @Data @NoArgsConstructor @Entity class Car { public Car(String name) { this.name = name; } @Id @GeneratedValue private Long id; @NonNull private String name; }
  • 14.
    Build a RESTAPI in Car Service @RepositoryRestResource interface CarRepository extends JpaRepository<Car, Long> { }
  • 15.
    Build a RESTAPI 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); }; }
  • 16.
    Consume Cars APIin Gateway @EnableFeignClients @EnableCircuitBreaker @EnableDiscoveryClient @SpringBootApplication public class ApiGatewayApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayApplication.class, args); } }
  • 17.
    Consume Cars APIin Gateway @Data class Car { private String name; } @FeignClient("car-service") interface CarClient { @GetMapping("/cars") @CrossOrigin Resources<Car> readCars(); }
  • 18.
    Consume Cars APIin Gateway @RestController class CoolCarController { private final CarClient carClient; public CoolCarController(CarClient carClient) { this.carClient = carClient; } // code on next slide }
  • 19.
    Consume Cars APIin 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()); }
  • 20.
    Consume Cars APIin 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"); }
  • 21.
  • 22.
  • 23.
    Java Microservices withSpring Boot and Spring Cloud developer.okta.com/blog/2019/05/22/java-microservices-spring-boot-spring-cloud
  • 24.
    Java Microservices withSpring Cloud Config & JHipster developer.okta.com/blog/2019/05/23/java-microservices-spring-cloud-config
  • 25.
    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
  • 26.
    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 }
  • 27.
    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 }
  • 28.
    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) }
  • 29.
    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} }
  • 30.
    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
  • 31.
    Generated with JDL:Docker Compose deployment { deploymentType docker-compose appsFolders [gateway, blog, store] dockerRepositoryName "jmicro" consoleOptions [zipkin] }
  • 32.
  • 33.
    What’s Next forJHipster? Full Reactive with WebFlux and Spring Cloud Gateway Spring Boot 2.2 GraphQL and Micro Frontends
  • 34.
    JHipster Reactive Microservicesin Alpha github.com/jhipster/generator-jhipster/issues/7608
  • 35.
    Reactive Microservices withSpring Cloud Gateway developer.okta.com/blog/2019/08/28/reactive-microservices-spring-cloud-gateway
  • 36.
  • 37.
  • 38.