作者:微信小助手
发布时间:2022-12-19T08:43:47
对于fegin调用,我们一般的用法:为每个 例如下面这样: 这样写的话,可能会有些 答案是可以的!!!^_^ 定义一个通用的接口,通用的get,post方法 executePostApi:(post方法) 定义一个动态 executePostApi:(post方法) feignName,表示需要调用的微服务的名称,一般对应 定义一个动态 主要的作用:是帮我们动态的创建一个 好了,具体的操作步骤,就是上面所说的了!!!是不是很通用了呢? 通用是通用了,那怎么玩呢(如何使用)? 使用的方式,也是十分的简单啦:^_^ 先获取到 "system",表示调用微服务的名称,一般对应 "/system/test",表示调用的 new HashMap<>(),为需要传递的参数 好了,这样就实现了一个通用版的,那我们就可以愉快的编写代码了!!!^_^Feign
在微服务框架中使得服务直接的调用变得很简洁、简单,而不需要再编写Java Http
调用其他微服务的接口。动态feign
微服务
都创建对应的feignclient
接口,然后为每个微服务的controller
接口,一一编写对应的方法,去调用对应微服务的接口。//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);
....
}累赘
,那么我们能不能创建一个动态的feign
;当调用sytem微服务
的时候,传递一个feignclient
的name
为system
进去,然后定义一个通用的方法
,指定调用的url
,传递的参数
,就可以了呢?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);
}
url
,表示你要调用微服务的接口url,一般来说是对应controller
接口的url;params
,为调用该接口所传递的参数,这里加了@RequestBody
,那对应的controller
接口,接收参数也需要加上该注解。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);
}
}
application.name
,例如:system
url
,表示你要调用微服务的接口url,一般来说是对应controller
接口的url;params
,为调用该接口所传递的参数,这里加了@RequestBody
,那对应的controller
接口,接收参数也需要加上该注解。feignclient
工厂类@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();
}
}
feignclient
对象DynamicClient dynamicClient = SpringUtil.getBean(DynamicClient.class);
Object result = dynamicClient.executePostApi("system", "/system/test", new HashMap<>());
System.out.println("==========>"+JSONObject.toJSONString(result));DynamicClient
对象,然后直接调用executePostApi
方法
application.name
url