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 Http
call the interface of other microservices.
dynamic feign
For fegin calls, our general usage is 微服务
to create corresponding feignclient
interfaces for each, and then controller
write 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 feignclient
to go name
in system
, and then define one 通用的方法
, specify the calling, url
and pass 参数
it, and that's it?
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);
}
复制代码
executePostApi: (post method)
url
, indicating that you want to call the interface url of the microservice, generally speaking,controller
the url of the corresponding interface;
params
, the parameters passed to call the interface are added here@RequestBody
, and the correspondingcontroller
interface, receiving parameters also need to add this annotation.
- define a dynamic
feignclient
@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,controller
the url of the corresponding interface;
params
, the parameters passed to call the interface are added here@RequestBody
, and the correspondingcontroller
interface, receiving parameters also need to add this annotation.
- Define a dynamic
feignclient
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();
}
}
复制代码
The main function: is to help us dynamically create an
feignclient
object
Well, specifically 操作步骤
, that's what I said above! ! ! Is it too 通用
much? ^_^
Universal is universal, so how to play ( 如何使用
)?
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
DynamicClient
object first, then call theexecutePostApi
method directly"system", which indicates the name of the calling microservice, which generally corresponds to
application.name
"/system/test", which means the called
url
new HashMap<>(), the parameter that needs to be passed
Well, this has implemented a general version feignclient
, then we can happily write code! ! ! ^_^