springCloud feign开启hystrix

在做springcloud的feign整合的时候,学习到熔断机制,hystrix实现了熔断机制,而springcloud的feign默认整合了hystrix,所以添加feign依赖后就不用在添加hystrix,那么怎么才能让feign的熔断机制生效呢,官方称默认fegin的hystrix已经生效了只要按一下步骤做:

1.feign注解增加fallback属性

@FeignClient(name = "这里写服务名称",fallback ="UserServiceAPIFallback .class")
public interface UserServiceAPI {
    @RequestMapping(method = RequestMethod.GET, value = "/user/getOne/{id}")
    User getOne(@PathVariable("id") Integer id);
}

 2.创建fallback类,注意fallback类要实现feignClient注解了的接口

/**
 * UserServiceClient服务器调用失败的退回类
 * @author gshen
 *
 */
@Component
public class UserServiceAPIFallback implements UserServiceAPI {
	@Override
	public User getOne(Integer id) {
		User user = new User();
		user.setId(-1);
		user.setUsername("默认用户");
		return user;
	}

}

 启动后,停掉userService的服务,显示找不到对应userService,这说明熔断没起作用

后来查阅官方资料发现,默认开启熔断更改为关闭熔断了,具体原因请看:https://github.com/spring-cloud/spring-cloud-netflix/issues/1277

官方提供开启开关

#开启feign熔断
feign:
  hystrix:
    enabled: true   

 启动服务发现返回结果成功

{
	"id": -1,
	"username": "默认用户",
	"password": null,
	"creater": null,
	"createTime": null,
	"updater": null,
	"updateTime": null,
	"salt": null,
	"nickname": null,
	"state": "1",
	"plainPassword": null,
	"roles": []
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值