SoFunction
Updated on 2025-03-08

FeignClient supports dynamically specifying URL methods at runtime

FeignClient supports dynamically specifying URLs at runtime

In actual development

We often call the three-party API through the FeignClient interface. When we face different environments, we can switch the corresponding addresses through the configuration file and placeholder.

The code snippet is as follows:

@FeignClient(name = "hongxuan-service", url = "${}")
public interface FeignService {
    //Relevant API interface method}


api:
  xxx: 
    url: http://127.0.0.1:8080

But when we need to call the same interface with different addresses in the same environment, this method fails.

We can pass

And implement the void apply (RequestTemplate template) interface of RequestInterceptor to implement it.

The code example is as follows:

public class FeignUrlInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        
     //todo determines the url according to the rules, and then assigns a value     (url);
    //In addition, you can also set the header        
    }
}

@FeignClient(name = "api-service", configuration = )
public interface ApiFeignService {

}

At this time, when feign as configuration file is configured, the address will be switched dynamically according to the rules

Summarize

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