Guideline:Migration from Hystrix to Sentinel

HystrixCommand

The execution model of Hystrix is designed with a command pattern, HystrixCommand, which encapsulates the business logic and fallback logic into a single command object (HystrixCommand / HystrixObservableCommand). A simple example:

public class SomeCommand extends HystrixCommand<String> {    public SomeCommand() {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("SomeGroup"))
// command key
.andCommandKey(HystrixCommandKey.Factory.asKey("SomeCommand"))
// command configuration
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withFallbackEnabled(true)
));
}
@Override
protected String run() {
// business logic
return "Hello World!";
}
}
// The execution model of Hystrix
// sync mode:
String s = new SomeCommand().execute();
// async mode (managed by Hystrix):
Observable<String> s = new SomeCommand().observe();
Entry entry = null;
try {
entry = SphU.entry("resourceName");
// your business logic here
return doSomeThing();
} catch (BlockException ex) {
// handle rejected
} finally {
if (entry != null) {
entry.exit();
}
}

Thread Pool Isolation

The advantage of thread pool isolation is that the isolation is relatively thorough, and it can be processed for the thread pool of a resource without affecting other resources. But the drawback is that the number of threads is large, and the overhead of thread context switching is very large, especially for low latency invocations. Sentinel does not provide such a heavy isolation strategy, but provides a relatively lightweight isolation strategy — thread count flow control as semaphore isolation.

Semaphore Isolation

Hystrix’s semaphore isolation is configured at Command definition, such as:

public class CommandUsingSemaphoreIsolation extends HystrixCommand<String> {    private final int id;    public CommandUsingSemaphoreIsolation(int id) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("SomeGroup"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)
.withExecutionIsolationSemaphoreMaxConcurrentRequests(8)));
this.id = id;
}
@Override
protected String run() {
return "result_" + id;
}
}
FlowRule rule = new FlowRule("doSomething") // resource name
.setGrade(RuleConstant.FLOW_GRADE_THREAD) // thread count mode
.setCount(8); // max concurrency
FlowRuleManager.loadRules(Collections.singletonList(rule)); // load the rules

Circuit Breaking

Hystrix circuit breaker supports error percentage mode. Related properties:

  1. circuitBreaker.errorThresholdPercentage: the threshold
  2. circuitBreaker.sleepWindowInMilliseconds: the sleep window when circuit breaker is open
public class FooServiceCommand extends HystrixCommand<String> {    protected FooServiceCommand(HystrixCommandGroupKey group) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("OtherGroup"))
// command key
.andCommandKey(HystrixCommandKey.Factory.asKey("fooService"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(500)
.withCircuitBreakerRequestVolumeThreshold(5)
.withCircuitBreakerErrorThresholdPercentage(50)
.withCircuitBreakerSleepWindowInMilliseconds(10000)
));
}
@Override
protected String run() throws Exception {
return "some_result";
}
}
DegradeRule rule = new DegradeRule("fooService")
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) // exception ratio mode
.setCount(0.5) // ratio threshold (0.5 -> 50%)
.setTimeWindow(10); // sleep window (10s)
// load the rules
DegradeRuleManager.loadRules(Collections.singletonList(rule));

Annotation Support

Hystrix provides annotation support to encapsulate command and configure it. Here is an example of Hystrix annotation:

// original method
@HystrixCommand(fallbackMethod = "fallbackForGetUser")
User getUserById(String id) {
throw new RuntimeException("getUserById command failed");
}
// fallback method
User fallbackForGetUser(String id) {
return new User("admin");
}
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50")
})
public User getUserById(String id) {
return userResource.getUserById(id);
}
  1. Add the annotation support dependency: sentinel-annotation-aspectj and register the aspect as a Spring bean (if you are using Spring Cloud Alibaba then the bean will be injected automatically);
  2. Add the @SentinelResource annotation to the method that needs flow control and circuit breaking. You can set fallback or blockHandler functions in the annotation;
  3. Configure rules
// original method
@SentinelResource(fallback = "fallbackForGetUser")
User getUserById(String id) {
throw new RuntimeException("getUserById command failed");
}
// fallback method (only invoked when the original resource triggers circuit breaking); If we need to handle for flow control / system protection, we can set `blockHandler` method
User fallbackForGetUser(String id) {
return new User("admin");
}
  1. via API (e.g. DegradeRuleManager.loadRules(rules) method)
  • DegradeRule rule = new DegradeRule("getUserById") .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) // exception ratio mode .setCount(0.5) // ratio threshold (0.5 -> 50%) .setTimeWindow(10); // sleep window (10s) // load the rules DegradeRuleManager.loadRules(Collections.singletonList(rule));
  1. via Sentinel dashboard

Integrations

Sentinel has integration modules with Web Servlet, Dubbo, Spring Cloud and gRPC. Users can quickly use Sentinel by introducing adapter dependencies and do simple configuration. If you have been using Spring Cloud Netflix before, you may consider migrating to the Spring Cloud Alibaba.

Dynamic Configuration

Sentinel provides dynamic rule data-source support for dynamic rule management. The ReadableDataSource and WritableDataSource interfaces provided by Sentinel are easy to use.
The Sentinel dynamic rule data-source provides extension module to integrate with popular configuration centers and remote storage. Currently, it supports many dynamic rule sources such as Nacos, ZooKeeper, Apollo, and Redis, which can cover many production scenarios.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com