springboot中使用Hystrix做超时处理示例及问题分析 - Go语言中文社区

springboot中使用Hystrix做超时处理示例及问题分析


前言

此示例项目基于springboot 2.06 +hystrix

通过另一个项目在6543端口上开放一个接口,用于测试调用hystrix调用接口超时时候的处理策略。

具体实现

1. 开放接口

(此用于测试的接口我是在另一个springboot项目中开放的,且端口设置为6543,避免与8080端口冲突)

@RequestMapping(value = "hystrix_anther_sleep",method = RequestMethod.GET)
public String hystrixSlepp(){

    try {
        sleep(6000);
  } catch (InterruptedException e) {
        e.printStackTrace();
  }

    return "hystrix sleep 6s";
}

此接口访问地址为:http://localhost:6543/leadsscoring/hystrix_sleep。

通过在接口中sleep 6s,测试当hystrix设置超时时间超过6s和小于6s时候的处理。

2. 测试hystrix的springboot项目

1) pom.xml

pom文件与一般的springboot并无差别,只是在引入hystrix的依赖包的时候要注意一下,因为hystrix是springcloud中的微服务。我这里引入的是微服务的依赖。所以需要注明springcloud的版本。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

以上几部分的配置就是在springboot中使用springcloud组件的方式。

2) application启动类

@SpringBootApplication
@EnableCreateCacheAnnotation
@MapperScan("com.example.demo.dao")
@EnableMethodCache(basePackages = "com.example.demo")
@EnableHystrix
public class SpringbootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }
}

上面几个注解只有@EnableHystrix是我们本次测试hystrix所要用到的注解。其他是我测试其他东西所使用的注解。

3) controller

@RestController
public class HystrixController {

    @Autowired
    private IHystrixService hystrixService;
    
    @RequestMapping("get_hystrix_response")
    public String getHystrixResponse(){
        
        String another_result = hystrixService.anotherGetHystrixResponse("hdjh");
        
        return "another hystrix response:...."+another_result;
    }
    
}

4) service

@HystrixCommand(commandKey = "test2Command", groupKey = "testGroup",fallbackMethod = "TimeOutFallBack",ignoreExceptions = {Exception.class},
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "8000" )
            })
    @Override
    public String anotherGetHystrixResponse(String id) {
        
        RestTemplate restTemplate = new RestTemplate();
        
        HttpHeaders headers = new HttpHeaders();
        
        headers.add("Content-Type", "application/json; charset=UTF-8");
        headers.add("Accept", "*/*");
        
        HttpEntity<String> requestEntity = new HttpEntity<>("", headers);
        ResponseEntity<String> exchange = restTemplate.exchange("http://localhost:6543/leadsscoring/hystrix_anther_sleep", HttpMethod.GET, requestEntity,String.class);
        
        String body = exchange.getBody();
        
        return body;
        
    }
    
public String TimeOutFallBack(String id){
    return "sorry, the request is timeout";
}

service中关于hystrix中的注解功能的使用可以查看笔记学习/springCloud/@HystrixCommand和@HystrixProperty的介绍与使用。这里只做示例代码和遇到的一些问题记录。

调用接口的方式是springboot中的restTemplate提供的方法。

进行测试

因为调用的接口是延迟了6秒再返回结果,所以我们分别测试超时时间设置为4秒和8秒的结果,按照预期的结果是超时的话会执行TimeOutFallBack方法。

设置超时时间为4s,页面返回结果是:
在这里插入图片描述

设置超时时间为4s,页面返回结果是:
在这里插入图片描述

由此可见,超时限制是没有问题的。

遇到的问题

1. 问题1: 设置了超时时间却没有作用

项目启动没有报错,接口调用也没有问题。但是超时了却没有执行fallback方法。

没有在application启动类上加EnableHystrix

2. 问题2: 报错fallback method wasn’t found: TimeOutFallBack([class java.lang.String])

在这里插入图片描述

在调用使用了hystrix注解的方法时候出现了这个错误,错误提示也很明显,fallback的方法找不到,又看了一下官方的文档,

fallbackMethod:方法执行时熔断、错误、超时时会执行的回退方法,需要保持此方法与 Hystrix 方法的签名和返回值一致。

原来是fallback方法的名车与参数也要和hystrix的方法保持一致。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/nb7474/article/details/83930532
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢