浅谈Ribbon、Feign和OpenFeign的区别

Ribbon Ribbon 是 Netflix开源的基于HTTP和TCP等协议负载均衡组件
Ribbon 可以用来做客户端负载均衡,调用注册中心的服务
Ribbon的使用需要代码里手动调用目标服务,请参考官方示例:https://github.com/Netflix/ribbon
Feign Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端
Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。
Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务
Feign支持的注解和用法请参考官方文档:https://github.com/OpenFeign/feign
Feign本身不支持Spring MVC的注解,它有一套自己的注解
OpenFeign OpenFeign是Spring Cloud 在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。
OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,
并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

// user-api 子项目public interface SysUserResource { @GetMapping("/test") Object getUser(); }// user-client 子项目 , 依赖了user-api 子项目// 其他业务模块可以直接依赖此模块,通过调用接口即可完成服务的远程调用,open-feign会对此类做动态代理// name = "user-center" 是被调用的服务实例名称@FeignClient(name = "user-center")public interface SysUserResourceClient extends SysUserResource {}// user-impl 子项目@RestControllerpublic class SysUserResourceImpl implements SysUserResource{ @Override Object getUser(){// do something }}// role-impl 子项目 , 依赖了 user-client 子项目@RestControllerpublic class SysRoleResourceImpl implements SysRoleResource{ @Resource private SysUserResourcesysUserResource; @Override Object test(){sysUserResource.getUser(); }}

浅谈Ribbon、Feign和OpenFeign的区别
文章图片

需要注意,@RequesMapping不能在类名上与@FeignClient同时使用

OpenFeign服务接口调用(与Feign的区别) 1简介
Feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端。
之前已经创建好了用户,订单,商品微服务,这三个微服务是互相隔离的,那么微服务和微服务之间如何互相调用呢,显然三个微服务都可以采用http通信,也就是restTemplate进行互相访问,但是这种方式对参数传递和使用都不是很方便,所以弃用此方式。
采用feign进行服务之间的调用,可以简化调用流程,真正感觉到是在同一个项目中调用另一个类的方法的欢快感,类似controller调用service。
Feign旨在使编写Java Http客户端变得更容易。 前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。
浅谈Ribbon、Feign和OpenFeign的区别
文章图片

2使用步骤
浅谈Ribbon、Feign和OpenFeign的区别
文章图片

1新建cloud-consumer-order80-fegin 2POM
cloud1000com.zs.springcloud1.0-SNAPSHOT4.0.0cloud-consumer-order80-feignorg.springframework.cloudspring-cloud-starter-openfeignorg.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-actuatororg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtestcom.zs.springcloudcloud-api-commons1.0-SNAPSHOTcompile

3YAML
server:port: 80eureka:client:register-with-eureka: falseservice-url:defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

4主启动类
浅谈Ribbon、Feign和OpenFeign的区别
文章图片

@SpringBootApplication@EnableFeignClientspublic class OrderMain80Feign {public static void main(String[] args) {SpringApplication.run(OrderMain80Feign.class, args); }}

5业务类
浅谈Ribbon、Feign和OpenFeign的区别
文章图片

@Component@FeignClient(value = "https://www.it610.com/article/CLOUD-PAYMENT-SERVICE")public interface PaymentFeignService {@GetMapping(value = "https://www.it610.com/payment/get/{id}")public CommonResult getPaymentById(@PathVariable("id") Long id); }

浅谈Ribbon、Feign和OpenFeign的区别
文章图片

@RestControllerpublic class OrderFeignController {@Autowiredprivate PaymentFeignService paymentFeignService; @GetMapping(value = "https://www.it610.com/consumer/payment/get/{id}")public CommonResult getPaymentById(@PathVariable("id") Long id) {return paymentFeignService.getPaymentById(id); }}

浅谈Ribbon、Feign和OpenFeign的区别
文章图片

浅谈Ribbon、Feign和OpenFeign的区别
文章图片

浅谈Ribbon、Feign和OpenFeign的区别
文章图片


浅谈Ribbon、Feign和OpenFeign的区别
文章图片

3OpenFeign超时控制
浅谈Ribbon、Feign和OpenFeign的区别
文章图片

3.1服务提供方8001故意写暂停程序
@GetMapping(value = "https://www.it610.com/payment/feign/timeout")public String paymentFeignTimeout(){try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace(); }return serverPort; }

3.2服务消费方80添加超时方法PaymentFeignService
@GetMapping(value = "https://www.it610.com/payment/feign/timeout")public String paymentFeignTimeout();

3.3服务消费方80添加超时方法OrderFeignController
@GetMapping(value = "https://www.it610.com/consumer/payment/feign/timeout")public String paymentFeignTimeout(){return paymentFeignService.paymentFeignTimeout(); }

3.4测试
http://localhost/consumer/payment/feign/timeout
浅谈Ribbon、Feign和OpenFeign的区别
文章图片

3.5YML文件里需要开启OpenFeign客户端超时控制
ribbon: #根ReadTimeout:5000ConnectTimeout: 5000

浅谈Ribbon、Feign和OpenFeign的区别
文章图片

4OpenFeign日志打印功能
浅谈Ribbon、Feign和OpenFeign的区别
文章图片

@Configurationpublic class FeignConfig {@BeanLogger.Level feignLoggerLevel(){return Logger.Level.FULL; }}

logging:level:com.zs.springcloud.service.PaymentFeignService: debug

浅谈Ribbon、Feign和OpenFeign的区别
文章图片

【浅谈Ribbon、Feign和OpenFeign的区别】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读