Springboot feignclient annotation and parameters

Time:2021-7-26

1、 Feignclient annotation

Feignclient annotation is modified by @ target (ElementType. Type), indicating that the function target of feignclient annotation is on the interface


@FeignClient(name = "github-client", url = "https://api.github.com", configuration = GitHubExampleConfig.class)
public interface GitHubClient {
  @RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
  String searchRepo(@RequestParam("q") String queryStr);
}

After the interface is declared, it can be used after @ resource injection in the [email protected] The common properties of feignclient tag are as follows:

  • Name: specify the name of feignclient. If the project uses ribbon, the name attribute will be used as the name of the micro service for service discovery
  • URL: URL is generally used for debugging. You can manually specify the address of @ feignclient call
  • Decode404: when an HTTP 404 error occurs, if the field bit is true, the decoder will be called for decoding, otherwise feignexception will be thrown
  • Configuration: feign configuration class. You can customize the encoder, decoder, loglevel and contract of feign
  • Fallback: defines a fault-tolerant processing class. When calling the remote interface fails or expires, the fault-tolerant logic of the corresponding interface will be called. The class specified in fallback must implement the interface marked @ feignclient
  • Fallbackfactory: factory class, which is used to generate fallback class examples. Through this attribute, we can implement the common fault-tolerant logic of each interface and reduce duplicate code
  • Path: defines the uniform prefix of the current feignclient
@FeignClient(name = "github-client",
    url = "https://api.github.com",
    configuration = GitHubExampleConfig.class,
    fallback = GitHubClient.DefaultFallback.class)
public interface GitHubClient {
  @RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
  String searchRepo(@RequestParam("q") String queryStr);
 
  /**
   *Fault tolerant processing class. When the call fails, it simply returns an empty string
   */
  @Component
  public class DefaultFallback implements GitHubClient {
    @Override
    public String searchRepo(@RequestParam("q") String queryStr) {
      return "";
    }
  }
}

When using the fallback attribute, you need to use the @ Component annotation to ensure that the fallback class is scanned by the spring container. The contents of githubexampleconfig are as follows:


@Configuration
public class GitHubExampleConfig {
  @Bean
  Logger.Level feignLoggerLevel() {
    return Logger.Level.FULL;
  }
}

When using feignclient, spring will create different ApplicationContext by name and isolate feignclient’s configuration information through different context. When using configuration class, you cannot put the configuration class under the path of spring app component scan, otherwise, the configuration class will take effect for all feignclients

2、 Feign client and @ requestmapping

If the current project has the same endpoint as the feign client, the feign client class cannot be annotated with @ requestmapping. Otherwise, 404 will be reported when the current project requests the endpoint HTTP and uses accpet
Controller:


@RestController
@RequestMapping("/v1/card")
public class IndexApi {
 
  @PostMapping("balance")
  @ResponseBody
  public Info index() {
    Info.Builder builder = new Info.Builder();
    builder.withDetail("x", 2);
    builder.withDetail("y", 2);
    return builder.build();
  }
}

Feign Client


@FeignClient(
    name = "card",
    url = "http://localhost:7913",
    fallback = CardFeignClientFallback.class,
    configuration = FeignClientConfiguration.class
)
@RequestMapping(value = "/v1/card")
public interface CardFeignClient {
 
  @RequestMapping(value = "/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
  Info info();
 
}  

if @RequestMapping is used on class, when invoke http /v1/card/balance, like this :

If the @ requestmapping annotation is used on feignclient class, when requesting / V1 / card / balance like the following code, pay attention to the accept header:


Content-Type:application/json
Accept:application/json
 
POST http://localhost:7913/v1/card/balance

Then 404 is returned.

If the request does not contain an accept header, it is OK:


Content-Type:application/json
POST http://localhost:7913/v1/card/balance

Or, for example, if the @ requestmapping annotation is not used on feign client, the request is OK, whether it contains accept or not:


@FeignClient(
    name = "card",
    url = "http://localhost:7913",
    fallback = CardFeignClientFallback.class,
    configuration = FeignClientConfiguration.class
)
 
public interface CardFeignClient {
 
  @RequestMapping(value = "/v1/card/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
  Info info();
 
}

3、 Feign request timeout problem

The default timeout time of hystrix is 1 second. If there is no response after this time, it will enter the fallback code. The first request is often slow (because spring’s lazy loading mechanism needs to instantiate some classes), and the response time may be greater than 1 second
There are three solutions. Take feign as an example.

Method 1
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
This configuration is to change the timeout of hystrix to 5 seconds

Method 2
hystrix.command.default.execution.timeout.enabled: false
This configuration is used to disable the timeout of hystrix

Method 3
feign.hystrix.enabled: false
This configuration is used to simply disable festrix of feign. This method is not recommended except for some special scenarios.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.