SpringCloud@FeignClient参数详解
SpringCloud @FeignClient 参数详解
今天因为⼯作中遇到FeignClient⼀个奇葩的bug,后⾯仔细研究了,找出了原因,那么刚好对FeignClient 这个注解总结⼀下: 1、先看@FeignClient 源码: 源码如下,本⽂最后⾯.
relief11个⽅法,常⽤⽅法说明如下
@FeignClient(name = "rvice-name", url = "${feign.urls.rvice-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 = "rvice-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 cau) {
return new HystrixClientWithFallBackFactory() {
@Override
public Hello iFailSometimes() {
return new Hello("fallback; reason was: " + Message());
}
};
}
}
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
* ud 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 rvice 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 rvice id with optional protocol prefix. Synonym for {@link #value() value}.
*
* @deprecated u {@link #name() name} instead
*/
@Deprecated
String rviceId() default "";
傻瓜英文/**
* The rvice 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.三的英文
*/patient
city bus
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 fal;
emba条件/**
* 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
* {@dec.Decoder}, {@dec.Encoder}, {@link feign.Contract}.
*
* @e 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. */
spermatozoonClass<?> fallback() default void.class;
/**
* Define a fallback factory for the specified Feign client interface. The fallback
* factory must produce instances of fallback class that implement the interface * annotated by {@link FeignClient}. The fallback factory must be a valid spring
* bean.
*
* @e feign.hystrix.FallbackFactory for details.
*/
Class<?> fallbackFactory() default void.class;
/**
* Path prefix to be ud by all method-level mappings. Can be ud with or without * <code>@RibbonClient</code>.
惠州翻译
hauber*/
String path() default "";
/**
* Whether to mark the feign proxy as a primary bean. Defaults to true.
*/
boolean primary() default true;
}