SlideShare a Scribd company logo
Building ‘Bootiful’
microservices cloud
Powered by Spring Cloud Netflix libraries
Idan Fridman, Tikal
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Client Side Load Balancer: Ribbon
● Router and Filter: Automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
● There is a strong trend in distributed systems with lightweight
architectures
● People have started to call them "microservices"
What are microservices?
(In short, I promise;P)
● Smaller units of a larger system
small enough to be owned by a small agile development team
re-writable within one or two agile sprints ( typically two to four
weeks)
The complexity does not require to refactoring or require further
divide into another microservice.
● Not monolithic
● Runs in its own process
Running independently of the application If fail app
won't fail and deliver workarounds
Life continues as if nothing has happened.
● Lightweight communication protocols
Rely on HTTP(Rest, etc..) or a lightweight
message bus
Establish a boundary between components
Ensures loose coupling.
● Single responsibility principle(SRP)
Should perform a single business function
Must be functionally complete
Also knowns as bounded context from
Domain-Driven Design
● Independent data source
Soa vs Microservices
- SOA and microservices don’t conflict or replace each other -> they
complement
- SOA is about how to reuse existing functionality as services
- Microservices is about how to make functionality to scale better with high
resilience and short release cycles...
Challenges with Microservices
● Distributed/versioned configuration
○ Auto configurations and refresh on runtime
● New services can auto-register at startup
○ Service registration and discovery
● Routing and load balancing
○ Clients can detect new instances as they are started up
● Centralized log management
○ Collects and visualize log events from distributed processes
● Circuit Breaker
○ Prevent problems with chain of failures
● Security
Open source to the rescue
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
Build your endpoints with Spring-
Boot
It can get pretty much small:
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
Pretty much micro?
Spring Boot
● A tool for getting started very quickly with Spring
● Common non-functional requirements for a "real" application
● Exposes a lot of useful features by default
● Gets out of the way quickly if you want to change defaults
● Available for web, batch, integration, data, amqp, aop, jdbc, ...
Spring Boot Modules
● Spring Boot - main library supporting the other parts of Spring Boot
● Spring Boot Autoconfigure - single @EnableAutoConfiguration annotation
creates a whole Spring context
● Spring Boot Starters - a set of convenient dependency descriptors that you
can include in your application.
Spring Boot Modules
● Spring Boot Actuator - common non-functional features that make an app
instantly deployable and supportable in production
● Spring Boot Tools - for building and executing self-contained JAR and
WAR archives
● Spring Boot Samples - a wide range of sample apps
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
Spring cloud - Netflix
● Spring Cloud Netflix provides Netflix OSS integration
● With simple annotations enable and configure can build large distributed
systems with battle-tested Netflix components.
● The patterns provided include Service Discovery (Eureka), Circuit Breaker
(Hystrix), Intelligent Routing (Zuul) and Client Side Load Balancing
(Ribbon)..
System Setup
System Landscape
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
Service discovery Eureka
Discover
Motivation
● When you’re running your software across a large number of replaceable pieces of hardware, it’
s inevitable that one of them will fail at some point.
● In a cloud environment you are guaranteed failure, and it is absolutely
● Critical to have a service discovery system that can survive failures.
Eureka features
● Eureka has a built-in concept of service heartbeats to prevent stale data - if a service doesn’t phone home often enough,
then Eureka will remove the entry from the service registry
● HeartBeat - New services can register, but “dead” ones will be kept, just in case a client might still be able to contact them
● Cache - Eureka caches on the client side. So even if every last Eureka server goes down, or there is a partition where a
client can’t talk to any of the Eureka servers, then the service registry still won’t be lost.
● HA - The server can be configured and deployed to be highly available, with each server replicating state about the
registered services to the others.
Building ‘Bootiful’ microservices cloud
Demo - 1
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Netflix Turbine
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
Circuit Breaker
What is the circuit breaker pattern?
● Communication with microservices(or any other services)
○ Remote calls
○ Different machines across a network
○ API calls and dependencies
● Unresponsive supplier
● Hang without a response
● Critical resources leading to cascading failures across systems
Failure risks
Building ‘Bootiful’ microservices cloud
How it works?
Hystrix uses the bulkhead pattern to isolate dependencies and limit concurrent access.
The idea behind circuit breaker
1. Monitor For failures
2. Failure occur -> Circuit is open -> Protected call wont be made -> Do something else + raise alert
3. Need external intervention to reset it when things are well again
Not all errors should trip the circuit, some should reflect normal failures and be dealt regularly
Remote function
call
Circuit breaker object
wrapper
Circuit breaker monitoring
● Any change in breaker state should be logged
● Breakers should reveal details of their state for deeper monitoring
What’s next after the break?
● You need to consider what to do in case of failure.
○ perhaps business decision?
○ Workarounds?
○ Show partials results?
Examples:
1. A bank action authorization could be put on a queue to deal with later.
2. Show partial content (better than nothing right?)
Circuit Breaker and Hystrix
What’s Hystrix?
● Hystrix evolved out of resilience engineering work that the Netflix API team began in 2011
● Isolating points of access between the services
● Handling fallbacks and protect API calls by stopping them(defining thresholds, etc..)
● Provide temporary fallback exchanges
● Monitoring dashboard to watch our our critical access points
Taken from spring.io:
HystrixCommand
● Wrap calls to dependencies in a HystrixCommand object
● HystrixCommand follows the command pattern and typically executes within a separate thread
● Hystrix maintains a thread-pool (or semaphore) for each dependency and rejects requests
(instead of queuing requests)
● if the thread-pool becomes exhausted -> It provides circuit-breaker functionality that can stop all
requests to a dependency.
Spring cloud for the rescue
● Using spring-cloud-netflix library we can implement circuit breaker logic
easily:
○ Hystrix Synchronous
○ Hystrix Asynchronous
○ Hystrix Observable
Hystrix synchronous
fallbackMethod
@HystrixCommand(fallbackMethod = "fallbackInvocation")
private void generateService(ObjectInput input) {
//invoke your service
}
public void fallbackInvocation(ObjectInput input) {
//keepDefaultvalues
//raise alert
//getNextServer from Eureka and invoke the method
//return data from a local cache
}
Hystrix asynchronous
Using future
@HystrixCommand(fallbackMethod = "stubbedBro")
public Future<String> runFutureBro(final String name) {
return new AsyncResult<String>() {
public String invoke() {
return(executeRemoteService(name));
}
};}
//and somewhere else:
this.runFutureBro(name).get();
Hystrix asynchronous
Using subscriptions
@HystrixCommand(fallbackMethod = "stubbedBro")
public Observable<String> executeObservableBro(String name) {
return new ObservableResult<String>() {
@Override
public String invoke() {
return executeRemoteService(name);
}
};
}
Hystrix asynchronous
Using subscriptions
broConsumerService.executeObservableBro(input).subscribe(new Observer<String>() {
@Override
public void onCompleted() {
System.out.println("completed");
}
@Override
public void onError(Throwable e) {
System.out.printf(e.getMessage());
}
@Override
public void onNext(String s) {
System.out.println("on next.." + s);
}
});
Command properties
@HystrixCommand(commandProperties = {
//execute on local thread or separate thread(Thread,Semaphore)
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
//sets the time in milliseconds after which the caller will observe a timeout and walk away from the command
//execution (default 1000)
},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
//maximum number of HystrixCommands that can execute concurrently
@HystrixProperty(name="hystrix.command.default.circuitBreaker.requestVolumeThreshold",
value = "30"),
//sets the minimum number of requests in a rolling window that will trip the circuit.
//maximum queue size
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
})
Drawbacks
● Each command execution involves:
○ Queueing
○ Scheduling
○ Context switching involved in running a command on a separate thread.
● Netflix’s experiences proved it minor enough to not have major cost or performance impact.
Demo - 2
Protected with Hystrix
Hystrix Dashboard
● You can Monitor your access points(wrapped points) via ui dashboard and grab metrics
Building ‘Bootiful’ microservices cloud
Enabling HystrixDashboards
Add @EnableHystrixDashboard
Demo-3
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Netflix Turbine
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava for aggregation of micro-service requests
● Declarative REST Client: Feign
Netflix Turbine
● Aggregating metrics streams from all cloud instances into one stream
● Use case: Use Hystrix real-time dashboard that uses Turbine to aggregate
from many machines
Demo
Rabbitmq
● Turbine can aggregate all streams by receiving all clients metrics via amqp
● Hystrix commands push metrics to Turbine and Spring Cloud enables that
with AMQP messaging
● Hysteric use RabbitMQ to communicate between circuit breakers and
dashboards
Annotate your turbine server with:
@EnableTurbineAmqp
Have all your Hystrix clients importing:
compile("org.springframework.cloud:spring-cloud-starter-hystrix:1.0.0.RELEASE")
compile("org.springframework.cloud:spring-cloud-starter-bus-amqp:1.0.0.RELEASE")
compile("org.springframework.cloud:spring-cloud-netflix-hystrix-amqp:1.0.0.RELEASE")
That library has everything you need in order to find running rabbitMQ broker and start pushing
messages.
In your Hystrix dashboard add the turbine’s amqp listening port: http://host:8989 (set via application.
yaml)
Demo4
1. Start rabbitmq sbin/rabbitmq-server
2. Stream Turbine using port 8989
[demo]
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Netflix Turbine
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
Netflix Ribbon
Client Side Load Balancing
● Dynamic Routing
● Load Balancer
● Support rules:
○ round robin
○ response time weighted
○ random load balancing
○ Custom rules
● Ribbon does not run as a separate service but instead as an embedded component in each service consumer.
Using Ribbon with Eureka
● Ribbon includes load balancers that are capable of service discovery in a dynamic environment
● Ribbon uses the information available in Eureka to locate appropriate service instances.
● Ribbon will apply load balancing to spread the requests over the available instances.
● Add new instance dynamically -> Registered automatically on discovery services -> Ribbon choose instance to
invoke
Ribbon Example
Ribbon and RestTemplate
● Inject RestTemplate and use your instance-id
● Spring Cloud Ribbon will handle automatically the load-balancing
@EnableEurekaClient
@RestController
public class Application {
@Autowired
RestTemplate restTemplate
@RequestMapping("/")
String consume() {
Response response = restTemplate.getForObject("http://baggage-service", Response.class)
}
}
Does make sense: Eureka’s service name
The ribbon-loadbalancer must be on the classpath (e.g. via "spring-cloud-starter-
ribbon").
Then you can inject one as a LoadBalancerClient or you can inject a RestTemplate
(it will be load-balancer aware if you have a LoadBalancerClient).
Demo
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Netflix Turbine
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava for aggregation of micro-service requests
● Declarative REST Client: Feign
Netflix Zuul - Edge Server
Zuul the Gatekeeper of Gozer
The Gatekeeper
of our microservices world
● Entry point to the microservices
● Zuul is a JVM based router and server side load balancer by Netflix.
● Intelligent Routing
● Using Ribbon to lookup available services and routes the external request to an
appropriate service instance
● By default Zuul set up a route to every service it can find in Eureka
Using Routings
● Routing in an integral part of a microservice architecture.
For example, / -> mapped to your web application,
/api/bookings -> mapped to the flight-booking service
/api/coupon -> mapped to the coupon service.
Zuul harmonically integrated with Netflix stack
● Zuul using Ribbon to locate an instance to forward to via Eureka
● All requests are executed in a hystrix command -> failures will show up in
Hystrix metrics
● once the circuit is open the proxy will not try to contact the service.
Use cases
● Use case where a UI application wants to proxy calls to one or more back end service
●
Other use cases:
● Authentication
● Stress Testing
● Security
● Service Migration
● Active/Active traffic management
Set your routings
On application.yaml you can set your routings and roles
In this example all services are ignored except coupon-service.
[Demo]
Demo - 5
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Netflix Turbine
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
How many services being called?
Scenario Example
1. Client calls to our app’s web service ->
2. our webservice need to request multiple micro-services->
3. uses a callbacks interface to pass the successful result to the
next web service call
4. define another success callback- >
5. and then moves on to the next web service request.
Orchestrator
A B C F
Client Request
Looks like that->
Also known as:
“The Callback Hell”
//The "Nested Callbacks" Way
public void fetchUserDetails() {
//first, request the users...
mService.requestUsers(new Callback<GithubUsersResponse>() {
@Override
public void success(final GithubUsersResponse githubUsersResponse,
final Response response) {
Timber.i(TAG, "Request Users request completed");
final List<GithubUserDetail> githubUserDetails = new ArrayList<GithubUserDetail>();
//next, loop over each item in the response
for (GithubUserDetail githubUserDetail : githubUsersResponse) {
//request a detail object for that user
mService.requestUserDetails(githubUserDetail.mLogin,
new Callback<GithubUserDetail>() {
@Override
public void success(GithubUserDetail githubUserDetail,
Response response) {
Log.i("User Detail request completed for user : " + githubUserDetail.mLogin);
githubUserDetails.add(githubUserDetail);
if (githubUserDetails.size() == githubUsersResponse.mGithubUsers.size()) {
//we've downloaded'em all - notify all who are interested!
Async our microservices call
(Using Reactor)
A library for composing asynchronous and
event-based programs by using observable sequences.
● Allow you to compose sequences together declaratively
● Abstracting away :
○ low-level threading
○ synchronization
○ thread-safety
○ concurrent data structures
○ non-blocking I/O.
RxJava for the rescue
● RxJava is single jar lightweight library.
● Using the Observable abstraction and related higher-order functions.
(Support Java6+)
The following external libraries can work with RxJava:
● Camel RX provides an easy way to reuse any of the Apache Camel components, protocols, transports and data
formats with the RxJava API
● rxjava-http-tail allows you to follow logs over HTTP, like tail -f
● mod-rxvertx - Extension for VertX that provides support for Reactive Extensions (RX) using the RxJava library
And ->> Hystrix latency and fault tolerance bulkheading library. (Which is why we here)
Using Observables
“Go do this thing and when that thing is done then invoke
that other code”
@HystrixCommand(fallbackMethod = "stubbedBro")
public Observable<String> executeObservableBro(String name) {
return new ObservableResult<String>() {
@Override
public String invoke() {
return executeRemoteService(name);
}
};
}
Agenda
● Microservices - Quick overview
● Implementing microservices - Build your endpoints with Spring-Boot
● Spring Cloud and Netflix
● Service Discovery: Eureka instances
● Circuit Breaker: Hystrix
● Netflix Turbine
● Client Side Load Balancer: Ribbon
● Router and Filter: automatic registration of Zuul
● RxJava: Aggregation of multiple micro-service requests
● Declarative REST Client: Feign
Demo
Feign Client
● It makes writing web service clients easier
● Feign connect your code to http API’S with minimal overhead.
● Maps to the REST API methods that are exposed by a service
● Customizable decoders and error handlings
● Feign works by processing annotations into a templatized request
Feign and Spring-Cloud
● Create interface and annotate
● Support Feign annotations and JAX-RS annotations
● Collaborated with Spring-MVC annotations(Using convertors like: HttpMessageConverters )
● Eureka-aware REST clients that uses Ribbon for client-side load-balancing to pick an available
service instance
● You can call methods on an interface instead of manually handling URLs, Spring Cloud takes
care of auto-instantiating the interface implementation and will use Eureka for discovery.
Demo 6
Thank You:)

More Related Content

PDF
Microservices with Netflix OSS and Spring Cloud - Dev Day Orange
PDF
Microservices with Netflix OSS and Spring Cloud
PDF
Microservice With Spring Boot and Spring Cloud
PDF
Microservices with Java, Spring Boot and Spring Cloud
PDF
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
PDF
Cloud Native Microservices with Spring Cloud
PDF
Microservices with Spring and Cloud Foundry
PPTX
Spring Cloud and Netflix Components
Microservices with Netflix OSS and Spring Cloud - Dev Day Orange
Microservices with Netflix OSS and Spring Cloud
Microservice With Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring Cloud
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Cloud Native Microservices with Spring Cloud
Microservices with Spring and Cloud Foundry
Spring Cloud and Netflix Components

What's hot (20)

PDF
Cloud Foundry for Spring Developers
PDF
Developing Resilient Cloud Native Apps with Spring Cloud
PPTX
Kubernetes your next application server
PDF
Open Service Broker APIとKubernetes Service Catalog #k8sjp
PDF
Cloud Native Microservices with Spring Cloud
PPTX
Serverless and Servicefull Applications - Where Microservices complements Ser...
PDF
CDK Meetup: Rule the World through IaC
PPTX
Vert.x for Microservices Architecture
PDF
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
PPTX
What’s New in Spring Batch?
PPTX
MicroServices at Netflix - challenges of scale
PDF
Spring IO 2016 - Spring Cloud Microservices, a journey inside a financial entity
PDF
A Series of Fortunate Events: Building an Operator in Java
PDF
Create Great CNCF User-Base from Lessons Learned from Other Open Source Commu...
PDF
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
PDF
Microservices - java ee vs spring boot and spring cloud
PDF
Istio presentation jhug
PDF
OPNFV & OpenStack
PDF
Microservices with Micronaut
PDF
Security model for a remote company
Cloud Foundry for Spring Developers
Developing Resilient Cloud Native Apps with Spring Cloud
Kubernetes your next application server
Open Service Broker APIとKubernetes Service Catalog #k8sjp
Cloud Native Microservices with Spring Cloud
Serverless and Servicefull Applications - Where Microservices complements Ser...
CDK Meetup: Rule the World through IaC
Vert.x for Microservices Architecture
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
What’s New in Spring Batch?
MicroServices at Netflix - challenges of scale
Spring IO 2016 - Spring Cloud Microservices, a journey inside a financial entity
A Series of Fortunate Events: Building an Operator in Java
Create Great CNCF User-Base from Lessons Learned from Other Open Source Commu...
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
Microservices - java ee vs spring boot and spring cloud
Istio presentation jhug
OPNFV & OpenStack
Microservices with Micronaut
Security model for a remote company
Ad

Viewers also liked (20)

PDF
Hystrix in Action - Ein Weg zu robuster Software
PDF
Spring 3.1 and MVC Testing Support - 4Developers
PDF
Chicago Hadoop Users Group: Enterprise Data Workflows
PDF
Reactive Programming With Akka - Lessons Learned
PDF
The no-framework Scala Dependency Injection Framework
PDF
A Sceptical Guide to Functional Programming
PDF
Effective akka scalaio
PDF
Actor Based Asyncronous IO in Akka
PDF
Efficient HTTP Apis
PDF
Spring Cloud and Netflix OSS overview v1
PDF
Beginning Haskell, Dive In, Its Not That Scary!
PPTX
C*ollege Credit: Creating Your First App in Java with Cassandra
PDF
Service discovery with Eureka and Spring Cloud
PDF
Effective Actors
PPTX
Spring cloud sleuth
KEY
Curator intro
PPTX
Unix Philosophy
PDF
Effective Scala (SoftShake 2013)
PDF
Using Apache Solr
PDF
Composable and streamable Play apps
Hystrix in Action - Ein Weg zu robuster Software
Spring 3.1 and MVC Testing Support - 4Developers
Chicago Hadoop Users Group: Enterprise Data Workflows
Reactive Programming With Akka - Lessons Learned
The no-framework Scala Dependency Injection Framework
A Sceptical Guide to Functional Programming
Effective akka scalaio
Actor Based Asyncronous IO in Akka
Efficient HTTP Apis
Spring Cloud and Netflix OSS overview v1
Beginning Haskell, Dive In, Its Not That Scary!
C*ollege Credit: Creating Your First App in Java with Cassandra
Service discovery with Eureka and Spring Cloud
Effective Actors
Spring cloud sleuth
Curator intro
Unix Philosophy
Effective Scala (SoftShake 2013)
Using Apache Solr
Composable and streamable Play apps
Ad

Similar to Building ‘Bootiful’ microservices cloud (20)

PDF
2017 Microservices Practitioner Virtual Summit: Microservices at Squarespace ...
ODP
Developing Microservices using Spring - Beginner's Guide
PDF
Microservices and modularity with java
PDF
'How to build efficient backend based on microservice architecture' by Anton ...
PPTX
Springboot Microservices
PDF
Lagom : Reactive microservice framework
PDF
Easy Microservices with JHipster - Devoxx BE 2017
PDF
Devoxx Belgium 2017 - easy microservices with JHipster
PDF
Kenzan: Architecting for Microservices
PDF
Day in the life event-driven workshop
PDF
NetflixOSS Open House Lightning talks
PDF
Comparison of Current Service Mesh Architectures
PDF
Monolithic to Microservices Migration Journey of iyzico with Spring Cloud
PDF
Triangle Devops Meetup 10/2015
PDF
Microservices @ Work - A Practice Report of Developing Microservices
PDF
Monolithic to microservices migration journey with spring cloud
PDF
C. Sotiriou, Vodafone Greece: Adopting Quarkus for the digital experience layer
PDF
Better Deployments with Sub Environments Using Spring Cloud and Netflix Ribbon
ODP
Spring cloud for microservices architecture
PDF
OpenStack Neutron: What's New In Kilo and a Look Toward Liberty
2017 Microservices Practitioner Virtual Summit: Microservices at Squarespace ...
Developing Microservices using Spring - Beginner's Guide
Microservices and modularity with java
'How to build efficient backend based on microservice architecture' by Anton ...
Springboot Microservices
Lagom : Reactive microservice framework
Easy Microservices with JHipster - Devoxx BE 2017
Devoxx Belgium 2017 - easy microservices with JHipster
Kenzan: Architecting for Microservices
Day in the life event-driven workshop
NetflixOSS Open House Lightning talks
Comparison of Current Service Mesh Architectures
Monolithic to Microservices Migration Journey of iyzico with Spring Cloud
Triangle Devops Meetup 10/2015
Microservices @ Work - A Practice Report of Developing Microservices
Monolithic to microservices migration journey with spring cloud
C. Sotiriou, Vodafone Greece: Adopting Quarkus for the digital experience layer
Better Deployments with Sub Environments Using Spring Cloud and Netflix Ribbon
Spring cloud for microservices architecture
OpenStack Neutron: What's New In Kilo and a Look Toward Liberty

Building ‘Bootiful’ microservices cloud

  • 1. Building ‘Bootiful’ microservices cloud Powered by Spring Cloud Netflix libraries Idan Fridman, Tikal
  • 2. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Client Side Load Balancer: Ribbon ● Router and Filter: Automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 3. ● There is a strong trend in distributed systems with lightweight architectures ● People have started to call them "microservices"
  • 4. What are microservices? (In short, I promise;P)
  • 5. ● Smaller units of a larger system small enough to be owned by a small agile development team re-writable within one or two agile sprints ( typically two to four weeks) The complexity does not require to refactoring or require further divide into another microservice. ● Not monolithic ● Runs in its own process Running independently of the application If fail app won't fail and deliver workarounds Life continues as if nothing has happened. ● Lightweight communication protocols Rely on HTTP(Rest, etc..) or a lightweight message bus Establish a boundary between components Ensures loose coupling. ● Single responsibility principle(SRP) Should perform a single business function Must be functionally complete Also knowns as bounded context from Domain-Driven Design ● Independent data source
  • 6. Soa vs Microservices - SOA and microservices don’t conflict or replace each other -> they complement - SOA is about how to reuse existing functionality as services - Microservices is about how to make functionality to scale better with high resilience and short release cycles...
  • 7. Challenges with Microservices ● Distributed/versioned configuration ○ Auto configurations and refresh on runtime ● New services can auto-register at startup ○ Service registration and discovery ● Routing and load balancing ○ Clients can detect new instances as they are started up ● Centralized log management ○ Collects and visualize log events from distributed processes ● Circuit Breaker ○ Prevent problems with chain of failures ● Security
  • 8. Open source to the rescue
  • 9. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 10. Build your endpoints with Spring- Boot It can get pretty much small: @RestController class ThisWillActuallyRun { @RequestMapping("/") String home() { "Hello World!" } } Pretty much micro?
  • 11. Spring Boot ● A tool for getting started very quickly with Spring ● Common non-functional requirements for a "real" application ● Exposes a lot of useful features by default ● Gets out of the way quickly if you want to change defaults ● Available for web, batch, integration, data, amqp, aop, jdbc, ...
  • 12. Spring Boot Modules ● Spring Boot - main library supporting the other parts of Spring Boot ● Spring Boot Autoconfigure - single @EnableAutoConfiguration annotation creates a whole Spring context ● Spring Boot Starters - a set of convenient dependency descriptors that you can include in your application.
  • 13. Spring Boot Modules ● Spring Boot Actuator - common non-functional features that make an app instantly deployable and supportable in production ● Spring Boot Tools - for building and executing self-contained JAR and WAR archives ● Spring Boot Samples - a wide range of sample apps
  • 14. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 15. Spring cloud - Netflix ● Spring Cloud Netflix provides Netflix OSS integration ● With simple annotations enable and configure can build large distributed systems with battle-tested Netflix components. ● The patterns provided include Service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon)..
  • 17. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 19. Motivation ● When you’re running your software across a large number of replaceable pieces of hardware, it’ s inevitable that one of them will fail at some point. ● In a cloud environment you are guaranteed failure, and it is absolutely ● Critical to have a service discovery system that can survive failures.
  • 20. Eureka features ● Eureka has a built-in concept of service heartbeats to prevent stale data - if a service doesn’t phone home often enough, then Eureka will remove the entry from the service registry ● HeartBeat - New services can register, but “dead” ones will be kept, just in case a client might still be able to contact them ● Cache - Eureka caches on the client side. So even if every last Eureka server goes down, or there is a partition where a client can’t talk to any of the Eureka servers, then the service registry still won’t be lost. ● HA - The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.
  • 23. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Netflix Turbine ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 25. What is the circuit breaker pattern? ● Communication with microservices(or any other services) ○ Remote calls ○ Different machines across a network ○ API calls and dependencies ● Unresponsive supplier ● Hang without a response ● Critical resources leading to cascading failures across systems Failure risks
  • 27. How it works? Hystrix uses the bulkhead pattern to isolate dependencies and limit concurrent access.
  • 28. The idea behind circuit breaker 1. Monitor For failures 2. Failure occur -> Circuit is open -> Protected call wont be made -> Do something else + raise alert 3. Need external intervention to reset it when things are well again Not all errors should trip the circuit, some should reflect normal failures and be dealt regularly Remote function call Circuit breaker object wrapper
  • 29. Circuit breaker monitoring ● Any change in breaker state should be logged ● Breakers should reveal details of their state for deeper monitoring
  • 30. What’s next after the break? ● You need to consider what to do in case of failure. ○ perhaps business decision? ○ Workarounds? ○ Show partials results? Examples: 1. A bank action authorization could be put on a queue to deal with later. 2. Show partial content (better than nothing right?)
  • 32. What’s Hystrix? ● Hystrix evolved out of resilience engineering work that the Netflix API team began in 2011 ● Isolating points of access between the services ● Handling fallbacks and protect API calls by stopping them(defining thresholds, etc..) ● Provide temporary fallback exchanges ● Monitoring dashboard to watch our our critical access points Taken from spring.io:
  • 33. HystrixCommand ● Wrap calls to dependencies in a HystrixCommand object ● HystrixCommand follows the command pattern and typically executes within a separate thread ● Hystrix maintains a thread-pool (or semaphore) for each dependency and rejects requests (instead of queuing requests) ● if the thread-pool becomes exhausted -> It provides circuit-breaker functionality that can stop all requests to a dependency.
  • 34. Spring cloud for the rescue ● Using spring-cloud-netflix library we can implement circuit breaker logic easily: ○ Hystrix Synchronous ○ Hystrix Asynchronous ○ Hystrix Observable
  • 35. Hystrix synchronous fallbackMethod @HystrixCommand(fallbackMethod = "fallbackInvocation") private void generateService(ObjectInput input) { //invoke your service } public void fallbackInvocation(ObjectInput input) { //keepDefaultvalues //raise alert //getNextServer from Eureka and invoke the method //return data from a local cache }
  • 36. Hystrix asynchronous Using future @HystrixCommand(fallbackMethod = "stubbedBro") public Future<String> runFutureBro(final String name) { return new AsyncResult<String>() { public String invoke() { return(executeRemoteService(name)); } };} //and somewhere else: this.runFutureBro(name).get();
  • 37. Hystrix asynchronous Using subscriptions @HystrixCommand(fallbackMethod = "stubbedBro") public Observable<String> executeObservableBro(String name) { return new ObservableResult<String>() { @Override public String invoke() { return executeRemoteService(name); } }; }
  • 38. Hystrix asynchronous Using subscriptions broConsumerService.executeObservableBro(input).subscribe(new Observer<String>() { @Override public void onCompleted() { System.out.println("completed"); } @Override public void onError(Throwable e) { System.out.printf(e.getMessage()); } @Override public void onNext(String s) { System.out.println("on next.." + s); } });
  • 39. Command properties @HystrixCommand(commandProperties = { //execute on local thread or separate thread(Thread,Semaphore) @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500") //sets the time in milliseconds after which the caller will observe a timeout and walk away from the command //execution (default 1000) }, threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "30"), //maximum number of HystrixCommands that can execute concurrently @HystrixProperty(name="hystrix.command.default.circuitBreaker.requestVolumeThreshold", value = "30"), //sets the minimum number of requests in a rolling window that will trip the circuit. //maximum queue size @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"), @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440") })
  • 40. Drawbacks ● Each command execution involves: ○ Queueing ○ Scheduling ○ Context switching involved in running a command on a separate thread. ● Netflix’s experiences proved it minor enough to not have major cost or performance impact.
  • 41. Demo - 2 Protected with Hystrix
  • 42. Hystrix Dashboard ● You can Monitor your access points(wrapped points) via ui dashboard and grab metrics
  • 46. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Netflix Turbine ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava for aggregation of micro-service requests ● Declarative REST Client: Feign
  • 47. Netflix Turbine ● Aggregating metrics streams from all cloud instances into one stream ● Use case: Use Hystrix real-time dashboard that uses Turbine to aggregate from many machines Demo
  • 48. Rabbitmq ● Turbine can aggregate all streams by receiving all clients metrics via amqp ● Hystrix commands push metrics to Turbine and Spring Cloud enables that with AMQP messaging ● Hysteric use RabbitMQ to communicate between circuit breakers and dashboards
  • 49. Annotate your turbine server with: @EnableTurbineAmqp Have all your Hystrix clients importing: compile("org.springframework.cloud:spring-cloud-starter-hystrix:1.0.0.RELEASE") compile("org.springframework.cloud:spring-cloud-starter-bus-amqp:1.0.0.RELEASE") compile("org.springframework.cloud:spring-cloud-netflix-hystrix-amqp:1.0.0.RELEASE") That library has everything you need in order to find running rabbitMQ broker and start pushing messages. In your Hystrix dashboard add the turbine’s amqp listening port: http://host:8989 (set via application. yaml)
  • 50. Demo4 1. Start rabbitmq sbin/rabbitmq-server 2. Stream Turbine using port 8989
  • 52. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Netflix Turbine ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 53. Netflix Ribbon Client Side Load Balancing ● Dynamic Routing ● Load Balancer ● Support rules: ○ round robin ○ response time weighted ○ random load balancing ○ Custom rules ● Ribbon does not run as a separate service but instead as an embedded component in each service consumer.
  • 54. Using Ribbon with Eureka ● Ribbon includes load balancers that are capable of service discovery in a dynamic environment ● Ribbon uses the information available in Eureka to locate appropriate service instances. ● Ribbon will apply load balancing to spread the requests over the available instances. ● Add new instance dynamically -> Registered automatically on discovery services -> Ribbon choose instance to invoke
  • 56. Ribbon and RestTemplate ● Inject RestTemplate and use your instance-id ● Spring Cloud Ribbon will handle automatically the load-balancing @EnableEurekaClient @RestController public class Application { @Autowired RestTemplate restTemplate @RequestMapping("/") String consume() { Response response = restTemplate.getForObject("http://baggage-service", Response.class) } } Does make sense: Eureka’s service name The ribbon-loadbalancer must be on the classpath (e.g. via "spring-cloud-starter- ribbon"). Then you can inject one as a LoadBalancerClient or you can inject a RestTemplate (it will be load-balancer aware if you have a LoadBalancerClient).
  • 57. Demo
  • 58. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Netflix Turbine ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava for aggregation of micro-service requests ● Declarative REST Client: Feign
  • 59. Netflix Zuul - Edge Server Zuul the Gatekeeper of Gozer
  • 60. The Gatekeeper of our microservices world ● Entry point to the microservices ● Zuul is a JVM based router and server side load balancer by Netflix. ● Intelligent Routing ● Using Ribbon to lookup available services and routes the external request to an appropriate service instance ● By default Zuul set up a route to every service it can find in Eureka
  • 61. Using Routings ● Routing in an integral part of a microservice architecture. For example, / -> mapped to your web application, /api/bookings -> mapped to the flight-booking service /api/coupon -> mapped to the coupon service.
  • 62. Zuul harmonically integrated with Netflix stack ● Zuul using Ribbon to locate an instance to forward to via Eureka ● All requests are executed in a hystrix command -> failures will show up in Hystrix metrics ● once the circuit is open the proxy will not try to contact the service.
  • 63. Use cases ● Use case where a UI application wants to proxy calls to one or more back end service ● Other use cases: ● Authentication ● Stress Testing ● Security ● Service Migration ● Active/Active traffic management
  • 64. Set your routings On application.yaml you can set your routings and roles In this example all services are ignored except coupon-service. [Demo]
  • 66. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Netflix Turbine ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 67. How many services being called?
  • 68. Scenario Example 1. Client calls to our app’s web service -> 2. our webservice need to request multiple micro-services-> 3. uses a callbacks interface to pass the successful result to the next web service call 4. define another success callback- > 5. and then moves on to the next web service request. Orchestrator A B C F Client Request
  • 69. Looks like that-> Also known as: “The Callback Hell”
  • 70. //The "Nested Callbacks" Way public void fetchUserDetails() { //first, request the users... mService.requestUsers(new Callback<GithubUsersResponse>() { @Override public void success(final GithubUsersResponse githubUsersResponse, final Response response) { Timber.i(TAG, "Request Users request completed"); final List<GithubUserDetail> githubUserDetails = new ArrayList<GithubUserDetail>(); //next, loop over each item in the response for (GithubUserDetail githubUserDetail : githubUsersResponse) { //request a detail object for that user mService.requestUserDetails(githubUserDetail.mLogin, new Callback<GithubUserDetail>() { @Override public void success(GithubUserDetail githubUserDetail, Response response) { Log.i("User Detail request completed for user : " + githubUserDetail.mLogin); githubUserDetails.add(githubUserDetail); if (githubUserDetails.size() == githubUsersResponse.mGithubUsers.size()) { //we've downloaded'em all - notify all who are interested!
  • 71. Async our microservices call (Using Reactor) A library for composing asynchronous and event-based programs by using observable sequences. ● Allow you to compose sequences together declaratively ● Abstracting away : ○ low-level threading ○ synchronization ○ thread-safety ○ concurrent data structures ○ non-blocking I/O.
  • 72. RxJava for the rescue ● RxJava is single jar lightweight library. ● Using the Observable abstraction and related higher-order functions. (Support Java6+) The following external libraries can work with RxJava: ● Camel RX provides an easy way to reuse any of the Apache Camel components, protocols, transports and data formats with the RxJava API ● rxjava-http-tail allows you to follow logs over HTTP, like tail -f ● mod-rxvertx - Extension for VertX that provides support for Reactive Extensions (RX) using the RxJava library And ->> Hystrix latency and fault tolerance bulkheading library. (Which is why we here)
  • 73. Using Observables “Go do this thing and when that thing is done then invoke that other code” @HystrixCommand(fallbackMethod = "stubbedBro") public Observable<String> executeObservableBro(String name) { return new ObservableResult<String>() { @Override public String invoke() { return executeRemoteService(name); } }; }
  • 74. Agenda ● Microservices - Quick overview ● Implementing microservices - Build your endpoints with Spring-Boot ● Spring Cloud and Netflix ● Service Discovery: Eureka instances ● Circuit Breaker: Hystrix ● Netflix Turbine ● Client Side Load Balancer: Ribbon ● Router and Filter: automatic registration of Zuul ● RxJava: Aggregation of multiple micro-service requests ● Declarative REST Client: Feign
  • 75. Demo
  • 76. Feign Client ● It makes writing web service clients easier ● Feign connect your code to http API’S with minimal overhead. ● Maps to the REST API methods that are exposed by a service ● Customizable decoders and error handlings ● Feign works by processing annotations into a templatized request
  • 77. Feign and Spring-Cloud ● Create interface and annotate ● Support Feign annotations and JAX-RS annotations ● Collaborated with Spring-MVC annotations(Using convertors like: HttpMessageConverters ) ● Eureka-aware REST clients that uses Ribbon for client-side load-balancing to pick an available service instance ● You can call methods on an interface instead of manually handling URLs, Spring Cloud takes care of auto-instantiating the interface implementation and will use Eureka for discovery.