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<OrgUserBO> getUserBo(@RequestParam("orgCode") String orgCode , @RequestParam("username") String name);/*** 测试Feign Get Pojo*/@RequestMapping(value = "/test/testUserBo",method = RequestMethod.GET)CommonResult<OrgUserBO> testUserBo(OrgUserBO userBO , @RequestParam("token") String token);/*** 测试Feign Post Pojo* 注意:实现类也要在参数前加 @RequestBody*/@RequestMapping(value = "/test/postUserBo",method = RequestMethod.POST)CommonResult<OrgUserBO> 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
@RestController
public class TestServiceImpl implements TestService {@AutowiredRedisTemplate redisTemplate;@Overridepublic CommonResult<OrgUserBO> getUserBo(String orgCode, String name) {return null;}@Overridepublic CommonResult<OrgUserBO> testUserBo(OrgUserBO userBO,String token) {log.info("Feign Get Pojo token:{}",token);return CommonResult.success(userBO);}@Overridepublic CommonResult<OrgUserBO> postUserBo(OrgUserBO userBO) {return CommonResult.success(userBO);}
}

转载于:https://my.oschina.net/u/3245438/blog/3073514

【SpringCloud】 - Feign 踩坑记录:404 ,调用不成功 , 接口定义规范 等问题记录相关推荐

  1. feign踩坑_spring cloud fegin踩坑记录

    1:多客户端时,fegin接口抽取到公共jar中,此时,客户端的启动类上需要对该jar中fegin所在的包进行扫描,要在spring和fegin中同时注册,否则启动时会报:"Consider ...

  2. SpringCloud Feign 源码底层如何调用Ribbon实现服务调用的负载均衡

    在SpringCloud微服务中调用都不是直接用Ribbon进行服务调用,因为那样的化项目中的代码量会增加很多,微服务之间的调用是用Feign来进行调用,下面就是普通的一个接口调用例子 这样就可以进行 ...

  3. vue 踩坑--父组件调用子组件方法,报错

    父组件,调用子组件的方法,报如下错误 分析解决:1.可能子页面加载未完成,父组件调用子组件方法加延时处理 setTimeout(() => {this.$refs.notice.getInfo( ...

  4. 在EPICS定义一个新的记录类型

    1) 新建一个顶层目录exer13,进入这个目录并且创建一个IOC目录结构: [blctrl@bjAli exer13]$ makeBaseApp.pl -t ioc xxxTest [blctrl@ ...

  5. 记录一次C语言调用go生成的动态库的踩坑过程

    记录一次C语言调用go生成的动态库的踩坑过程 问题现象 由于某些特殊原因,需要在C语言中调用go语言生成的so,本来挺顺利,一切都运行的很好.突然某一天,不知道怎么回事,再一个新程序中无法正常运行了, ...

  6. springcloud踩坑记录1

    idea使用spring intaller 创建eureka服务踩坑记录 1.SpringBoot与SpringCloud的版本号需要一一对应,具体内容请查看官网. 2.Maven引入Eureka包时 ...

  7. 【javascript】浏览器调用摄像头扫二维码踩坑记录

    前言 最近做一个项目需要用浏览器调用摄像头扫二维码,然后就踩了几个坑记录下. 踩坑记录 我一开始发现了zxing这个库,他分为https://www.npmjs.com/package/@zxing/ ...

  8. 日常踩坑记录-汇总版

    开发踩坑记录,不定时更新 心得 RTFM 严谨的去思考问题,处理问题 严格要求自己的代码编写习惯与风格 注意 单词拼写 20200207 mybatis plus 自带insert插入异常 sql i ...

  9. SpringCloud Feign声明式服务调用

    SpringCloud Feign声明式服务调用 1. 加入pom依赖 2. Application.java上声明@EnableFeignClients 3. @FeignClient声明接口调用服 ...

  10. Python打包工具Pyintealler打包py文件为windows exe文件过程及踩坑记录+实战例子

    Python打包工具Pyintealler打包py文件为windows exe文件过程及踩坑记录+实战例子 目录 Python打包工具Pyintealler打包py文件为windows exe文件过程 ...

最新文章

  1. 互联网技术都应该了解的一种数据格式——JSON
  2. 新巴巴运动网 项目第十一天
  3. 语言const的生命周期_如何理解一门编程语言2——以复制构造函数为例
  4. 关于SQL中的两个问题的理解
  5. 【2022年】帝豪gs/帝豪gl 车机安装第三方软件教程
  6. native数据类型 react_React-Native 之 数据持久化
  7. word目录及图表目录的自动生成
  8. iOS监听键盘的删除按键事件
  9. 直播行业市场分析:2022年构建多元化的直播生态体系
  10. nmon监控工具使用(打开nmon文件出现  运行时错误13类型不匹配)
  11. 三星手机不能连接无线网络连接服务器,手机已连接wife但无法访问互联网
  12. 外显子名词解释_生物信息学常用名词解释(四)
  13. 程序员视角m1 Macbook air使用指南和指令备忘录
  14. opencv 提取彩色图像轮廓
  15. VS2015 C++/CLR
  16. 创业团队 磨合 团队稳定 团队成员
  17. 用计算机打女生节快乐,女生节快乐的朋友圈说说
  18. 12亿次月访问流量网站服务器架构探秘
  19. 操盘技巧学习笔记——证券交易中篇完结
  20. 如何在EDUIS中导出ETL字幕模板_Arctime教程 - 导出到剪辑软件 | Arctime字幕软件

热门文章

  1. Microsoft FxCop 的设计规则 .
  2. 浅谈~区块链的现状与未来!
  3. Android WebView加载网页进度监听
  4. java全能速查宝典.chm_Java全能速查宝典
  5. Fully Convolutional Cross-Scale-Flows for Image-based Defect Detection
  6. OctetString 转String
  7. python实现Content-Type:application/octet-stream
  8. Linux使用adsl计时制分享(转)
  9. 注册表终极修改ie主页的方法
  10. Impala graceful shutdown功能介绍