最近刚好打算做一个springcloud系列的分享,趁此机会刚好梳理下springcloud常用组件的使用,今天先对feign做个简单介绍!

feign是一个声明式的Web服务客户端,它使得发送web请求变得很容易,而openFign是springcloud对feign的一个升级,可以支持springMvc的注解;

接下来描述下我是怎么使用openFeign发送web请求的,首先需要明确,使用openFeign是有一定的代码侵入的,不过侵入的是consumer的代码,通过在consumer中添加openFeign的注解来拼装请求并发送,一般有两种方式,一种是consumer端根据provider提供的接口文档编写调用用例.另一种是由Prodiver提供一个接口API的jar包供consumer调用;下面上我的测试步骤及代码,两种方式都有涉及;

consumer端

 首先在consumer中引入openFeigin以及HTTPClient的依赖:

     <!-- 引入open-feign的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> <!-- 引入open-feign的httpClien依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
     <!-- 引入Prodiver提供的接口包(根据实际需要) -->
     <dependency>
    <groupId>com.darling.api</groupId>
    <artifactId>server-apis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
     </dependency>

  然后在consumer中封装一个service引入feign,来进行远程调用,关键代码如下:

package com.darling.eureka.consumer.service;

import com.darling.api.service.UserService;
import com.darling.eureka.consumer.model.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*; /**
* @description: 通过openFeign远程调用服务提供者
* @author: dll
* @date: Created in 2021/9/14 12:29
* @version: 1.0
* @modified By:
*/
@FeignClient(name = "EUREKA-PROVIDER")
public interface UserApiService extends UserService{ @GetMapping("/test/sayHi?name={name}")
String sayHi(@PathVariable String name); /**
* 测试 插入一条信息
* @param user
* @return
*/
@GetMapping("/test/insertInfo")
String insertInfo(@RequestBody User user); }

  说明:

    1、consumer中的启动类需要加上@EnableFeignClients注解,这里代码没有贴出

    2、FeignClient注解有多种属性可选,如自定义配置信息的configuration、直接指定访问链接的url等,由于我这里只是一个测试的demo就直接用name来指定在eureka中注册的Prodiver的服务名称了;

    3、代码中的GetMapping就是上面说的两种方式之一,consumer根据Prodiver提供的接口文档进行编码调用的;

    4、代码中的UserService就是上面说的另一种方式,由服务提供者提供的一个接口依赖包,我这里包名叫server-apis,,这样consumer中就可以像调用本地方法一样调用远程服务提供的接口能力,下面贴上UserService的代码:

package com.darling.api.service;

import com.darling.api.model.UserInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; /**
* @description:
* @author: dll
* @date: Created in 2021/9/15 22:02
* @version:
* @modified By:
*/
@RequestMapping("/serverApis")
public interface UserService { @GetMapping("/test")
UserInfo test(); /**
* 获取用户信息
* @return 用户姓名、年龄等信息
*/
@GetMapping("/getInfo")
UserInfo getInfo(); }

    需要注意的是,无论是通过Prodiver提供的依赖包还是直接编写调用代码,以上代码中所有的类似GetMapping、PostMapping的注解都是由Prodiver暴露出来的接口请求路径,由feign负责识别、拼装并发起远程请求的;

    下面来看看consumer中的Controller的调用代码:

package com.darling.eureka.consumer.controller;

import com.darling.api.model.UserInfo;
import com.darling.eureka.consumer.model.User;
import com.darling.eureka.consumer.service.UserApiService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /**
* @description: 测试openFeign的远程调用
* @author: dll
* @date: Created in 2021/8/25 21:52
* @version: 1.0
* @modified By:
*/
@RestController
@RequestMapping("/consumer/test")
public class TestConsumerController { @Resource
private UserApiService userApiService; /**
* 调用UserApiService中自己根据接口文档编写的调用代码
* @return
*/
@RequestMapping("/sayHello")
public String sayHello() {
String res = userApiService.sayHi("hahaha");
System.out.println("res = " + res);
return res;
} /**
* 调用UserApiService中自己根据接口文档编写的调用代码
* @param user
* @return
*/
@RequestMapping("/insert")
public String insertInfo(User user) {
String res = userApiService.insertInfo(user);
System.out.println("res = " + res);
return res;
} /**
* 调用UserApiService中由Prodiver提供的server-apis依赖包中的UserService的接口
* @return
*/
@RequestMapping("/getUserInfo")
public UserInfo getUserInfo() {
UserInfo res = userApiService.getInfo();
System.out.println("res = " + res);
return res;
} }

  provider端

  如果没有采用由Prodiver提供接口依赖包的方式的话,使用openFeign对Prodiver端几乎无任何侵入,Prodiver只需写好自己可以提供的服务并提供一份接口文档即可;

  下面主要聊聊如果通过提供接口依赖包的方式Prodiver应该做哪些调整以及其中可能遇到的坑

  首先,需要定义一个专门用来提供接口能力的api服务(server-apis),就正常的web服务即可,代码可参见上面的UserService;然后由Prodiver引入这个服务;需要注意的是如果通过这种方式提供服务的话,Prodiver

需要以接口实现类的方式来提供具体服务的实现,不能在controller中定义接口的调用路径了,否则访问会直接404;

  总结

  通过以上的调用实例,我们可以得出结论,feign调用虽香但是对代码的确会有一定的侵入性,主要针对服务的调用者而言,而是通过依赖包调用还是由consumer自己编码调用,无非是愿意接受jar包耦合还是接口文档耦合了,

所以对于同一个团队开发的consumer和Prodiver来说,个人更倾向于通过定义统一的接口依赖包,双方严格按照依赖包进行开发,依赖包的变更大家都能实时感知并作出及时响应,调用起来也更方便;

而对于跨团队来开发consumer和Prodiver来说,个人认为通过接口文档会更合理,consumer相对来说能把命运掌握在自己手上,对接的时候只认接口文档,减少了对接口包的依赖

  

    

最新文章

  1. Nodejs初阶之express
  2. Python’s SQLAlchemy vs Other ORMs[转发 4]peewee
  3. Atitit.工作流系统的本质是dsl&#160;图形化的dsl&#160;&#160;4gl
  4. CentOS命令登录MySQL时,报错ERROR 1045 (28000):
  5. DM8168 坎坷硬件之路(DDR3)
  6. SecureCRT连接vm中的ubuntu
  7. openstack 手动安装版 功能测试
  8. UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures&lt;Problem I&gt;
  9. 为应用程序池 &#39;DefaultAppPool&#39; 提供服务的进程关闭时间超过了限制
  10. 在vim中使用perltidy美化perl代码
  11. Fastjson简单使用方法
  12. OkHttp实现全局过期token自动刷新
  13. spring mvc跨域(ajax post json)--filter方案
  14. Spring Boot中如何扩展XML请求和响应的支持
  15. 【XSY2808】董先生的休闲方案 组合数学
  16. SSH防暴力破解脚本
  17. ceph 的 bufferlist
  18. android开发学习——day2
  19. word中添加引文操作
  20. 【jQuery源码】DOM Ready

热门文章

  1. Mybatis-Plus高级之LambdaQueryWrapper,Wrappers.<实体类>lambdaQuery的使用
  2. 【NOI P模拟赛】奶油蛋糕塔(状压 DP)
  3. [SDOI2017]序列计数 (矩阵加速,小容斥)
  4. java数组---概念
  5. RTSP播放器开发填坑之道
  6. 来点基础的练习题吧,看见CSDN这类基础的代码不多
  7. [DOM]获取元素:根据ID、标签名、HTML5新增的方法、特殊元素获取
  8. vue方法同步(顺序)执行:async/await使用
  9. MySQL DDL执行方式-Online DDL介绍
  10. Django ORM 实现数据的单表 增删改查