微服務(wù)架構(gòu)下Feign和Dubbo的性能大比拼,到底鹿死誰手?
概述
隨著微服務(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ù)治理功能。
二、性能對比
- 調(diào)用性能:在單次調(diào)用方面,F(xiàn)eign的性能表現(xiàn)略遜于Dubbo。由于Feign的自動生成HTTP請求機(jī)制,其性能相較于Dubbo的直接RPC調(diào)用會有一定的損失。然而,對于大多數(shù)應(yīng)用而言,這種性能差異并不明顯。
- 負(fù)載均衡:Feign和Dubbo都提供了負(fù)載均衡功能。Feign使用Ribbon作為其負(fù)載均衡組件,而Dubbo則內(nèi)置了負(fù)載均衡機(jī)制。在負(fù)載均衡方面,Dubbo提供了更多的配置選項(xiàng)和策略,具有更強(qiáng)的靈活性。
- 服務(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ū)別分析
- 架構(gòu)差異:Feign基于SpringCloud體系,更適用于微服務(wù)架構(gòu)。而Dubbo則獨(dú)立于任何框架,具有更強(qiáng)的通用性。
- 適用場景:對于簡單的服務(wù)調(diào)用場景,F(xiàn)eign更加簡潔易用。而當(dāng)需要復(fù)雜的服務(wù)治理功能時(shí),Dubbo則更具優(yōu)勢。
- 擴(kuò)展性:Feign提供了豐富的注解和配置選項(xiàng),可以輕松地與SpringCloud的其他組件集成。而Dubbo則提供了豐富的SPI機(jī)制,使得擴(kuò)展更加靈活。
- 社區(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á)到更好的效果。