当前位置 : 主页 > 编程语言 > java >

SpringCloud集成Hystrix熔断过程分步分解

来源:互联网 收集:自由互联 发布时间:2023-01-30
版本: SpringBoot 2.6.1 SpringCloud 2021.0.0 依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix/artifactIdversion2.1.6.RELEASE/version/dependency 贴@EnableHystrix注解 @EnableHystrix

版本:

SpringBoot 2.6.1

SpringCloud 2021.0.0

依赖

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

贴@EnableHystrix注解

@EnableHystrix
@SpringBootApplication
public class ConsumerOneApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerOneApplication.class, args);
    }
}

在需要熔断的接口上贴@HystrixCommand注解

@RequestMapping("/consumerOne")
@RestController
public class ControllerOne {
    @Autowired
    private RestTemplate restTemplate;
    private String providerOneName = "provider-one";
    @HystrixCommand(fallbackMethod = "fallbackMethodOne")
    @RequestMapping("/serviceOne/{msg}")
    public String serviceOne(@PathVariable("msg") String msg) {
        return restTemplate.getForObject("http://" + providerOneName + "/providerOne/serviceOne/" + msg, String.class);
    }
	/** 熔断回调方法 */
    private String fallbackMethodOne(String msg) {
        return "熔断一默认返回:" + msg;
    }
}

异常熔断测试

其中一个服务方抛异常,另一服务方正常返回

@RequestMapping("/providerOne")
@RestController
public class ControllerOne {
    @RequestMapping("/serviceOne/{msg}")
    public String serviceOne(@PathVariable("msg") String msg) {
        throw new RuntimeException();
        // return "8702:" + msg;
    }
}
@RequestMapping("/providerOne")
@RestController
public class ControllerOne {
    @RequestMapping("/serviceOne/{msg}")
    public String serviceOne(@PathVariable("msg") String msg) {
        return "8701:" + msg;
    }
}

访问结果:

超时熔断测试

全局

先配置默认超时时间为3秒(default为全局)

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000

在这里插入代码片

其中一个接口睡眠4秒

@RequestMapping("/providerOne")
@RestController
public class ControllerOne {
    @RequestMapping("/serviceOne/{msg}")
    public String serviceOne(@PathVariable("msg") String msg) {
        try {
            Thread.sleep(4 * 1000);
            return "8702:" + msg;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "8702:" + msg;
    }
}

访问结果:

当服务方为延时返回的那个时,访问等待了3秒返回熔断默认对象

另一个正常

单个接口

除了全局外,另外给serviceTwo接口配置独立的超时时间,其余不变

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
    serviceTwo:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000

增加一个路径为serviceTwo的接口

@RequestMapping("/consumerOne")
@RestController
public class ControllerOne {
    @Autowired
    private RestTemplate restTemplate;
    private String providerOneName = "provider-one";
    @HystrixCommand(fallbackMethod = "fallbackMethodOne")
    @RequestMapping("/serviceOne/{msg}")
    public String serviceOne(@PathVariable("msg") String msg) {
        return restTemplate.getForObject("http://" + providerOneName + "/providerOne/serviceOne/" + msg, String.class);
    }
    @HystrixCommand(fallbackMethod = "fallbackMethodOne")
    @RequestMapping("/serviceTwo/{msg}")
    public String serviceTwo(@PathVariable("msg") String msg) {
        return restTemplate.getForObject("http://" + providerOneName + "/providerOne/serviceOne/" + msg, String.class);
    }
    private String fallbackMethodOne(String msg) {
        return "熔断一默认返回:" + msg;
    }
}

测试结果:

一个为等待5秒返回结果

一个正常返回

到此这篇关于SpringCloud集成Hystrix熔断过程分步分解的文章就介绍到这了,更多相关SpringCloud Hystrix熔断内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

上一篇:Java类加载器ClassLoader源码层面分析讲解
下一篇:没有了
网友评论