際際滷

際際滷Share a Scribd company logo
Microservices With Spring Boot and Spring Cloud
Netflix
Capgemini APPS Evolve! Summit 2016
Bad Soden, 3rd of March 2016
Krzysztof Sobkowiak ( )
The Apache Software Foundation Member
Senior Solution Architect at Capgemini
@ksobkowiak
Outline
Domain Architecture
Basic Technologies
Service Discovery
Load Balancing
Communication
Resilience
Domain Architecture
Three Microservices
Architecture Considerations
Separate data storages
Lots of communication
Bounded Context
Dont modularize microservices by data
Basic Technologies
HSQL Database
In memory database
Not really suited for production use
Keeps the example application easy
Spring Data JPA
Provides support to build repositories based on Spring and JPA
Support for Querydsl predicates and thus type-safe JPA queries
Pagination support, dynamic query execution, ability to integrate custom data access code
p u b l i c i n t e r f a c e P e r s o n R e p o s i t o r y e x t e n d s P a g i n g A n d S o r t i n g R e p o s i t o r y < P e r s o n , L o n g > {
L i s t < P e r s o n > f i n d B y L a s t N a m e ( @ P a r a m ( " n a m e " ) S t r i n g n a m e ) ;
}
Spring Data REST
Provides the domain objects with little effort via REST
Can hide certain data elements
Can be con gured exibly
Tight coupling between the internal model and the interface can be decoupled
@ R e p o s i t o r y R e s t R e s o u r c e ( c o l l e c t i o n R e s o u r c e R e l = " p e o p l e " , p a t h = " p e o p l e " )
p u b l i c i n t e r f a c e P e r s o n R e p o s i t o r y e x t e n d s P a g i n g A n d S o r t i n g R e p o s i t o r y < P e r s o n , L o n g > {
L i s t < P e r s o n > f i n d B y L a s t N a m e ( @ P a r a m ( " n a m e " ) S t r i n g n a m e ) ;
}
Spring Boot
It can be pretty small
Prede ned packages/starters available
Can generate WAR or JAR le
@ R e s t C o n t r o l l e r
@ S p r i n g B o o t A p p l i c a t i o n
p u b l i c c l a s s C o n t r o l l e r A n d M a i n {
@ R e q u e s t M a p p i n g ( " / " )
p u b l i c S t r i n g h e l l o ( ) {
r e t u r n " h e l l o " ;
}
p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) {
S p r i n g A p p l i c a t i o n . r u n (
C o n t r o l l e r A n d M a i n . c l a s s , a r g s ) ;
}
}
Writing a single service is nice
but no microservice is an island
Challenges of Distributed Systems
Con guration management
Service registration & discovery
Routing & balancing
Fault tolerance (Circuit Breakers!)
Monitoring
Spring Cloud
Spring Cloud Components
Security - supports the implementation of security mechanisms
Con g - centralizes and dynamically adjusts the con guration
Bus - sends dynamic con guration changes for Spring Cloud Con g
Sleuth - distributed tracing with tools like Zipkin or Htrace
Zookeeper - supports Apache Zookeeper
Consult - facilitates Services Discovery using Consul
Cluster - implements leader election and stateful patterns using technologies like Zookeeper or Consul
Stream - supports messaging using Redis, Rabbit or Kafka
Spring Cloud Netflix
Zuul - routing
Ribbon - Load Balancer.
Hystrix - resilience in Microservices.
Turbine - can consolidate monitoring data from different Hystrix servers.
Feign - option for an easier implementation of REST clients
Eureka - Service Discovery
Service Discovery
Why Eureka?
REST based service registry
Supports replication
Caches on the client
Resilient
Fast, but not consistent
Foundation for other services
Eureka Client
Registers automatically with the Eureka server under a de ned name
Can access other Microservices
Integrates Load Balancing with Ribbon using
DiscoveryClient, FeignClient
Eureka aware RestTemplate(sample later)
@EnableDiscoveryClientor @EnableEurekaClient
Dependency to spring-cloud-starter-eureka
e u r e k a . c l i e n t . s e r v i c e U r l . d e f a u l t Z o n e = h t t p : / / e u r e k a : 8 7 6 1 / e u r e k a /
e u r e k a . i n s t a n c e . l e a s e R e n e w a l I n t e r v a l I n S e c o n d s = 5
s p r i n g . a p p l i c a t i o n . n a m e = c a t a l o g
e u r e k a . i n s t a n c e . m e t a d a t a M a p . i n s t a n c e I d = c a t a l o g : $ { r a n d o m . v a l u e }
e u r e k a . i n s t a n c e . p r e f e r I p A d d r e s s = t r u e
Eureka Server
@EnableEurekaServer
Dependency to cloud-starter-eureka-server
@ E n a b l e E u r e k a S e r v e r
@ E n a b l e A u t o C o n f i g u r a t i o n
p u b l i c c l a s s E u r e k a A p p l i c a t i o n {
p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) {
S p r i n g A p p l i c a t i o n . r u n ( E u r e k a A p p l i c a t i o n . c l a s s , a r g s ) ;
}
}
Eureka Dashboard
Load Balancing
Ribbon
Decentralized, client side Load Balancing
No bottle neck
Resilient
Registration information might be inconsistent
RestTemplate& Load Balancing
@RibbonClient
Dependency to spring-cloud-starter-ribbon
@ R i b b o n C l i e n t ( n a m e = " r i b b o n A p p " )
. . . / / L e f t o u t o t h e r S p r i n g C l o u d / B o o t A n n o t a t i o n s
p u b l i c c l a s s R i b b o n A p p {
@ A u t o w i r e d
p r i v a t e R e s t T e m p l a t e r e s t T e m p l a t e ;
p u b l i c v o i d c a l l M i c r o s e r v i c e ( ) {
S t o r e s t o r e = r e s t T e m p l a t e . g e t F o r O b j e c t ( " h t t p : / / s t o r e s / s t o r e / 1 " , S t o r e . c l a s s ) ;
}
}
Communication
Zuul Routing
One URL to outside
Internal many microservices
Maps route to server registered on Eureka, e.g.
/customer/**to CUSTOMER
Allows to internally change the structure of the
Microservices
REST or HTML gui
@EnableZuulProxy, dependency to spring-cloud-starter-zuul
Alternatively @EnableZuulServer- no routing, uses lters
Eureka, Zuul & Ribbon Interactions
Resilience
Microservices can deal with the failure of other Microservices
Even if a called Microservice is not available, they will still work
Hystrix
Enables resilient applications
Call in other thread
Wont block request handler
Can implement timeout
Circuit Breaker with Hystrix
Circuit open after certain number (error threshold) of failed calls
If open, calls not directed to called system
After con gured window circuit closes
Hystrix with Annotations
Java proxies automaticaly created
Annotations of javanica library
@EnableCircuitBreakeror @EnableHystrix, dependency to spring-cloud-starter-hystrix
@ H y s t r i x C o m m a n d ( f a l l b a c k M e t h o d = " g e t I t e m s C a c h e " , c o m m a n d P r o p e r t i e s = {
@ H y s t r i x P r o p e r t y ( n a m e = " c i r c u i t B r e a k e r . r e q u e s t V o l u m e T h r e s h o l d " , v a l u e = " 2 " )
} )
p u b l i c C o l l e c t i o n < I t e m > f i n d A l l ( ) {
t h i s . i t e m s C a c h e = . . .
. . .
r e t u r n p a g e d R e s o u r c e s . g e t C o n t e n t ( ) ;
}
p r i v a t e C o l l e c t i o n < I t e m > g e t I t e m s C a c h e ( ) {
r e t u r n i t e m s C a c h e ;
}
Hystrix Dashboard
Turbine
Aggregates data from different Hystrix systems
The state of all Circuit Breakers can be summarized on a single dashboard
Turbine
@EnableTurbineand @EnableEurekaClient, dependency to spring-cloud-starter-turbine
Can use @EnableHystrixDashboardto display dahsboard on the same instance
Consolidated data determined by the con guration
t u r b i n e :
a g g r e g a t o r :
c l u s t e r C o n f i g : O R D E R
a p p C o n f i g : o r d e r
Conclusion
Easy to create new project
REST integrated
messaging supported
Simple deployment
Uniform operations
Thanks!
Any questions?
Microservices demo -
Travel demo -
https://github.com/ewolff/microservice
https://github.com/microservices-summit-2016/resilience-demo

More Related Content

Microservices With Spring Boot and Spring Cloud Netflix

  • 1. Microservices With Spring Boot and Spring Cloud Netflix Capgemini APPS Evolve! Summit 2016 Bad Soden, 3rd of March 2016 Krzysztof Sobkowiak ( ) The Apache Software Foundation Member Senior Solution Architect at Capgemini @ksobkowiak
  • 2. Outline Domain Architecture Basic Technologies Service Discovery Load Balancing Communication Resilience
  • 5. Architecture Considerations Separate data storages Lots of communication Bounded Context Dont modularize microservices by data
  • 7. HSQL Database In memory database Not really suited for production use Keeps the example application easy
  • 8. Spring Data JPA Provides support to build repositories based on Spring and JPA Support for Querydsl predicates and thus type-safe JPA queries Pagination support, dynamic query execution, ability to integrate custom data access code p u b l i c i n t e r f a c e P e r s o n R e p o s i t o r y e x t e n d s P a g i n g A n d S o r t i n g R e p o s i t o r y < P e r s o n , L o n g > { L i s t < P e r s o n > f i n d B y L a s t N a m e ( @ P a r a m ( " n a m e " ) S t r i n g n a m e ) ; }
  • 9. Spring Data REST Provides the domain objects with little effort via REST Can hide certain data elements Can be con gured exibly Tight coupling between the internal model and the interface can be decoupled @ R e p o s i t o r y R e s t R e s o u r c e ( c o l l e c t i o n R e s o u r c e R e l = " p e o p l e " , p a t h = " p e o p l e " ) p u b l i c i n t e r f a c e P e r s o n R e p o s i t o r y e x t e n d s P a g i n g A n d S o r t i n g R e p o s i t o r y < P e r s o n , L o n g > { L i s t < P e r s o n > f i n d B y L a s t N a m e ( @ P a r a m ( " n a m e " ) S t r i n g n a m e ) ; }
  • 10. Spring Boot It can be pretty small Prede ned packages/starters available Can generate WAR or JAR le @ R e s t C o n t r o l l e r @ S p r i n g B o o t A p p l i c a t i o n p u b l i c c l a s s C o n t r o l l e r A n d M a i n { @ R e q u e s t M a p p i n g ( " / " ) p u b l i c S t r i n g h e l l o ( ) { r e t u r n " h e l l o " ; } p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { S p r i n g A p p l i c a t i o n . r u n ( C o n t r o l l e r A n d M a i n . c l a s s , a r g s ) ; } }
  • 11. Writing a single service is nice
  • 12. but no microservice is an island
  • 13. Challenges of Distributed Systems Con guration management Service registration & discovery Routing & balancing Fault tolerance (Circuit Breakers!) Monitoring
  • 15. Spring Cloud Components Security - supports the implementation of security mechanisms Con g - centralizes and dynamically adjusts the con guration Bus - sends dynamic con guration changes for Spring Cloud Con g Sleuth - distributed tracing with tools like Zipkin or Htrace Zookeeper - supports Apache Zookeeper Consult - facilitates Services Discovery using Consul Cluster - implements leader election and stateful patterns using technologies like Zookeeper or Consul Stream - supports messaging using Redis, Rabbit or Kafka
  • 16. Spring Cloud Netflix Zuul - routing Ribbon - Load Balancer. Hystrix - resilience in Microservices. Turbine - can consolidate monitoring data from different Hystrix servers. Feign - option for an easier implementation of REST clients Eureka - Service Discovery
  • 18. Why Eureka? REST based service registry Supports replication Caches on the client Resilient Fast, but not consistent Foundation for other services
  • 19. Eureka Client Registers automatically with the Eureka server under a de ned name Can access other Microservices Integrates Load Balancing with Ribbon using DiscoveryClient, FeignClient Eureka aware RestTemplate(sample later) @EnableDiscoveryClientor @EnableEurekaClient Dependency to spring-cloud-starter-eureka e u r e k a . c l i e n t . s e r v i c e U r l . d e f a u l t Z o n e = h t t p : / / e u r e k a : 8 7 6 1 / e u r e k a / e u r e k a . i n s t a n c e . l e a s e R e n e w a l I n t e r v a l I n S e c o n d s = 5 s p r i n g . a p p l i c a t i o n . n a m e = c a t a l o g e u r e k a . i n s t a n c e . m e t a d a t a M a p . i n s t a n c e I d = c a t a l o g : $ { r a n d o m . v a l u e } e u r e k a . i n s t a n c e . p r e f e r I p A d d r e s s = t r u e
  • 20. Eureka Server @EnableEurekaServer Dependency to cloud-starter-eureka-server @ E n a b l e E u r e k a S e r v e r @ E n a b l e A u t o C o n f i g u r a t i o n p u b l i c c l a s s E u r e k a A p p l i c a t i o n { p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { S p r i n g A p p l i c a t i o n . r u n ( E u r e k a A p p l i c a t i o n . c l a s s , a r g s ) ; } }
  • 23. Ribbon Decentralized, client side Load Balancing No bottle neck Resilient Registration information might be inconsistent
  • 24. RestTemplate& Load Balancing @RibbonClient Dependency to spring-cloud-starter-ribbon @ R i b b o n C l i e n t ( n a m e = " r i b b o n A p p " ) . . . / / L e f t o u t o t h e r S p r i n g C l o u d / B o o t A n n o t a t i o n s p u b l i c c l a s s R i b b o n A p p { @ A u t o w i r e d p r i v a t e R e s t T e m p l a t e r e s t T e m p l a t e ; p u b l i c v o i d c a l l M i c r o s e r v i c e ( ) { S t o r e s t o r e = r e s t T e m p l a t e . g e t F o r O b j e c t ( " h t t p : / / s t o r e s / s t o r e / 1 " , S t o r e . c l a s s ) ; } }
  • 26. Zuul Routing One URL to outside Internal many microservices Maps route to server registered on Eureka, e.g. /customer/**to CUSTOMER Allows to internally change the structure of the Microservices REST or HTML gui @EnableZuulProxy, dependency to spring-cloud-starter-zuul Alternatively @EnableZuulServer- no routing, uses lters
  • 27. Eureka, Zuul & Ribbon Interactions
  • 29. Microservices can deal with the failure of other Microservices Even if a called Microservice is not available, they will still work
  • 30. Hystrix Enables resilient applications Call in other thread Wont block request handler Can implement timeout
  • 31. Circuit Breaker with Hystrix Circuit open after certain number (error threshold) of failed calls If open, calls not directed to called system After con gured window circuit closes
  • 32. Hystrix with Annotations Java proxies automaticaly created Annotations of javanica library @EnableCircuitBreakeror @EnableHystrix, dependency to spring-cloud-starter-hystrix @ H y s t r i x C o m m a n d ( f a l l b a c k M e t h o d = " g e t I t e m s C a c h e " , c o m m a n d P r o p e r t i e s = { @ H y s t r i x P r o p e r t y ( n a m e = " c i r c u i t B r e a k e r . r e q u e s t V o l u m e T h r e s h o l d " , v a l u e = " 2 " ) } ) p u b l i c C o l l e c t i o n < I t e m > f i n d A l l ( ) { t h i s . i t e m s C a c h e = . . . . . . r e t u r n p a g e d R e s o u r c e s . g e t C o n t e n t ( ) ; } p r i v a t e C o l l e c t i o n < I t e m > g e t I t e m s C a c h e ( ) { r e t u r n i t e m s C a c h e ; }
  • 34. Turbine Aggregates data from different Hystrix systems The state of all Circuit Breakers can be summarized on a single dashboard
  • 35. Turbine @EnableTurbineand @EnableEurekaClient, dependency to spring-cloud-starter-turbine Can use @EnableHystrixDashboardto display dahsboard on the same instance Consolidated data determined by the con guration t u r b i n e : a g g r e g a t o r : c l u s t e r C o n f i g : O R D E R a p p C o n f i g : o r d e r
  • 36. Conclusion Easy to create new project REST integrated messaging supported Simple deployment Uniform operations
  • 37. Thanks! Any questions? Microservices demo - Travel demo - https://github.com/ewolff/microservice https://github.com/microservices-summit-2016/resilience-demo