SoFunction
Updated on 2025-04-05

How to use feignClient in SpringBoot project

Use of feignClient in SpringBoot project

1. Add two configurations to it

# feign client configurationhystrix:
  threadpool:
    default:
      coreSize: 500
      maxQueueSize: -1
      queueSizeRejectionThreshold: 10000
  command:
    default:
      circuitBreaker:
        requestVolumeThreshold: 1500
      execution:
        #(Timeout time)        timeout:
          enabled: false
        # (Context pass)        isolation:
          strategy: SEMAPHORE
          semaphore:
            maxConcurrentRequests: 1500
            maxSemaphores: 1500
ribbon:
  ConnectTimeout: 50000000
  ReadTimeout: 500000000

2. Join on the main class

scanBasePackageClasses = , scanBasePackages = {""}
FeignInterceptorUsed to scan Interceptor that passes context
 {""}Used to scan自己的包
 @EnableFeignClients(On the main category)Scan to be@FeignClientAnnotated interface
  
@EnableFeignClients
@SpringBootApplication(scanBasePackageClasses = , scanBasePackages = {""})

3. Add xxxRestContextInterceptor to the interceptor

 @Configuration
public class  WebAppConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //Register custom interceptor, add intercept paths and exclude intercept paths        (new XsyRestContextInterceptor());
    }
}

4. Interface call instructions

Declare an interface such as: PaasAggFeignClient, explicitly add @FeignClient annotation to it

Fill in the registered service name on Eureka internally, for example:

@FeignClient("manager-service", configuration = )

Among them, configuration = should be added to ensure that FeignInterceptor takes effect.

Custom method, the request path is the corresponding rest request path on the service, as follows

@FeignClient("manager-service")
public interface PaasAggFeignClient {
    @RequestMapping(value = "/api/xxxx/test/description", method = )
    String testDescription();
 
    @RequestMapping(value = "/api/xxxx/test/{id}", method = )
    String testInfo(@PathVariable("id") Long id);
 
    @RequestMapping(value = "/api/xxxx/test/", method = , consumes = {MediaType.APPLICATION_JSON})
    String testCreate(@RequestBody JSONObject jsonObject);
 
    @RequestMapping(value = "/api/xxxx/test/{id}", method = )
    String testDelete(@PathVariable("id") Long id);
 
    @RequestMapping(value = "/api/xxxx/test/{id}", method = , consumes = {MediaType.APPLICATION_JSON})
    String testUpdate(@PathVariable("id") Long id, @RequestBody JSONObject jsonObject);
}

5. Introduce a custom interface into the corresponding controller to use it

@Resource
private PaasAggFeignClient paasAggFeignClient;
 
String paasAggResult = ();
 
String paasAggResult = (entity);
 
String paasAggResult = (id);
 
.....

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.