Dynamic Feign and generic interface methods

Get into the habit of writing together! This is the 12th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Feign, In the microservice framework, the direct invocation of the service becomes very concise and simple, and there is no need to 编写Java Httpcall the interface of other microservices.

dynamic feign

For fegin calls, our general usage is 微服务to create corresponding feignclientinterfaces for each, and then controllerwrite corresponding methods for each microservice interface one by one to call the corresponding microservice interface.

For example the following:

//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);
    
    ....
}
复制代码

If it is written in this way, there may be some 累赘, so can we create one 动态的feign; when calling sytem微服务, pass one feignclientto go namein system, and then define one 通用的方法, specify the calling, urland pass 参数it, and that's it?

The answer is yes! ! ! ^_^

image.png

  • 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);
}
复制代码

executePostApi: (post method)

url, indicating that you want to call the interface url of the microservice, generally speaking, controllerthe url of the corresponding interface;

params, the parameters passed to call the interface are added here @RequestBody, and the corresponding controllerinterface, receiving parameters also need to add this annotation.

  • 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);
    }
}
复制代码

executePostApi: (post method)

feignName, indicating the name of the microservice to be called, generally corresponds to application.name, for example:system

url, indicating that you want to call the interface url of the microservice, generally speaking, controllerthe url of the corresponding interface;

params, the parameters passed to call the interface are added here @RequestBody, and the corresponding controllerinterface, receiving parameters also need to add this annotation.

  • Define a dynamic feignclientfactory 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();
    }
}
复制代码

The main function: is to help us dynamically create an feignclientobject

Well, specifically 操作步骤, that's what I said above! ! ! Is it too 通用much? ^_^

Universal is universal, so how to play ( 如何使用)?

image.png

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));
复制代码

Get the DynamicClientobject first, then call the executePostApimethod directly

"system", which indicates the name of the calling microservice, which generally corresponds toapplication.name

"/system/test", which means the calledurl

new HashMap<>(), the parameter that needs to be passed

Well, this has implemented a general version feignclient, then we can happily write code! ! ! ^_^

image.png

Guess you like

Origin juejin.im/post/7085661725881696293