SpringCloud的Feign有哪些注意事项
发表于:2025-12-04 作者:千家信息网编辑
千家信息网最后更新 2025年12月04日,这篇文章主要介绍"SpringCloud的Feign有哪些注意事项",在日常操作中,相信很多人在SpringCloud的Feign有哪些注意事项问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作
千家信息网最后更新 2025年12月04日SpringCloud的Feign有哪些注意事项
这篇文章主要介绍"SpringCloud的Feign有哪些注意事项",在日常操作中,相信很多人在SpringCloud的Feign有哪些注意事项问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"SpringCloud的Feign有哪些注意事项"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Feign注意事项
* 一、FeignClients使用注意事项: * 1. @EnableFeignClients 默认扫描 xxxxApplication启动入口 所在包路径下的 @FeignClient bean,若无法扫描到, 可以在使用Feign调用外部模块的api时候,需要在引用服务中 xxxxApplication 中的 `@EnableFeignClients(basePackages = "cn.tendyron.customer")` 添加外部包需要扫描FeignClient的路径,否则无法注入bean * 2. @FeignClient 声明的类,使用 spring.application.name 作为 name配置 @FeignClient(name="xxxx"),如果想保留 context-path , 则需要配置 path 属性 ,如:@FeignClient(name="xxxx" , path="xxxx(context-path)") * 3. @FeignClient 接口对应的实现类,需要使用 @RestController注解 声明 * 4. mapper注解不支持 : @GetMapping,@PostMapping , 参数要加 @RequestParam("xxx") * 5. FeignClient 调用,实质是httpClient调用 ,若我们暴露的接口api,声明了对应的 http mapper 信息,在调用方调用时候,通过代理 发起了 http请求,到服务提供方对应的http服务上去,所以在服务提供方的接口,可以使用 @RestController * 来声明接口的 实现,否则调用方无法找到 http 对应的路径,会报404 ; * 或者 根据 api 声明的http 信息,构建一个 Controller ,再来调用接口的实现类,但是这样不太方便;
Feign接口定义
package cn.tendyron.customer.api;import cn.tendyron.common.api.protocol.CommonResult;import cn.tendyron.customer.bo.OrgUserBO;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;/** * @author CRong.L * @ClassName: TestService: * @Description: 测试服务,注意 Feign的标准写法 * @date 2019/7/5 */@FeignClient(name = "customer-center",path = "customer")public interface TestService { /** * 一、FeignClients使用注意事项: * 1. @EnableFeignClients 默认扫描 xxxxApplication启动入口 所在包路径下的 @FeignClient bean,若无法扫描到, 可以在使用Feign调用外部模块的api时候,需要在引用服务中 xxxxApplication 中的 `@EnableFeignClients(basePackages = "cn.tendyron.customer")` 添加外部包需要扫描FeignClient的路径,否则无法注入bean * 2. @FeignClient 声明的类,使用 spring.application.name 作为 name配置 @FeignClient(name="xxxx"),如果想保留 context-path , 则需要配置 path 属性 ,如:@FeignClient(name="xxxx" , path="xxxx(context-path)") * 3. @FeignClient 接口对应的实现类,需要使用 @RestController注解 声明 * 4. mapper注解不支持 : @GetMapping,@PostMapping , 参数要加 @RequestParam("xxx") * 5. FeignClient 调用,实质是httpClient调用 ,若我们暴露的接口api,声明了对应的 http mapper 信息,在调用方调用时候,通过代理 发起了 http请求,到服务提供方对应的http服务上去,所以在服务提供方的接口,可以使用 @RestController * 来声明接口的 实现,否则调用方无法找到 http 对应的路径,会报404 ; * 或者 根据 api 声明的http 信息,构建一个 Controller ,再来调用接口的实现类,但是这样不太方便; */ /** * 测试Feign Get ,普通处理 * 注意: @RequestParam("xx") 注解一定要写,而且属性名xx不能省略 */ @RequestMapping(value = "/test/getUserBo",method = RequestMethod.GET) CommonResult getUserBo(@RequestParam("orgCode") String orgCode , @RequestParam("username") String name); /** * 测试Feign Get Pojo */ @RequestMapping(value = "/test/testUserBo",method = RequestMethod.GET) CommonResult testUserBo(OrgUserBO userBO , @RequestParam("token") String token); /** * 测试Feign Post Pojo * 注意:实现类也要在参数前加 @RequestBody */ @RequestMapping(value = "/test/postUserBo",method = RequestMethod.POST) CommonResult postUserBo(@RequestBody OrgUserBO userBO);} 接口实现
package cn.tendyron.customer.service.impl;import cn.tendyron.common.api.protocol.CommonResult;import cn.tendyron.common.util.BeanUtils;import cn.tendyron.customer.api.AuthService;import cn.tendyron.customer.api.TestService;import cn.tendyron.customer.bo.OrgUserBO;import cn.tendyron.customer.common.constant.BizErrorCodeEnum;import cn.tendyron.customer.common.constant.Constant;import cn.tendyron.customer.entity.OrgUserDO;import cn.tendyron.customer.support.OrgUserSupport;import com.alibaba.fastjson.JSON;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.util.Assert;import org.springframework.web.bind.annotation.RestController;import java.util.Objects;import java.util.concurrent.TimeUnit;/** * @author CRong.L * @ClassName: TestServiceImpl * @Description: * @date 2019/7/5 */@Slf4j@RestControllerpublic class TestServiceImpl implements TestService { @Autowired RedisTemplate redisTemplate; @Override public CommonResult getUserBo(String orgCode, String name) { return null; } @Override public CommonResult testUserBo(OrgUserBO userBO,String token) { log.info("Feign Get Pojo token:{}",token); return CommonResult.success(userBO); } @Override public CommonResult postUserBo(OrgUserBO userBO) { return CommonResult.success(userBO); }} 到此,关于"SpringCloud的Feign有哪些注意事项"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
接口
服务
事项
注意事项
路径
注解
供方
信息
时候
学习
测试
配置
参数
属性
入口
实质
所在
更多
模块
会报
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
超云服务器管理口
数据库源代码
网页设计服务器字体没有路径
计算机网络技术上数学吗
数据库怎么学啊
关于网络安全宣传的观后感
交行软件开发中心 面试
uniapp表单提交存入数据库
app的服务器的选择
互联网保险和网络安全有关吗
GCC下载软件开发
魔兽世界怎么设置一键换服务器
信元网络安全技术股份
厦门共享陪护床软件开发
360网络安全工资
网络安全扫描要多少钱
融媒体中心网络安全应急预案简报
网络安全情况总结
带网络安全模式连不上网
网络安全空间学院
贵州热梦网络技术有限公司董事长
数据库系统教程 何玉洁
微信小程序软件开发专家
迷你服务器主板推荐
cago天梯如何重连服务器
小程序制作软件开发
财务管理网络安全管理
网络安全工程师求职非你莫属
软件开发的税前扣除
服务器上部署tomcat中间件