赞
踩
目录
2.3在需要进行错误降级的方法上添加@HystrixCommand
整合hystrix时还是使用的前一篇整合feign创建的项目(具体见:)springcloud-feign-consumer,端口9320
涉及到的文件
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
- </dependency>
注意:回调方法getFallback需要和原方法get有相同的方法签名,入参、返回值等都需要相同
只启动eureka服务和springcloud-feign-consumer服务
启动服务实例如下:
访问http://localhost:9320/feignConsumer/get返回的正是回调方法中定义的内容。测试成功
由于feign中已经包含了hystrix,所以不需要额外引入hystrix相关依赖。
还是在springcloud-feign-consumer中测试
因为前面测试了单独使用hystrix的方式,所以在springcloud-feign-consumer中需要去除
1. pom.xml中单独引入的hystrix依赖
2.主启动类去除@EnableHystrix
3.controller中去除@HystrixCommand
spring-cloud-starter-openfeign
2.2.6.RELEASE及之前版本为feign.hystrix.enabled
2.2.7.RELEASE及之后版本为feign.circuitbreaker.enabled
具体配置可以查看FeignClientsConfiguration
在整合feign和hystrix时发现,使用网上的配置feign.hystrix.enabled=true不生效,通过查看源码,发现配置方式有改动。
本次使用的springcloud版本为Hoxton.SR10,其中spring-cloud-starter-openfeign版本为2.2.7.RELEASE
application.yml
- feign:
- circuitbreaker:
- enabled: true
CommonFallback中定义回调方法,实现需要被代理的接口
CommonFeignService为需要feign代理的接口,在其中添加fallback回调类(刚刚编写的)
只启动eureka服务和springcloud-feign-consumer服务
启动服务实例如下:
访问http://localhost:9320/feignConsumer/get返回的正是回调类CommonFallback中定义的内容。测试成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。