SpringCloud @FeignClient参数的用法解析

 更新时间:2021年10月20日 15:52:53   作者:苦海菩提路  
这篇文章主要介绍了SpringCloud @FeignClient参数的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringCloud @FeignClient 参数详解

今天因为工作中遇到FeignClient一个奇葩的bug,后面仔细研究了,找出了原因,那么刚好对FeignClient 这个注解总结一下:

先看@FeignClient 源码:源码如下,本文最后面。

11个方法,常用方法说明如下

@FeignClient(name = "service-name", url = "${feign.urls.service-name:}", fallback =ApiFallBack.class,configuration = Interceptor.class)
  • 1.value,name 这两个就同一个意思:对应的是调用的微服务的服务名,对用服务发现、走网关调用,这个很关键。
  • 2.url 这是访问地址,可以直接提供给外部调用,也可以直接写如192.168.1.11:8800/applicationName
  • 3.fallback fallbackFactory

就给@FeignClient注解设置fallback属性,并且回退类要继承@FeignClient所注解的接口

ApiFallBack类拿出去单独作为一个类的话,我们就得在该类上添加注解@Component

如果fallback默认优先级比fallfactory优先级高。所以二者都存在的话,会访问fallback的回退方法。

这里不做演示。

那么fallback和fallfactory有什么区别呢

@FeignClient(name = "service-name", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/test")
           Hello iFailSometimes();
 }
@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
@Override
public HystrixClient create(Throwable cause) {
return new HystrixClientWithFallBackFactory() {
@Override
public Hello iFailSometimes() {
return new Hello("fallback; reason was: " + cause.getMessage());
}
};
}
}

fallback和fallfactory区别

  • fallback 只是重写了回退方法。
  • fallfactory 层面比较深,因为它用线程抛出了异常,可以看到底层具体问题。
/**
 * Annotation for interfaces declaring that a REST client with that interface should be
 * created (e.g. for autowiring into another component). If ribbon is available it will be
 * used to load balance the backend requests, and the load balancer can be configured
 * using a <code>@RibbonClient</code> with the same name (i.e. value) as the feign client.
 *
 * @author Spencer Gibb
 * @author Venil Noronha
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
 
   /**
    * The name of the service with optional protocol prefix. Synonym for {@link #name()
    * name}. A name must be specified for all clients, whether or not a url is provided.
    * Can be specified as property key, eg: ${propertyKey}.
    */
   @AliasFor("name")
   String value() default "";
 
   /**
    * The service id with optional protocol prefix. Synonym for {@link #value() value}.
    *
    * @deprecated use {@link #name() name} instead
    */
   @Deprecated
   String serviceId() default "";
 
   /**
    * The service id with optional protocol prefix. Synonym for {@link #value() value}.
    */
   @AliasFor("value")
   String name() default "";
   
   /**
    * Sets the <code>@Qualifier</code> value for the feign client.
    */
   String qualifier() default "";
 
   /**
    * An absolute URL or resolvable hostname (the protocol is optional).
    */
   String url() default "";
 
   /**
    * Whether 404s should be decoded instead of throwing FeignExceptions
    */
   boolean decode404() default false;
 
   /**
    * A custom <code>@Configuration</code> for the feign client. Can contain override
    * <code>@Bean</code> definition for the pieces that make up the client, for instance
    * {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
    *
    * @see FeignClientsConfiguration for the defaults
    */
   Class<?>[] configuration() default {};
 
   /**
    * Fallback class for the specified Feign client interface. The fallback class must
    * implement the interface annotated by this annotation and be a valid spring bean.
    */
   Class<?> fallback() default void.class;
 
   /**
    * Define a fallback factory for the specified Feign client interface. The fallback
    * factory must produce instances of fallback classes that implement the interface
    * annotated by {@link FeignClient}. The fallback factory must be a valid spring
    * bean.
    *
    * @see feign.hystrix.FallbackFactory for details.
    */
   Class<?> fallbackFactory() default void.class;
 
   /**
    * Path prefix to be used by all method-level mappings. Can be used with or without
    * <code>@RibbonClient</code>.
    */
   String path() default "";
 
   /**
    * Whether to mark the feign proxy as a primary bean. Defaults to true.
    */
   boolean primary() default true;
 
}

@FeignClient 注解常用参数

怕以后又忘记,总结下目前项目中实际用到的 @FeignClient 注解中的参数,如下:

@FeignClient(value = "annoroad-alpha",  url = "${annoroad.ms.annoroad-alpha.url}")
public interface UserFacade {
    @PostMapping(value = "/user/detail")
    UserDto detail(@RequestParam("id") long id);
}

value

  • value 等同于 name

url

  • 一般用于调试,可以手动指定 @FeignClient 调用的地址

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • springboot controller参数注入方式

    springboot controller参数注入方式

    这篇文章主要介绍了springboot controller参数注入方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • SpringBoot中RestTemplate的使用详解

    SpringBoot中RestTemplate的使用详解

    这篇文章主要介绍了SpringBoot中RestTemplate的使用详解,RestTemplate是由Spring框架提供的一个可用于应用中调用rest服务的类它简化了与http服务的通信方式,统一了RESTFul的标准,封装了http连接,我们只需要传入url及其返回值类型即可,需要的朋友可以参考下
    2023-10-10
  • SpringBoot JMX的基本使用方式

    SpringBoot JMX的基本使用方式

    这篇文章主要介绍了SpringBoot JMX的基本使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 详解如何在Java中实现屏幕共享

    详解如何在Java中实现屏幕共享

    在本文中,将为大家展示如何利用 JxBrowser 的功能,实现运行在不同电脑上的两个 Java 应用程序之间的屏幕共享,感兴趣的小伙伴可以参考一下
    2024-10-10
  • java实现潜艇大战游戏源码

    java实现潜艇大战游戏源码

    潜艇大战游戏相信大家都玩过,是一款非常有趣的小游戏,那么基于代码是如何实现的呢?今天小编给大家带来一篇教程帮助大家学习java实现潜艇大战游戏,感谢的朋友一起看看吧
    2021-06-06
  • SpringBoot文件上传功能的实现方法

    SpringBoot文件上传功能的实现方法

    这篇文章主要介绍了SpringBoot文件上传功能的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • SpringMVC 拦截器不拦截静态资源的三种处理方式方法

    SpringMVC 拦截器不拦截静态资源的三种处理方式方法

    本篇文章主要介绍了SpringMVC 拦截器不拦截静态资源的三种处理方式方法,详细的介绍了三种方法,有兴趣的可以了解一下。
    2017-01-01
  • hibernate通过session实现增删改查操作实例解析

    hibernate通过session实现增删改查操作实例解析

    这篇文章主要介绍了hibernate通过session实现增删改查操作实例解析,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • Java获取用户访问IP及地理位置的方法详解

    Java获取用户访问IP及地理位置的方法详解

    这篇文章主要介绍了Java获取用户访问IP及地理位置的方法,结合实例形式详细分析了Java基于百度地图开放平台获取用户访问IP及地理位置相关操作技巧,需要的朋友可以参考下
    2020-04-04
  • Java 离线中文语音文字识别功能的实现代码

    Java 离线中文语音文字识别功能的实现代码

    这篇文章主要介绍了Java 离线中文语音文字识别,本次使用springboot +maven实现,官方demo为springboot+gradle,结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07

最新评论