SpringCloud@FeignClient参数的⽤法解析
⽬录
SpringCloud@FeignClient参数详解
@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: " + 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
* 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    * {@dec.Decoder}, {@dec.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>.
*/springcloud怎么读音
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 调⽤的地址
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。