Make writing a habit together! This is the 12th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Feign, in the microservices framework, it is very simple and simple to call services directly without having to write Java Http interfaces to call other microservices.

Dynamic feign

For fegin calls, we generally use: create a corresponding FeignClient interface for each microservice, and then write a corresponding method for each microservice’s Controller interface to call the corresponding microservice interface.

For example:

//system
@FeignClient(name = "system")
public interface SystemClient {
    @GetMapping("/system/test1")
    JsonResult test1(String test1);
    
    @GetMapping("/system/test2")
    JsonResult test2(String test2); . }//user
@FeignClient(name = "user")
public interface UserClient {
    @GetMapping("/user/test1")
    JsonResult test1(String test1);
    
    @GetMapping("/user/test2")
    JsonResult test2(String test2); . }Copy the code

It might be a little bit cumbersome to write it this way, so can we create a dynamic feign; When calling the sytem microservice, pass in a feignClient name as System and define a generic method that specifies the url to call and the parameters to pass.

The answer is yes!! ^_^

  • Define a generic interface, generic GET, post methods
public interface DynamicService {
    
    @PostMapping("{url}")
    Object executePostApi(@PathVariable("url") String url, @RequestBody Object params);

    @GetMapping("{url}")
    Object executeGetApi(@PathVariable("url") String url, @SpringQueryMap Object params);
}
Copy the code

ExecutePostApi :(post method)

Url, the url of the interface that you want to call the microservice, generally the URL corresponding to the controller interface;

Params, @requestBody for parameters passed by calling this interface, that corresponds to the Controller interface, which also needs to be annotated for receiving parameters.

  • Define a dynamicfeignclient
@Component public class DynamicClient { @Autowired private DynamicFeignClientFactory<DynamicService> dynamicFeignClientFactory; public Object executePostApi(String feignName, String url, Object params) { DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName); return dynamicService.executePostApi(url, params); } public Object executeGetApi(String feignName, String url, Object params) { DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName); return dynamicService.executeGetApi(url, params); }}Copy the code

ExecutePostApi :(post method)

FeignName: indicates the name of the microservice to be invoked, usually corresponding to application.name, for example, system

Url, the url of the interface that you want to call the microservice, generally the URL corresponding to the controller interface;

Params, @requestBody for parameters passed by calling this interface, that corresponds to the Controller interface, which also needs to be annotated for receiving parameters.

  • Define a dynamicfeignclientThe factory class
@Component public class DynamicFeignClientFactory<T> { private FeignClientBuilder feignClientBuilder; public DynamicFeignClientFactory(ApplicationContext appContext) { this.feignClientBuilder = new FeignClientBuilder(appContext); } public T getFeignClient(final Class<T> type, String serviceId) { return this.feignClientBuilder.forType(type, serviceId).build(); }}Copy the code

The main function: to help us dynamically create a FeignClient object

Good, the specific operation steps, is the above said!! Is it universal? ^_^

Gm is gm, so how to play (how to use)?

The way to use it is also very simple: ^_^

DynamicClient dynamicClient = SpringUtil.getBean(DynamicClient.class);
Object result = dynamicClient.executePostApi("system", "/system/test", new HashMap<>());
System.out.println("==========>"+JSONObject.toJSONString(result));
Copy the code

Get the DynamicClient object and call the executePostApi method directly

“System” : indicates the name of the invoked microservice, usually corresponding to application.name

“/system/test”, indicating the calling URL

New HashMap<>(), which is the parameter to pass

Now that we have implemented a generic version of FeignClient, we can have fun coding!! ^_^