当前位置: 首页> 教育> 就业 > 深圳做网站推广公司_国家新闻出版署入口_seo网站有哪些_谷歌浏览器手机版官网下载

深圳做网站推广公司_国家新闻出版署入口_seo网站有哪些_谷歌浏览器手机版官网下载

时间:2025/8/27 1:50:06来源:https://blog.csdn.net/qushaming/article/details/144468713 浏览次数:0次
深圳做网站推广公司_国家新闻出版署入口_seo网站有哪些_谷歌浏览器手机版官网下载

目录

一、概述

二、完整示例代码

2.1. DynamicFeignClientFactory工厂类

2.2. DynamicClient

2.3. DynamicService

2.4. 测试类


一、概述

我们在日常开发过程中,在通过Feign进行微服务间的接口调用时,经常会编写各个微服务功能模块的Feign接口,每新增一个功能模块,我们就需要新定义一个这样的接口,非常地不方便。本章节为我们将通过一个实际的案例来给大家演示如何动态的实现Feign接口调用,减少业务代码量。

package com.example.demo;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;@FeignClient(name = "user")
public interface UserClient {@GetMapping("/system/test1")R test1(String name);@PostMapping("/system/test2")R test2(String name);
}
package com.example.demo;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;@FeignClient(name = "product")
public interface ProductClient {@GetMapping("/product/test1")R test1(String name);@PostMapping("/product/test2")R test2(String name);
}

二、完整示例代码

万能Feign调用代码

2.1. DynamicFeignClientFactory工厂类

package com.example.demo;import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;/*** Feign api工厂类* @param <T>*/
@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();}
}

2.2. DynamicClient

package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class DynamicClient {@Autowiredprivate DynamicFeignClientFactory<DynamicService> dynamicFeignClientFactory;/*** 创建post api* @param feignName 远程调用服务名* @param url 远程调用地址* @param params* @return*/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);}
}

2.3. DynamicService

package com.example.demo;import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;/*** 定义方法类型以及接收调用地址和参数*/
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);
}

2.4. 测试类

package com.example;import cn.hutool.extra.spring.SpringUtil;
import com.example.demo.DynamicClient;import java.util.HashMap;public class Test {public static void main(String[] args) {// 动态feign调用DynamicClient dynamicClient = SpringUtil.getBean(DynamicClient.class);Object user = dynamicClient.executePostApi("user", "/system/test", new HashMap());}
}

关键字:深圳做网站推广公司_国家新闻出版署入口_seo网站有哪些_谷歌浏览器手机版官网下载

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: