Hystrix

This component allows you to wrap your network calls with the Hystrix library. Hystrix is a library that add latency tolerance and fault tolerance logic, as well as metrics.

<dependency>
    <groupId>org.seedstack.addons.netflix</groupId>
    <artifactId>netflix-hystrix</artifactId>
</dependency>
Show version
dependencies {
    compile("org.seedstack.addons.netflix:netflix-hystrix:1.2.2")
}

For more information on Hystrix: https://github.com/Netflix/Hystrix/wiki

Configuration

All Hystrix configuration is based on properties that can be specified in SeedStack configuration:

netflix:
  hystrix:
    properties:
      threadpool.default.coreSize: 10
      threadpool.default.maxQueueSize: 100
      threadpool.default.queueSizeRejectionThreshold: 50
      ...

To dump the netflix.hystrix configuration options:

mvn -q -Dargs="netflix.hystrix" seedstack:config

Note that the hystrix. prefix found in all properties from the Hystrix documentation must be removed.

Usage

Simply annotate a method with @HystrixCommand, and it will automatically be wrapped in an Hystrix command. Hystrix command are often used in business services implementations depending on the network:

@Service
public interface SomeService {
	String sayHello(String name);
}

And:

public class SomeServiceImpl implements SomeService {
    @HystrixCommand
    public String sayHello(String name) {
        // A real service would have a network call here
        return "Hello " + name + "!"; 
    }
}

Method interception is used to wrap the method in an Hystrix command. Beware of SeedStack method interception limitations.

The method can then be used like usual in other parts of the application:

public class SomeClass {
    @Inject
    private SomeService someService;
    
    public void doSomething() {
        someService.sayHello("world");
    }
}

Fallback

To make use of the Hystrix fallback mecanism, add a fallback method to the same class and reference its name from the annotation:

public class SomeServiceImpl implements SomeService {
    @HystrixCommand(fallbackMethod = "sayHelloFallback")
    public String sayHello(String name) {
        // a real service would have a network call here
        return "Hello " + name + "!"; 
    }
    
    public String sayHelloFallback(String name) {
        return "Failed to say hello to " + name + "!";
    }
}

The nominal and fallback methods must be in the same class and have the same signature. The fallback method could be a Hystrix command as well, but this is not recommended.

Asynchronous execution

To process a Hystrix command asynchronously, your return type must be a Future:

public class SomeServiceAsyncImpl implements SomeService {
    @HystrixCommand
    public Future<String> sayHello(String name) {
        // a real service would have a network call here
        ExecutorService executor = ...
        return executor.submit(() -> "Hello async " + name + "!");
    }
}

Annotation parameters

You can set the commandKey and groupKey values in the @HystrixCommand annotation. These values are used to name commands and group of commands for the purpose of configuration, reporting, alerting, statistics, etc. For more information, see the documentation on Hystrix wiki.

   

On this page


Edit this page