hystrix 介绍
TRANSCRIPT
Hystrix 介绍晓丹 2015/05/29
What ?
• https://github.com/Netflix/Hystrix
• Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
Why ?
• 故障是常态
• 远程服务和第三⽅方类库不可靠
• 故障蔓延,恢复延⻓长
How ?• Command
• Thread Pool/Semaphore
• Fallback
• Timeout
• Circuit Breaker
• ……
Flow
https://raw.githubusercontent.com/wiki/Netflix/Hystrix/images/hystrix-command-flow-chart.png
Basic
• Hystrix-clj
• (defcommand hello [name] (str “hello,” name))
• defcommand vs. defn
Basic
• Synchronous: (hello “dennis”) => hello,dennis
• Asynchronous: (queue #’hello “dennis”)
• RxJava: (observe #’hello “dennis”) => rx.Observable
Configuration(defcommand hello
{:hystrix/group-key “CloudCode”
:hystrix/command-key “beforeAction”
:hystrix/thread-pool-key “CloudCode”}
[name]
(str “hello,” name))
Command Group
• Group together commands such as for reporting, alerting, dashboards, or team/library ownership.
• By default Hystrix group key to define the command thread-pool unless a separate one is defined.
execution.isolation.strategy
• Thread vs. Semaphore
• coreSize/maxQueueSize vs. maxConcurrentRequests
Fallback
(defcommand hello {:hystrix/fallback-fn (fn [name] (str “hello from fallback,” name))} [name] (throw (ex-info “test” {})
Fallback: Primary + Secondary
Request Cache
Request Cache
(defcommand hello
{:hystrix/cache-fn (fn [name] name)}
[name]
(println name)
(str “hello,” name))
Request Collapsing
Request Collapsing(defcollapser search
"Collapsing single-term search command"
(collapse [arg-lists]
; Create a multi-search command, passing individual terms as a seq of args
(instantiate multi-search (map first arg-lists)))
(map [arg-lists batch-result]
; Map from input args to results. Here we assume order is preserve by
; multi-search so we can return the result list directly
batch-result))
(search “Something1”)
(search “Something2")
Circuit Breaker
• Tripping a circuit-breaker to stop all requests to a particular service for a period of time, either manually or automatically if the error percentage for the service passes a threshold.
Circuit Breaker
Configuration
• Global default from code
• Dynamic global default property
• Instance default from code
• Dynamic instance property
• https://github.com/Netflix/archaius
Dashboard• https://github.com/Netflix/Hystrix/wiki/Metrics-and-Monitoring
• https://github.com/Netflix/Hystrix/wiki/Dashboard
Patterns• Fail Fast
• Fail Silent
• Fallback
• static
• stubbed
• cache vs. network
• Primary + secondary
• https://github.com/Netflix/Hystrix/wiki/How-To-Use#Common-Patterns
The End