spring cloud微服务架构(三):Hystrix的使用之小试牛刀

在Hystrix中实现了线程隔离、断路器等一系列的服务保护功能,本文都没有涉及,也没有和spring cloud进行整合,本文内容:

  1. Hystrix命令的基本使用
  2. 当依赖的服务发生延迟时,对客户端进行保护
  3. 回退方法的使用

1 服务提供端

首先需要明确一点,Hystrix是在客户端使用的,先创建一个服务端项目,提供服务,一个简单的spring boot项目即可。
提供两个服务,normalHello可以正常调用;errorHello至少10秒后才会返回结果,Hystrix处于对客户端的保护,会认为该服务超时。

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 客户端

再创建一个客户端项目,在该项目中使用Hystrix,pom依赖如下:

<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是命令设计模式的实现者,命令执行者和命令调用者完全解耦,将服务的调用作为一个命令。如果想要调用服务器提供的normalHello服务,创建一个实现HystrixCommand的类,并实现run方法,在run方法中实现对服务的调用。

客户端通过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());
    }

}

当调用延迟的服务时,需要再实现一个getFallback方法,就是上篇文章中的回退方法,当被调用的服务出现问题时,直接调用回退方法。

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 服务调用

然后,实例化一个调用服务的命令,执行命令即可。执行ErrorCommand命令时,因为服务延迟,会直接调用getFallback方法返回结果:

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

}

猜你喜欢

转载自blog.csdn.net/disiwei1012/article/details/80203125
今日推荐