• 动态调用微服务


    主要由三个文件组成

    DynamicService.java

    DynamicFeignClientFactory.java

    DynamicClient.java

    代码

    1. package org.jeecg.modules.cloud.feign;
    2. import org.springframework.cloud.openfeign.SpringQueryMap;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. import org.springframework.web.bind.annotation.PathVariable;
    5. import org.springframework.web.bind.annotation.PostMapping;
    6. import org.springframework.web.bind.annotation.RequestBody;
    7. /**
    8. * Description: 通用接口,里面定义通用方法
    9. * 注意: 由于服务生产者所有的接口的返回值都是json格式的字符串,
    10. * 所以这里的通用接口的返回值最好使用String类型!!!
    11. */
    12. public interface DynamicService {
    13. /**
    14. * post方法对应的方法
    15. *
    16. * @param url 服务生产者Handler方法请求映射地址
    17. * @param params 服务生产者Handler方法参数
    18. * @return
    19. */
    20. @PostMapping(value = "{url}")
    21. String executePostRequest(@PathVariable("url") String url, @RequestBody Object params);
    22. @GetMapping(value = "{url}")
    23. String executeGetRequest(@PathVariable("url") String url, @SpringQueryMap Object params);
    24. }
    1. package org.jeecg.modules.cloud.feign;
    2. import org.springframework.cloud.openfeign.FeignClientBuilder;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.stereotype.Component;
    5. /**
    6. * Description: FeignClient工厂类,根据服务名称创建FeignClient对象(代理对象)
    7. */
    8. @Component
    9. public class DynamicFeignClientFactory {
    10. private FeignClientBuilder feignClientBuilder;
    11. public DynamicFeignClientFactory(ApplicationContext applicationContext){
    12. this.feignClientBuilder = new FeignClientBuilder(applicationContext);
    13. }
    14. //动态生成feignClient对象(代理对象)
    15. public T getFeignClient(final Class type,String ServiceID){
    16. return this.feignClientBuilder.forType(type,ServiceID).build();
    17. }
    18. }
    1. package org.jeecg.modules.cloud.feign;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.stereotype.Component;
    4. /*
    5. * Description: 通过FeignClient工厂获取到的FeignClient对象通过指定的请求去调用生产者方法!
    6. */
    7. @Component
    8. public class DynamicClient {
    9. @Autowired
    10. private DynamicFeignClientFactory dynamicDynamicFeignClientFactory;
    11. public Object executePostApi(String feignName, String url, Object params) {
    12. DynamicService dynamicService = dynamicDynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName);
    13. return dynamicService.executePostRequest(url, params);
    14. }
    15. public Object executeGetApi(String feignName, String url, Object params) {
    16. DynamicService dynamicService = dynamicDynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName);
    17. return dynamicService.executeGetRequest(url, params);
    18. }
    19. }

    DynamicClient 提供了两个方法 get 和 post

  • 相关阅读:
    计算机组成原理知识总结(五)中央处理器
    PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
    ES6中新增加的Proxy对象及其使用方式
    code:-9907磁盘不足如何处理?帮你整理了几个必备的!
    PyTorch函数中的__call__和forward函数
    Java native 关键字
    多态day02
    opencv c++ 高斯模糊,高斯双边模糊(28)
    Pytoch随笔(光速入门篇)
    VRRP虚拟路由器冗余协议
  • 原文地址:https://blog.csdn.net/yaa2004/article/details/132851818