DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Migrating From Hystrix to Sentinel Tutorial

Migrating From Hystrix to Sentinel Tutorial

As Netflix will no longer be supporting the open source Hystrix platform, we take a look at how to move a Hystrix based app to Sentinel, another open source framework.

Leona Zhang user avatar by
Leona Zhang
·
Jan. 23, 19 · Tutorial
Like (7)
Save
Tweet
Share
8.74K Views

Join the DZone community and get the full member experience.

Join For Free

As microservices become more popular, the stability between services becomes more and more important. Technologies such as flow control, fault tolerance, and system load protection are widely used in microservice systems to improve the robustness of the system and guarantee the stability of the business and to minimize system outages caused by excessive access traffic and heavy system load.

Hystrix, an open source latency and fault tolerance library of Netflix, has recently announced on its GitHub homepage that new features are no longer under development. It is recommended that developers use other open source projects that are still active. So what are the alternatives?

Last time, we introduced two alternatives in the article, "Resilience4j and Sentinel: Two Open-Source Alternatives to Netflix Hystrix."

This article will help you migrate from Hystrix to Sentinel and help you get up to speed on using Sentinel.

Feature in Hystrix Migration Solution Feature in Sentinel
Thread Pool Isolation / Semaphore Isolation Sentinel does not support thread pool isolation. In Sentinel, flow control in thread count mode represents semaphore isolation. If you are using semaphore isolation, you can simply add flow rules for the target resource. Thread Count Flow Control
Circuit Breaker Sentinel supports circuit breaking by average response time, exception ratio, and exception count. If you want to use circuit breaking in Sentinel, you cam simply configure degrade rules for the target resource. Circuit breaking with various strategy
Command Definition You can define your resource entry (similar to command key) via the SphU API in Sentinel. Resource definition and rule configuration are separate. Resource Entry Definition
Command Configuration Rules can be hardcoded through the xxxRuleManager API in Sentinel, and multiple dynamic rule data sources are also supported. Rule Configuration
HystrixCommand annotation Sentinel also provides annotation support (SentinelResource), which is easy to use. SentinelResource annotation
Spring Cloud Netflix Sentinel provides out-of-box integration modules for Servlet, Dubbo, Spring Cloud, and gRPC. If you were using Spring Cloud Netflix previously, it's easy for you to migrate to Spring Cloud Alibaba. Spring Cloud Alibaba

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();

Sentinel does not specify an execution model, nor does it care how the code is executed. In Sentinel, what you should do is just to wrap your code with the Sentinel API to define resources:

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();
    }
}

In Hystrix, you usually have to configure rules when the command is defined. In Sentinel, resource definitions and rule configurations are separate. Users first define resources for the corresponding business logic through the Sentinel API, and then configure the rules when needed. For details, please refer to this document.

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 the 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;
    }
}

In Sentinel, semaphore isolation is provided as a mode of flow control (thread count mode), so you only need to configure the flow rule for the resource:

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

If you are using the Sentinel dashboard, you can also easily configure the rules in the dashboard.

Circuit Breaking

Hystrix circuit breaker supports error percentage mode. Related properties:

  1. circuitBreaker.errorThresholdPercentage: the threshold.
  2. circuitBreaker.sleepWindowInMilliseconds: the sleep window when the circuit breaker is open.

For example:

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";
    }
}

In Sentinel, you only need to configure circuit breaking rules for resources that want to be automatically degraded. For example, the rules corresponding to the Hystrix example above:

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));

If you are using the Sentinel dashboard, you can also easily configure the circuit breaking rules in the dashboard.

In addition to the exception ratio mode, Sentinel also supports automatic circuit breaking based on average response time and minute exceptions.

Annotation Support

Hystrix provides annotation support to encapsulate a 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");
}

Hystrix rule configuration is bundled with command execution. We can configure rules for command in the commandProperties property of the @HystrixCommand annotation, such as:

@HystrixCommand(commandProperties = {
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50")
    })
public User getUserById(String id) {
    return userResource.getUserById(id);
}

Using Sentinel annotations is similar to Hystrix, as follows:

  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.

For the details, you can refer to the annotation support document. An example for Sentinel annotation:

// 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");
}

Then configure the rules:

  1. Via the 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));
  2. Via the 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 configurations. 

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 an 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.

Spring Cloud Isolation (database systems) Thread pool Flow control (data) Open source Spring Framework Annotation Command (computing) Circuit Breaker Pattern Flow (web browser)

Published at DZone with permission of Leona Zhang. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Bye Bye, Regular Dev [Comic]
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Mr. Over, the Engineer [Comic]
  • Upgrade Guide To Spring Data Elasticsearch 5.0

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: