1.Feign传统方式的不足
①.在微服务架构中,当我们使用Feign传统方式进行服务调用的时候,需要在每个服务消费者中添加FeignClient接口,编写对应的方法,而且当服务生产者Handler新增方法之后,服务消费者也要在FeignClient接口中添加方法,这样的话,会有些累赘.
那么能不能在调用服务提供者方法的时候,传入生产者服务名称的动态生成FeignClient对象,然后通过一个通用的方法,指定Handler的URL以及参数就可以调用到指定的方法呢?
当然可以!
2.动态Feign
2.1.服务生产者
由于微服务项目创建过程并不是重点,所以后续只展示关键的代码.
2.1.1.Handler方法
@RestController
public class OrderController {
private static Map<String, Object> maps = new HashMap<>(10);
static {
maps.put("1", "笔记本");
}
@GetMapping(value = "/getOrderById/{oId}")
public Object getOrderById(@PathVariable(name = "oId") String oid) {
return maps.get(oid);
}
@PostMapping(value = "/addOrder")
public boolean addOrder(@RequestBody Map<String, String> orderMap) {
try {
maps.putAll(orderMap);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
2.2.动态Feign
为了方便在多个服务消费者模块中使用动态Feign的功能,可以将动态Feign相关的功能单独抽取出来作为一个公共的模块,之后在服务消费者模块中直接依赖该公共模块即可.
①.pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
②.通用方法
public interface DynamicService {
@PostMapping(value = "{url}")
String executePostRequest(@PathVariable("url") String url, @RequestBody Object params);
@GetMapping(value = "{url}")
String executeGetRequest(@PathVariable("url") String url, @SpringQueryMap Object params);
@PutMapping(value = "{url}")
String executePutRequest(@PathVariable("url") String url, @RequestBody Object params);
@DeleteMapping(value = "{url}")
String executeDeleteRequest(@PathVariable("url") String url, @RequestBody Object params);
}
②.Feign工厂类
@Component
public class DynamicFeignClientFactory <T>{
private FeignClientBuilder feignClientBuilder;
public DynamicFeignClientFactory(ApplicationContext applicationContext){
this.feignClientBuilder = new FeignClientBuilder(applicationContext);
}
public T getFeignClient(final Class<T> type,String ServiceID){
return this.feignClientBuilder.forType(type,ServiceID).build();
}
}
③.动态FeignClient类
@Component
public class DynamicClient {
@Autowired
private DynamicFeignClientFactory<DynamicService> dynamicDynamicFeignClientFactory;
public Object executePostApi(String feignName,String url,Object params){
DynamicService dynamicService = dynamicDynamicFeignClientFactory.getFeignClient(DynamicService.class,feignName);
return dynamicService.executePostRequest(url,params);
}
public Object executeGetApi(String feignName,String url,Object params){
DynamicService dynamicService = dynamicDynamicFeignClientFactory.getFeignClient(DynamicService.class,feignName);
return dynamicService.executeGetRequest(url, params);
}
}
动态Feign一旦定义好之后,不管后续服务生产者中添加加多少Handler方法,这里的代码基本上不需要修改,对于服务消费者来说也不会产生任何影响(有点类似于设计模式里面的"开闭原则");
2.3.服务消费者
①.引入动态Feign模块
<dependency>
<groupId>com.xp</groupId>
<artifactId>dynamic-feign</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
②.Handler接口
@RestController
public class UserController {
@Autowired
private DynamicClient dynamicClient;
@GetMapping(value = "/getorder/{oid}")
public Object getOrderById(@PathVariable(value = "oid") String oid) {
return this.dynamicClient.executeGetApi("cloud-order", "/getOrderById/".concat(oid),new HashMap<>());
}
@PostMapping(value = "/addOrder")
public Object addOrder(@RequestBody Map<String,Object> map){
return this.dynamicClient.executePostApi("cloud-order", "/addOrder", map);
}
}
③.启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
代码完成之后,启动相关的服务,就可以进行测试
3.总结
使用动态Feign方式进行服务调用可以让开发者少写很多代码,使其可以专注于业务本身!
|