微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?

开发 前端
总的来说,基于SpringCloudAlibaba框架下,Feign和Dubbo各有千秋。选择使用哪一个组件取决于具体的项目需求和团队技术栈。对于需要快速构建微服务架构的项目,Feign是一个不错的选择;而对于需要更多自定义和服务治理功能的项目,Dubbo可能更适合。在实际应用中,也可以根据具体场景将两者结合使用,以达到更好的效果。

概述

随着微服务架构的普及,服务间的通信和调用成为了关键问题。在SpringCloudAlibaba框架下,Feign和Dubbo是两种常用的服务调用组件。本文将对两者进行性能对比及区别分析。

一、Feign与Dubbo概述

Feign是一个声明式的Web服务客户端,使得编写HTTP客户端变得更简单。通过简单的注解,Feign将自动生成HTTP请求,使得服务调用更加便捷。而Dubbo是一个高性能、轻量级的Java RPC框架,提供了丰富的服务治理功能。

二、性能对比

  1. 调用性能:在单次调用方面,Feign的性能表现略逊于Dubbo。由于Feign的自动生成HTTP请求机制,其性能相较于Dubbo的直接RPC调用会有一定的损失。然而,对于大多数应用而言,这种性能差异并不明显。
  2. 负载均衡:Feign和Dubbo都提供了负载均衡功能。Feign使用Ribbon作为其负载均衡组件,而Dubbo则内置了负载均衡机制。在负载均衡方面,Dubbo提供了更多的配置选项和策略,具有更强的灵活性。
  3. 服务发现:Feign依赖于Eureka、Consul、Nacos等注册中心实现服务发现,而Dubbo则提供了内置的服务发现机制。在服务发现的性能和稳定性方面,Dubbo具有一定的优势。

三、区别分析

  1. 架构差异:Feign基于SpringCloud体系,更适用于微服务架构。而Dubbo则独立于任何框架,具有更强的通用性。
  2. 适用场景:对于简单的服务调用场景,Feign更加简洁易用。而当需要复杂的服务治理功能时,Dubbo则更具优势。
  3. 扩展性:Feign提供了丰富的注解和配置选项,可以轻松地与SpringCloud的其他组件集成。而Dubbo则提供了丰富的SPI机制,使得扩展更加灵活。
  4. 社区活跃度:Feign的社区相对活跃,随着SpringCloud的发展,Feign也在不断迭代和完善。Dubbo的社区虽然活跃度不如Feign,但凭借其多年的积累和沉淀,依然拥有大量的用户和稳定的支持者。

四、实战性能对比

引入SpringCloud的pom集成dubbo

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.7</version>
    <relativePath/> 
</parent>
<properties>
        <spring.boot.version>2.7.7</spring.boot.version>
        <alibaba.cloud.version>2021.0.4.0</alibaba.cloud.version>
        <spring.cloud.version>2021.0.5</spring.cloud.version>
</properties>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
</dependencyManagement>

创建公共API模块

public interface HelloService {
    void sayHello(Long timme);
}

创建服务生产者

<dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>com.tiger</groupId>
            <artifactId>tiger-example-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

配置application.yml

### 服务端口号
server:
  port: 9800
#### nacos 注册中心地址
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  
  #### 服务名
  application:
    name: tiger-producer  
dubbo:
  application:
    # 关闭qos端口避免单机多生产者端口冲突 如需使用自行开启
    qos-enable: false
  registry:
    address: nacos://127.0.0.1:8848?username=nacos&password=nacos
    group: DEFAULT_GROUP
  protocol:
    # 如需使用 Triple 3.0 新协议 可查看官方文档
    # 使用 dubbo 协议通信
    name: dubbo
    # dubbo 协议端口(-1表示自增端口,从20880开始)
    port: -1


  # 消费者相关配置
  consumer:
    # 超时时间
    timeout: 3000
  scan:
    # 接口实现类扫描
    base-packages: com.tiger.**.dubbo

业务代码

@Service
@Slf4j
@DubboService
public class HelloServiceImpl implements HelloService {
    @Override
    public void sayHello(Long time) {
        long startTime = System.currentTimeMillis();
        long elapsed = time - startTime;
        log.info("dubbo rpc 调用耗时 {}",elapsed);
    }
}

创建Application启动器

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

创建服务消费者

<dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>com.tiger</groupId>
            <artifactId>tiger-example-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

配置application.yml

### 服务端口号
server:
  port: 9400
#### nacos 注册中心地址
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  
  #### 服务名
  application:
    name: tiger-consumer  
dubbo:
  application:
    # 关闭qos端口避免单机多生产者端口冲突 如需使用自行开启
    qos-enable: false
  registry:
    address: nacos://127.0.0.1:8848?username=nacos&password=nacos
    group: DEFAULT_GROUP
  protocol:
    # 如需使用 Triple 3.0 新协议 可查看官方文档
    # 使用 dubbo 协议通信
    name: dubbo
    # dubbo 协议端口(-1表示自增端口,从20880开始)
    port: -1
  # 消费者相关配置
  consumer:
    # 超时时间
    timeout: 3000
  scan:
    # 接口实现类扫描
    base-packages: com.tiger.**.dubbo

创建消费者调用代码

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/consumer/remoteTest")
public class RemoteController {


    @DubboReference
    private final HelloService helloService;


    private final ProducerFeign producerFeign;




    @GetMapping("dubboTest")
    public ResponseResult<?> dubboTest(){
        helloService.sayHello(System.currentTimeMillis());
        return ResponseResult.success();
    }


    @GetMapping("feignTest")
    public ResponseResult<?> feignTest(){
        producerFeign.sayHello(System.currentTimeMillis());
        return ResponseResult.success();
    }




}

创建Feign调用用于对比性能测试

@FeignClient("tiger-producer")
public interface ProducerFeign {


    @GetMapping("/producer/remoteTest/testFeign")
    void sayHello(@RequestParam Long time);


}

创建应用启动类

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


}

进行测试

http://localhost:9400/consumer/remoteTest/dubboTest
http://localhost:9400/consumer/remoteTest/feignTest

图片图片

从调用时间上可见 Dubbo的性能确实比Feign的性能好上不少

总结

总的来说,基于SpringCloudAlibaba框架下,Feign和Dubbo各有千秋。选择使用哪一个组件取决于具体的项目需求和团队技术栈。对于需要快速构建微服务架构的项目,Feign是一个不错的选择;而对于需要更多自定义和服务治理功能的项目,Dubbo可能更适合。在实际应用中,也可以根据具体场景将两者结合使用,以达到更好的效果。

责任编辑:武晓燕 来源: HELLO程序员
相关推荐

2020-08-04 17:06:40

Merging Rebasing Git

2009-12-01 08:47:41

2010-05-28 11:09:51

SVN功能

2011-03-04 15:22:37

ADSLLAN

2018-10-25 14:08:07

KubernetesGoogle

2009-09-22 09:59:05

服务器售后服务

2009-07-02 18:50:43

2010-03-18 14:54:46

主流无线技术

2011-03-08 10:55:00

亚马逊谷歌微软

2012-06-18 09:49:11

虚拟化

2011-01-19 11:10:30

2009-02-06 14:26:37

UbuntuVistaWindows7

2016-08-10 20:58:21

ARM架构X86架构CPU

2010-05-24 18:15:34

SVN中Branch和

2017-09-10 14:29:03

眼力

2010-09-08 15:41:28

SIP协议栈

2010-08-25 16:12:34

职场

2023-05-26 15:53:48

MidjourneyAI图像

2014-01-07 17:08:02

Java开源框架

2021-03-15 21:07:17

IT行业薪酬薪水
点赞
收藏

51CTO技术栈公众号