微服務架構下Feign和Dubbo的性能大比拼,到底鹿死誰手?
概述
隨著微服務架構的普及,服務間的通信和調用成為了關鍵問題。在SpringCloudAlibaba框架下,Feign和Dubbo是兩種常用的服務調用組件。本文將對兩者進行性能對比及區別分析。
一、Feign與Dubbo概述
Feign是一個聲明式的Web服務客戶端,使得編寫HTTP客戶端變得更簡單。通過簡單的注解,Feign將自動生成HTTP請求,使得服務調用更加便捷。而Dubbo是一個高性能、輕量級的Java RPC框架,提供了豐富的服務治理功能。
二、性能對比
- 調用性能:在單次調用方面,Feign的性能表現略遜于Dubbo。由于Feign的自動生成HTTP請求機制,其性能相較于Dubbo的直接RPC調用會有一定的損失。然而,對于大多數應用而言,這種性能差異并不明顯。
- 負載均衡:Feign和Dubbo都提供了負載均衡功能。Feign使用Ribbon作為其負載均衡組件,而Dubbo則內置了負載均衡機制。在負載均衡方面,Dubbo提供了更多的配置選項和策略,具有更強的靈活性。
- 服務發現:Feign依賴于Eureka、Consul、Nacos等注冊中心實現服務發現,而Dubbo則提供了內置的服務發現機制。在服務發現的性能和穩定性方面,Dubbo具有一定的優勢。
三、區別分析
- 架構差異:Feign基于SpringCloud體系,更適用于微服務架構。而Dubbo則獨立于任何框架,具有更強的通用性。
- 適用場景:對于簡單的服務調用場景,Feign更加簡潔易用。而當需要復雜的服務治理功能時,Dubbo則更具優勢。
- 擴展性:Feign提供了豐富的注解和配置選項,可以輕松地與SpringCloud的其他組件集成。而Dubbo則提供了豐富的SPI機制,使得擴展更加靈活。
- 社區活躍度: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可能更適合。在實際應用中,也可以根據具體場景將兩者結合使用,以達到更好的效果。