温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Ribbon、Feign和OpenFeign的区别是什么

发布时间:2021-06-23 11:41:20 来源:亿速云 阅读:583 作者:chen 栏目:开发技术

这篇文章主要讲解了“Ribbon、Feign和OpenFeign的区别是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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 子项目
@RestController
public class SysUserResourceImpl implements SysUserResource  {
 @Override
 Object getUser(){
  // do something
 }
}
// role-impl 子项目 , 依赖了 user-client 子项目
@RestController
public class SysRoleResourceImpl implements SysRoleResource  {
 @Resource
 private SysUserResource  sysUserResource;
 
 @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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud1000</artifactId>
        <groupId>com.zs.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloud-consumer-order80-feign</artifactId>
    <!--openfeign-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.zs.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

3YAML

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

4主启动类

Ribbon、Feign和OpenFeign的区别是什么

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

5业务类

Ribbon、Feign和OpenFeign的区别是什么

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<?> getPaymentById(@PathVariable("id") Long id);
}

Ribbon、Feign和OpenFeign的区别是什么

@RestController
public class OrderFeignController {
    @Autowired
    private PaymentFeignService paymentFeignService;
    @GetMapping(value = "/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 = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
        return serverPort;
    }

3.2服务消费方80添加超时方法PaymentFeignService

@GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout();

3.3服务消费方80添加超时方法OrderFeignController

  @GetMapping(value = "/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:  5000
  ConnectTimeout: 5000

Ribbon、Feign和OpenFeign的区别是什么

4OpenFeign日志打印功能

Ribbon、Feign和OpenFeign的区别是什么

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}
logging:
  level:
    com.zs.springcloud.service.PaymentFeignService: debug

Ribbon、Feign和OpenFeign的区别是什么

感谢各位的阅读,以上就是“Ribbon、Feign和OpenFeign的区别是什么”的内容了,经过本文的学习后,相信大家对Ribbon、Feign和OpenFeign的区别是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI