Usage Analysis of SpringCloud @ FeignClient Parameter

  • 2021-11-29 23:48:44
  • OfStack

Directory SpringCloud @ FeignClient Parameter Details @ FeignClient Annotation Common Parameters

Detailed explanation of SpringCloud @ FeignClient parameters

Today, because I encountered a wonderful bug of FeignClient1 in my work, I studied it carefully and found out the reason, so I just summarized the annotation of FeignClient 1:

Look at @ FeignClient source code first: the source code is as follows, at the end of this article.

11 methods, commonly used methods are described as follows


@FeignClient(name = "service-name", url = "${feign.urls.service-name:}", fallback =ApiFallBack.class,configuration = Interceptor.class)
1. value , name These two have the same meaning: they correspond to the service name of the called microservice, which is very important for service discovery and gateway call. 2. url This is the access address, which can be provided directly to external calls or written directly as 192.168. 1.11: 8800/applicationName 3. fallback And fallbackFactory

Set the fallback property for the @ FeignClient annotation, and the fallback class inherits the interface annotated by @ FeignClient

If the ApiFallBack class is taken out as a separate class, we have to add the annotation @ Component to the class

If the fallback default priority is higher than the fallfactory priority. Therefore, if both exist, the fallback method of fallback will be accessed.

There is no demonstration here.

So what's the difference between fallback and 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());
}
};
}
}

Difference between fallback and fallfactory

fallback It just overrides the fallback method. fallfactory The level is deeper, because it throws exceptions with threads, and you can see the underlying specific problems.

/**
 * 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 annotation common parameters

For fear of forgetting in the future, summarize the parameters in the @ FeignClient annotation actually used in the current project, as follows:


@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 is equivalent to name

url

1 is used for debugging, and you can manually specify the address of @ FeignClient call

Related articles: