当前位置: 移动技术网 > IT编程>开发语言>Java > 详解springcloud Feign的Hystrix支持

详解springcloud Feign的Hystrix支持

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文介绍了springcloud feign的hystrix支持,分享给大家,具体如下:

一、feign client中加入hystrix的fallback

@feignclient(name="springboot-h2", fallback=hystrixclientfallback.class) //在fallback属性中指定断路器的fallback 
public interface userfeignclient { 
// @getmapping("/user/{id}") 
  @requestmapping(value = "/user/{id}", method = requestmethod.get) 
  user findbyid(@pathvariable("id") long id); 
   
  @requestmapping(value="/users", method=requestmethod.get) 
  list<user> findall(); 
   
  @requestmapping(value="/post/user", method=requestmethod.post) 
  user save(@requestbody user user); 
} 

二、编写hystrixclientfallback类

@component //加入spring bean中 
public class hystrixclientfallback implements userfeignclient{ 
 
  @override 
  public user findbyid(long id) { 
    user u = new user(); 
    u.setname("临时名"); 
    u.setusername("匿名"); 
    return u; 
  } 
 
  @override 
  public list<user> findall() { 
    return null; 
  } 
 
  @override 
  public user save(user user) { 
    return null; 
  } 
} 

三、加入hystrix支持

@enablecircuitbreaker 

四、测试

不启动底层依赖的服务,直接启动服务,然后测试,发现浏览器中的结果为:

{"id":null,"username":"匿名","name":"临时名","age":null,"balance":null}   

并没有像想象中的那样报异常,而是进入了hystrixclientfallback类中的findbyid方法中。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网