Hystrix-go Introduction

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Hystrix is a fault-tolerant library designed to isolate requests to remote systems, services, and third-party libraries, eliminate cascading failures, and achieve resiliency in complex distributed systems, where failures are unavoidable.

This project originated from Netflix's Open source Java project with the same name. Https://github.com/Netflix/Hystrix

Execute code just like the Hystrix command

Define the application logic that relies on the external system and pass the function on Go . When the external system is in a healthy state, this function will be the only code executed.

hystrix.Go("my_command", func() error {// talk to other servicesreturn nil}, nil)

Defining fallback behavior

If you want to perform some action when the external system hangs, you can Go pass the second function. Ideally, the logic here will allow your app to gracefully handle situations where external systems are not available.

Fallback is triggered when the first function returns an error, or if the function fails to run at the end of a series of health checks. A more detailed reference is here

hystrix.Go("my_command", func() error {// talk to other servicesreturn nil}, func(err error) error {// do this when services are downreturn nil})

Wait for output

The call Go is like executing a goroutine, except you can get an error channel and monitor it.

output := make(chan bool, 1)errors := hystrix.Go("my_command", func() error {// talk to other servicesoutput <- truereturn nil}, nil)select {case out := <-output:// successcase err := <-errors:// failure}

Synchronization API

Invoking an excuse and waiting for the return to be a common scenario (corresponding to Goroutine), Hystrix provides a Do function that returns an error

err := hystrix.Do("my_command", func() error {// talk to other servicesreturn nil}, nil)

Configuration

During app startup, you can call ConfigureCommand to add a configuration for each command:

hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{Timeout:               1000,MaxConcurrentRequests: 100,ErrorPercentThreshold: 25,})

There are other configuration methods, please refer to the official documentation for more detailed information.

An example

Finally, let me give you an example.

package mainimport ("fmt""github.com/afex/hystrix-go/hystrix""net/http""time")func main() {hystrix.Go("get_baidu", func() error {// talk to other services_, err := http.Get("https://www.baidu.com/")if err != nil {fmt.Println("get error")return err}return nil}, func(err error) error {fmt.Println("get an error, handle it")return nil}) time.Sleep(2 * time.Second)  // 调用Go方法就是起了一个goroutine,这里要sleep一下,不然看不到效果}

Network requests, you disconnect the network, you can simulate the situation of external services hanging off.

Summarize

The fusing mechanism is almost an essential component in a distributed system, as summarized below:

Characteristics

    1. Hystrix acting on the client side, the client program relies on the Hystrix related third-party package, which causes the client to isolate itself from the services it relies on (goroutine isolation). Latency and failure of dependent services can be controlled. Protects the execution of the caller goroutine.

    2. Avoids cascading effects caused by the failure of individual components in a distributed system.

    3. Rapid failure, rapid recovery. Hystrix has a fast failure mechanism, a single component service failure rate to a certain extent, then request, will respond directly to failure. After this, there will be a retry mechanism. Reduce the overhead of the system on the wrong service call.

    4. Downgrade Apps

Design principles of Hystrix

    1. Prevents any single dependent service from exhausting all user threads

    2. Respond directly to failure rather than waiting

    3. Provides an error return interface instead of having the user thread handle the exception thrown by the dependent service directly

    4. Use isolation or fusing technology to reduce and limit the impact of individual dependencies on the entire system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.