自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

微服務(wù)架構(gòu)下Feign和Dubbo的性能大比拼,到底鹿死誰手?

開發(fā) 前端
總的來說,基于SpringCloudAlibaba框架下,F(xiàn)eign和Dubbo各有千秋。選擇使用哪一個(gè)組件取決于具體的項(xiàng)目需求和團(tuán)隊(duì)技術(shù)棧。對于需要快速構(gòu)建微服務(wù)架構(gòu)的項(xiàng)目,F(xiàn)eign是一個(gè)不錯(cuò)的選擇;而對于需要更多自定義和服務(wù)治理功能的項(xiàng)目,Dubbo可能更適合。在實(shí)際應(yīng)用中,也可以根據(jù)具體場景將兩者結(jié)合使用,以達(dá)到更好的效果。

概述

隨著微服務(wù)架構(gòu)的普及,服務(wù)間的通信和調(diào)用成為了關(guān)鍵問題。在SpringCloudAlibaba框架下,F(xiàn)eign和Dubbo是兩種常用的服務(wù)調(diào)用組件。本文將對兩者進(jìn)行性能對比及區(qū)別分析。

一、Feign與Dubbo概述

Feign是一個(gè)聲明式的Web服務(wù)客戶端,使得編寫HTTP客戶端變得更簡單。通過簡單的注解,F(xiàn)eign將自動生成HTTP請求,使得服務(wù)調(diào)用更加便捷。而Dubbo是一個(gè)高性能、輕量級的Java RPC框架,提供了豐富的服務(wù)治理功能。

二、性能對比

  1. 調(diào)用性能:在單次調(diào)用方面,F(xiàn)eign的性能表現(xiàn)略遜于Dubbo。由于Feign的自動生成HTTP請求機(jī)制,其性能相較于Dubbo的直接RPC調(diào)用會有一定的損失。然而,對于大多數(shù)應(yīng)用而言,這種性能差異并不明顯。
  2. 負(fù)載均衡:Feign和Dubbo都提供了負(fù)載均衡功能。Feign使用Ribbon作為其負(fù)載均衡組件,而Dubbo則內(nèi)置了負(fù)載均衡機(jī)制。在負(fù)載均衡方面,Dubbo提供了更多的配置選項(xiàng)和策略,具有更強(qiáng)的靈活性。
  3. 服務(wù)發(fā)現(xiàn):Feign依賴于Eureka、Consul、Nacos等注冊中心實(shí)現(xiàn)服務(wù)發(fā)現(xiàn),而Dubbo則提供了內(nèi)置的服務(wù)發(fā)現(xiàn)機(jī)制。在服務(wù)發(fā)現(xiàn)的性能和穩(wěn)定性方面,Dubbo具有一定的優(yōu)勢。

三、區(qū)別分析

  1. 架構(gòu)差異:Feign基于SpringCloud體系,更適用于微服務(wù)架構(gòu)。而Dubbo則獨(dú)立于任何框架,具有更強(qiáng)的通用性。
  2. 適用場景:對于簡單的服務(wù)調(diào)用場景,F(xiàn)eign更加簡潔易用。而當(dāng)需要復(fù)雜的服務(wù)治理功能時(shí),Dubbo則更具優(yōu)勢。
  3. 擴(kuò)展性:Feign提供了豐富的注解和配置選項(xiàng),可以輕松地與SpringCloud的其他組件集成。而Dubbo則提供了豐富的SPI機(jī)制,使得擴(kuò)展更加靈活。
  4. 社區(qū)活躍度:Feign的社區(qū)相對活躍,隨著SpringCloud的發(fā)展,F(xiàn)eign也在不斷迭代和完善。Dubbo的社區(qū)雖然活躍度不如Feign,但憑借其多年的積累和沉淀,依然擁有大量的用戶和穩(wěn)定的支持者。

四、實(shí)戰(zhàn)性能對比

引入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>

創(chuàng)建公共API模塊

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

創(chuàng)建服務(wù)生產(chǎn)者

<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

### 服務(wù)端口號
server:
  port: 9800
#### nacos 注冊中心地址
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  
  #### 服務(wù)名
  application:
    name: tiger-producer  
dubbo:
  application:
    # 關(guān)閉qos端口避免單機(jī)多生產(chǎn)者端口沖突 如需使用自行開啟
    qos-enable: false
  registry:
    address: nacos://127.0.0.1:8848?username=nacos&password=nacos
    group: DEFAULT_GROUP
  protocol:
    # 如需使用 Triple 3.0 新協(xié)議 可查看官方文檔
    # 使用 dubbo 協(xié)議通信
    name: dubbo
    # dubbo 協(xié)議端口(-1表示自增端口,從20880開始)
    port: -1


  # 消費(fèi)者相關(guān)配置
  consumer:
    # 超時(shí)時(shí)間
    timeout: 3000
  scan:
    # 接口實(shí)現(xiàn)類掃描
    base-packages: com.tiger.**.dubbo

業(yè)務(wù)代碼

@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 調(diào)用耗時(shí) {}",elapsed);
    }
}

創(chuàng)建Application啟動器

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

創(chuàng)建服務(wù)消費(fèi)者

<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

### 服務(wù)端口號
server:
  port: 9400
#### nacos 注冊中心地址
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  
  #### 服務(wù)名
  application:
    name: tiger-consumer  
dubbo:
  application:
    # 關(guān)閉qos端口避免單機(jī)多生產(chǎn)者端口沖突 如需使用自行開啟
    qos-enable: false
  registry:
    address: nacos://127.0.0.1:8848?username=nacos&password=nacos
    group: DEFAULT_GROUP
  protocol:
    # 如需使用 Triple 3.0 新協(xié)議 可查看官方文檔
    # 使用 dubbo 協(xié)議通信
    name: dubbo
    # dubbo 協(xié)議端口(-1表示自增端口,從20880開始)
    port: -1
  # 消費(fèi)者相關(guān)配置
  consumer:
    # 超時(shí)時(shí)間
    timeout: 3000
  scan:
    # 接口實(shí)現(xiàn)類掃描
    base-packages: com.tiger.**.dubbo

創(chuàng)建消費(fèi)者調(diào)用代碼

@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();
    }




}

創(chuàng)建Feign調(diào)用用于對比性能測試

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


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


}

創(chuàng)建應(yīng)用啟動類

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


}

進(jìn)行測試

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

圖片圖片

從調(diào)用時(shí)間上可見 Dubbo的性能確實(shí)比Feign的性能好上不少

總結(jié)

總的來說,基于SpringCloudAlibaba框架下,F(xiàn)eign和Dubbo各有千秋。選擇使用哪一個(gè)組件取決于具體的項(xiàng)目需求和團(tuán)隊(duì)技術(shù)棧。對于需要快速構(gòu)建微服務(wù)架構(gòu)的項(xiàng)目,F(xiàn)eign是一個(gè)不錯(cuò)的選擇;而對于需要更多自定義和服務(wù)治理功能的項(xiàng)目,Dubbo可能更適合。在實(shí)際應(yīng)用中,也可以根據(jù)具體場景將兩者結(jié)合使用,以達(dá)到更好的效果。

責(zé)任編輯:武曉燕 來源: HELLO程序員
相關(guān)推薦

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

服務(wù)器售后服務(wù)

2009-07-02 18:50:43

2011-01-19 11:10:30

2010-03-18 14:54:46

主流無線技術(shù)

2011-03-08 10:55:00

亞馬遜谷歌微軟

2012-06-18 09:49:11

虛擬化

2009-02-06 14:26:37

UbuntuVistaWindows7

2010-05-24 18:15:34

SVN中Branch和

2016-08-10 20:58:21

ARM架構(gòu)X86架構(gòu)CPU

2010-07-14 13:38:51

Perl開發(fā)工具

2014-01-07 17:08:02

Java開源框架

2010-09-08 15:41:28

SIP協(xié)議棧

2017-09-10 14:29:03

眼力

2010-08-25 16:12:34

職場

2021-03-15 21:07:17

IT行業(yè)薪酬薪水
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號