这篇文章主要介绍Open-Feign整合hystrix降级熔断的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
application.yml
server:
port: 9000
spring:
application:
name: my-test2 #服务的名称
@RestController
public class ShoppingController {
@RequestMapping("/myTestBuy2")
public String myTestBuy2(){
//用来模拟服务超时
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "购买成功——时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}
<!--Open-Feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
server:
port: 8000
spring:
application:
name: my-test1 #服务的名称
#允许服务降级配置
feign:
hystrix:
enabled: true
#自定义ribbon的超时时间 设置的要比hystrix-timeoutInMilliseconds超时时间大
ribbon:
#指的是建立连接后从服务器读取到可用资源所用的时间。
ReadTimeout: 10000
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间,处理请求的超时时间,默认为5秒。
ConnectTimeout: 10000
hystrix:
command:
default:
execution:
isolation:
thread:
#feign整合hystrix 光设置Hystrix超时没用的 要配合ribbon超时
timeoutInMilliseconds: 5000
my-test2:
url: http://127.0.0.1:9000
@SpringBootApplication
@EnableFeignClients//开启open-feign
@EnableHystrix//开启降级熔断服务
public class MyTestApplication1 {
public static void main(String[] args) {
SpringApplication.run(MyTestApplication1.class,args);
}
}
@RestController
public class TestController1 {
@Autowired
TestService1 testService1;
@RequestMapping("/myTestBuy1")
public String myTestBuy2(){
return testService1.myTestBuy2();
}
}
@Component
public class MyHystrix1 implements TestService1 {
@Override
public String myTestBuy2() {
return "调用失败,该服务被熔断——时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}
@FeignClient(name = "my-test2", url = "${my-test2.url}", fallback = MyHystrix1.class)
public interface TestService1 {
@RequestMapping("/myTestBuy2")
String myTestBuy2();
}
只给两秒的时间,则自动启动熔断
熔断时间根据hystrix设置的时间,我这里设置的是5秒
以上是“Open-Feign整合hystrix降级熔断的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。