Spring Cloud Learning | 第六篇:在Feign中使用断路器

一. 准备工作

      在上一篇文章我们已经了解了怎么在Ribbon中使用断路器,而在实际工作中我们更多的是使用Feign,所以我们也需要知道如何在Feign中使用断路器。

      首先准备好第四篇文章里的项目工程,同样启动eureka-server和eureka-client工程,端口号分别为8761和8762。

二. 快速入门

      Feign是自带断路器的,所以不需要再引入依赖,但是它默认是没有打开的,因此需要在service-feign工程的配置文件中配置打开它:

feign.hystrix.enabled=true
      修改HelloService接口,直接在@FeignClient注解上添加fallback属性,值得注意的是这里是指定故障时回调的类,此类需要实现HelloService接口:
@FeignClient(value = "hello-service", fallback = HelloServiceHystrix.class)
public interface HelloService {

	@GetMapping("/hello")
	String sayHello(@RequestParam(value = "name") String name);
	
}
      接着实现上面fallback指定的类:
@Component
public class HelloServiceHystrix implements HelloService {

	@Override
	public String sayHello(String name) {
		return "hello " + name + ", error!";
	}

}

      到此改造完成,启动service-feigin工程,端口号为8765。

      访问http://localhost:8765/hello?name=Jack看到

Hello Jack. I am from port:8762

      关闭eureka-client工程,再次访问http://localhost:8765/hello?name=Jack可以看到

hello Jack, error!

      证明断路器已开始工作。


      源码下载:https://github.com/lingd3/SpringCloudLearning/tree/master/chapter6







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值