spring cloud microservice architecture (3): a small test of the use of Hystrix

A series of service protection functions such as thread isolation and circuit breakers are implemented in Hystrix, which are not covered in this article, nor integrated with spring cloud. The content of this article:

  1. Basic usage of Hystrix commands
  2. Protect clients from delays in dependent services
  3. Use of fallback methods

1 service provider

First of all, it needs to be clear that Hystrix is ​​used on the client side. First, create a server-side project to provide services, a simple spring boot project.
Provide two services, normalHello can be called normally; errorHello will return the result after at least 10 seconds, Hystrix is ​​in the protection of the client, it will consider the service timed out.

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @RequestMapping(value = "/normalHello", method = RequestMethod.GET)
    public String normalHello() {
        return "Hello World";
    }

    @RequestMapping(value = "/errorHello", method = RequestMethod.GET)
    public String errorHello() throws Exception {
        Thread.sleep(10000);
        return "Error Hello World";
    }
}

2 clients

Create another client project and use Hystrix in this project. The pom dependencies are as follows:

<dependencies>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <version>1.7.25</version>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
    </dependencies>

Hystrix is ​​the implementer of the command design pattern. The command executor and the command caller are completely decoupled, and the invocation of the service is regarded as a command. If you want to call the normalHello service provided by the server, create a class that implements HystrixCommand, implement the run method, and implement the call to the service in the run method.

The client calls the service through httpclient

public class HelloCommand extends HystrixCommand<String> {

    public HelloCommand() {
        super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
    }

    protected String run() throws Exception {
        String url = "http://localhost:8080/normalHello";
        HttpGet httpget = new HttpGet(url);     
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpResponse response = httpclient.execute(httpget);
        return EntityUtils.toString(response.getEntity());
    }

}

When calling the delayed service, you need to implement another getFallback method, which is the fallback method in the previous article. When there is a problem with the called service, the fallback method is called directly.

public class ErrorCommand extends HystrixCommand<String> {

    public ErrorCommand() {
        super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
    }

    protected String run() throws Exception {
        String url = "http://localhost:8080/errorHello";
        HttpGet httpget = new HttpGet(url);     
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpResponse response = httpclient.execute(httpget);
        return EntityUtils.toString(response.getEntity());
    }

    protected String getFallback() {
        System.out.println("fall back method");
        return "fall back hello";
    }

}

3 Service call

Then, instantiate a command that calls the service and execute the command. When the ErrorCommand command is executed, because of the service delay, the getFallback method will be called directly to return the result:

public class NormalMain {

    public static void main(String[] args) {
        HelloCommand command = new HelloCommand();
        String result = command.execute();
        System.out.println(result);
    }

}
public class ErrorMain {

    public static void main(String[] args) {
        ErrorCommand command = new ErrorCommand();
        String result = command.execute();
        System.out.println(result);
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325990448&siteId=291194637