栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

通过Hystrix熔断降级

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Hystrix熔断降级

当服务性能下降时,避免连累其他服务的可用性,此时可以采用降级策略。

1. 引入依赖

        org.springframework.cloud

        spring-cloud-starter-hystrix

        Edgware.RELEASE

2. 开启熔断@EnableHystrix

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

3. 使用注解,定义降级方法

@RequestMapping(value="/")
@HystrixCommand(fallbackMethod="fallback_hello", commandProperties={
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1000")
})
public String hello() throws InterruptedException {
    Thread.sleep(3000);
    return "Welcome Hystrix";
}

// 降级方法
private String fallback_hello() {
    return "Request fails. It takes long time to response";
}

 此处刻意睡眠3秒钟造成超时场景,会自动调用fallback_hello方法进行降级处理

@HystrixCommand是降级注解,这里定义了fallbackMethod即降级方法,当服务不可用时,会调用降级方法。

commandProperties是降级的一个属性,使用@HystrixProperty定义了一个属性,即超时时间,name是execution.isolation.thread.timeoutInMilliseconds, 值为1000毫秒,即1秒。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号