1. 优势
 
  可以使用maven 进行访问,实现代码的共享,减少跨服务调用服务编写的问题
 
2. 使用
 
   定义接口

  1. publicinterfaceIUserService{
  2. @RequestMapping(method =RequestMethod.GET, value ="/users/app",headers={"tenantid"})
  3. @ApiOperation(value ="发送短信通知",httpMethod ="GET", response =User.class,notes ="请保证传入字段正确")
  4. User getUser(@RequestParam("id")String id,@RequestHeader("tenantid")String tenantid);
  5. @RequestMapping(method =RequestMethod.GET, value ="/users/app2")
  6. User getUser2();
  7. @RequestMapping(method =RequestMethod.GET, value ="/users/app3")
  8. User getUser3();
  9. @RequestMapping(method =RequestMethod.POST, value ="/users/app3")
  10. User getUser4(@RequestBodyUser users);
  11. }
spring mvc rest  接口实现
 

  1. @RestController
  2. publicclassUserServiceFeignControllerimplementsIUserService{
  3. @Autowired
  4. privateUserserviceImpl userserviceImpl;
  5. @Override
  6. publicUser getUser2(){
  7. // TODO Auto-generated method stub
  8. return userserviceImpl.getUser2();
  9. }
  10. @Override
  11. publicUser getUser3(){
  12. // TODO Auto-generated method stub
  13. return userserviceImpl.getUser3();
  14. }
  15. @Override
  16. publicUser getUser(String id,@RequestHeader("tenantid")String tenantid){
  17. // TODO Auto-generated method stub
  18. return userserviceImpl.getUser(id, tenantid);
  19. }
  20. @Override
  21. publicUser getUser4(User users){
  22. // TODO Auto-generated method stub
  23. return users;
  24. }
  25. }
feign 跨服务调用
 
  1. @FeignClient(url="http://localhost:8089/",name="userservicecall")
  2. publicinterfaceIFeignUserServiceextendsIUserService{
  3. }
注解
  1. @SpringBootApplication
  2. @EnableFeignClients()
  3. publicclassApplication{
  4. publicstaticvoid main(String[] args){
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }
调用
  1. @RestController
  2. publicclassUserCacheController{
  3. @Autowired
  4. privateIFeignUserService userservice;
  5. @RequestMapping(value ="/demo1", method =RequestMethod.GET)
  6. publicObject importUser(String id,@RequestHeader("tenantid")String tenantid)throwsExecutionException{
  7. return userservice.getUser(id,tenantid);
  8. }
  9. }
3. 注意问题
   尽管使用接口继承,但是还是会发现部分参数是没法继承的类似 header 解决方法
   调整rest 实现接口方法的签名,如下:

  1. @Override
  2. publicUser getUser(String id,@RequestHeader("tenantid")String tenantid){
  3. // TODO Auto-generated method stub
  4. return userserviceImpl.getUser(id, tenantid);
  5. }
 
  
 

spring cloud feign 接口继承以及参数传递的问题的更多相关文章

  1. Spring Cloud Feign接口返回流

    身无彩凤双飞翼,心有灵犀一点通. 服务提供者 @GetMapping("/{id}") public void queryJobInfoLogDetail(@PathVariabl ...

  2. 第六章:声明式服务调用:Spring Cloud Feign

    Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...

  3. Spring Cloud Feign 声明式服务调用

    目录 一.Feign是什么? 二.Feign的快速搭建 三.Feign的几种姿态 参数绑定 继承特性 四.其他配置 Ribbon 配置 Hystrix 配置 一.Feign是什么? ​ 通过对前面Sp ...

  4. Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常

    Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...

  5. Bug集锦-Spring Cloud Feign调用其它接口报错

    问题描述 Spring Cloud Feign调用其它服务报错,错误提示如下:Failed to instantiate [java.util.List]: Specified class is an ...

  6. Spring Cloud Feign 如何使用对象参数

    概述 Spring Cloud Feign 用于微服务的封装,通过接口代理的实现方式让微服务调用变得简单,让微服务的使用上如同本地服务.但是它在传参方面不是很完美.在使用 Feign 代理 GET 请 ...

  7. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  8. 笔记:Spring Cloud Feign 声明式服务调用

    在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...

  9. 笔记:Spring Cloud Feign 其他配置

    请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...

随机推荐

  1. rdlc报表出现多余空白页面

    1.RDLC报表设计好后,在ReportViewer预览报表时,页数都正常:但在切换为整页模式时,常造成多了不少空白页出来.第一时间觉得不可思议,在ReportViewer看来正常,怎可能在整页预览时 ...

  2. fio使用

    关于lat(I/O completion latency).clat(complete latency).slat(summit latency): lat is the total latency. ...

  3. kafka遗忘点

    1.通常,分区数比broker多.follower从leader拉取批量日志应用到自己的日志.消费者消费消息 也是拉取模式. 2.如果leader没有故障,我们就不需要follower!当leader ...

  4. (MSSQL)sp_refreshview刷新视图失败及更新Table字段失败的问题解决

    在近期工作中遇到一个任务,需要批量更改散布在很多Table中的某字段,同时刷新相关视图,但是在执行脚本时,发现了如下问题 更新字段问题 消息 ,级别 ,状态 ,第 行 对象'View_Simple' ...

  5. ios8 - CoreLocation定位服务的变化

    在iOS8开发中,发现一个CoreLocation的使用问题,执行操作之后,不会调用到定位之后的delegate方法中. 根据文档,需要在使用CoreLocation前调用方法: [locationm ...

  6. 你必须了解的Session的本质

    http://netsecurity.51cto.com/art/201402/428721.htm Cookie和session由于实现手段不同,因此也各有优缺点和各自的应用场景: 1.   应用场 ...

  7. 如何利用$_SERVER["PHP_SELF"]变量植入script代码?

    假如我们是黑客,可以诱骗用户访问如下链接, 相当于用户会在浏览器地址栏中输入以下地址: http://www.xxx.com/test_form.php/%22%3E%3Cscript%3Ealert ...

  8. php获取excel文件数据

    很简单就可以实现,下面为大家简单介绍下 1.下载PHPExcel类,是一个文件夹,还得有一个文件PHPExcel.php,两个在同级目录 require __DIR__ . './PHPExcel/I ...

  9. three.js入门——先跑个旋转的正方体

    WebGl中文网看了几篇教程,又百度了几篇文章,顿时感觉手痒,打开编辑器,写个demo玩玩. demo是写在vue项目中的,所以首先: npm install three --save; npm in ...

  10. java 导入Excel -- 套路及代码分析

    一.思路分析 1.我们要做导入,实际上也就是先文件上传,然后读取文件的数据. 2.我们要有一个导入的模板,因为我们导入的Excel列要和我们的数据字段匹配上,所以我们要给它来一个规定,也就是模板. 3 ...