Spring Boot and Spring Cloud are an ideal foundation for creating Microservices based on Java. This presentation explains basic concepts of these libraries.
3. Eberhard Wolff - @ewolff
Micro Services: Definition
• Small
• Independent deployment units
• i.e. processes or VMs
• Any technology
• Any infrastructure
• Include GUI
Micro
Service
Server
Micro
Service
Server
4. Eberhard Wolff - @ewolff
Components Collaborate
Micro
Service
Micro
Service
Link
Data Replication
REST
Messaging
5. Eberhard Wolff - @ewolff
Infrastructure for
Microservices
• Lots of services
• Need infrastructure
Easy to create a new project
REST integrated
Messaging supported
Uniform operations
7. Eberhard Wolff - @ewolff
Easy to Create New Project
• One pom.xml
• …Gradle / Ant
• Very few dependencies
• One plug in
• Versions defined for you
8. Eberhard Wolff - @ewolff
REST Integrated
• Support in Spring MVC
• As we have seen
• Also support for JAX-RS
• Jersey
9. Eberhard Wolff - @ewolff
Messaging Support
• Numerous Spring Boot Starter
• AMQP (RabbitMQ)
• HornetQ (JMS)
• ActiveMQ (JMS, no starter)
10. Eberhard Wolff - @ewolff
Messaging Support
• Spring JMS abstraction
• Message driven POJOs
• Scalable
• Simplify sending JMS
• Can use other libs, too!
• Boot can do everything plain Spring /
Java can do
11. Eberhard Wolff - @ewolff
Infrastructure for
Microservices
• More services
• Need infrastructure
Easy to create a new project
REST integrated
Messaging supported
Simple deployment
Uniform operations
✓
✓
✓
13. Eberhard Wolff - @ewolff
Deploy
• Just package everything in an
executable JAR
• …or a WAR
• Based on Maven, Ant or Gradle
• Build in configuration (YAML,
properties etc.)
15. Eberhard Wolff - @ewolff
Infrastructure for
Microservices
• More services
• Need infrastructure
Easy to create a new project
REST integrated
Messaging supported
Simple deployment
Uniform operations
✓
✓
✓
✓
16. Eberhard Wolff - @ewolff
Spring Boot Actuator
• Provide information about the
application
• Via http / JSON
• Can be evaluated by monitoring
tools etc.
• Another alternative approach to
monitoring
23. Eberhard Wolff - @ewolff
Spring Cloud
• Spring support for Amazon Web
Services
• Connector for Heroku PaaS
• …and Cloud Foundry PaaS
• The rest of Spring Cloud is for
Microservices
27. Eberhard Wolff - @ewolff
Why Eureka?
• REST based service registry
• Supports replication
• Caches on the client
• Resilient
• Fast
• …but not consistent
• Foundation for other services
28. Eberhard Wolff - @ewolff
Eureka Client in Spring Cloud
• @EnableDiscoveryClient:
generic
• @EnableEurekaClient:
more specific
• Dependency to
spring-cloud-starter-eureka
• Automatically registers application
29. Eberhard Wolff - @ewolff
application.properties
eureka.client.serviceUrl.defaultZone=http://host:
8761/eureka/<
eureka.instance.leaseRenewalIntervalInSeconds=5<
spring.application.name=catalog<
eureka.instance.metadataMap.instanceId=$
{spring.application.name}:${random.value}<
eureka.instance.preferIpAddress=true<
Eureka server
Can include user / password
Need unique ID
Load balancing
Faster updates
Docker won’t resolve host names
Used for registration
In CAPITAL caps
30. Eberhard Wolff - @ewolff
Eureka Server
@EnableEurekaServer<
@EnableAutoConfiguration<
public'class'EurekaApplication'{'
<
<public'static'void'main(String[]'args)'{'
<<SpringApplication.run(EurekaApplication.class,'args);'
<}<
<
}<
Add dependency to
spring-cloud-starter-eureka-server
35. Eberhard Wolff - @ewolff
Routing
• One URL to the outside
• Internal: Many Microservices
• REST
• Or HTML GUI
36. Eberhard Wolff - @ewolff
Customer Order Catalog
Zuul
Proxy
Automatically maps route to server registered on Eureka
i.e. /customer/**
to CUSTOMER
No configuration
Can add filters etc
37. Eberhard Wolff - @ewolff
Zuul Proxy
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulApplication.class).
web(true).run(args);
}
}
Enable Zuul Proxy
Can change route
Also routing to external services possible
44. Eberhard Wolff - @ewolff
Spring Cloud Bus
• Pushed config updates
• …or individual message
• I prefer a messaging solution
• Independent from Spring
45. Eberhard Wolff - @ewolff
Microservice Microservice
Must find each other
Configuration
Route calls to a service
Communication
Security
47. Eberhard Wolff - @ewolff
Spring Cloud Security
• Single Sign On via OAuth2
• Forward token e.g. via
RestTemplate
• Support for Zuul
• Very valuable!
51. Eberhard Wolff - @ewolff
Ribbon: Client Side Load
Balancing
• Decentralized Load Balancing
• No bottle neck
• Resilient
• Hard to consider metrics / health
• Data might be inconsistent
Load
Balancer
Server
Client
52. Eberhard Wolff - @ewolff
RestTemplate & Load
Balancing
@RibbonClient(name = "ribbonApp")
…
public class RibbonApp {
@Autowired
private RestTemplate restTemplate;
public void callMicroService() {
Store store = restTemplate.
getForObject("http://stores/store/1",
Store.class);
}
}
Enable Ribbon
Left out other annotations
Eureka name or server list
Standard Spring
REST client
Can also use Ribbon API
55. Eberhard Wolff - @ewolff
Hystrix
• Enable resilient applications
• Do call in other thread pool
• Won’t block request handler
• Can implement timeout
56. Eberhard Wolff - @ewolff
Hystrix
• Circuit Breaker
• If call system fail open
• If open do not forward call
• Forward calls after a time window
• System won’t be swamped with
requests
57. Eberhard Wolff - @ewolff
Hystrix / Spring Cloud
• Annotation based approach
• Java Proxies automatically created
• Annotations of javanica libraries
• Simplifies Hystrix dramatically
• No commands etc
65. Eberhard Wolff - @ewolff
Must find each other: Service Discovery
Configuration
Route calls to a service
Communication
Load Balancing
Resilience
Spring Cloud for
Microservices