Spring 微服务中远程调用 Dubbo 与 Feign的对比

Dubbo 和 Feign 是分布式系统中用于服务调用的两种方式,适用于不同的场景和技术栈:

Dubbo

Dubbo 是一个高性能、轻量级的开源 Java RPC 框架,最初由阿里巴巴开发,现已捐献给 Apache 基金会。它主要解决微服务架构中服务的注册、发现和远程调用问题。

核心组成

  1. 服务容器(Service Container) 服务提供者在容器中启动并暴露服务,Dubbo 提供多种方式来启动服务,比如 Spring 容器。
  2. 注册中心(Registry Center) 用于服务注册和发现。常用的注册中心有:
    1. Zookeeper:常用的分布式协调服务。
    2. Nacos:阿里开源的服务注册和配置管理平台,支持动态配置管理。(推荐)
    3. Redis:轻量级实现,但不适合大规模服务注册。
  3. 协议支持(Protocol Support)
    1. Dubbo 协议:基于 Netty 的二进制高性能协议,适合高并发场景。
    2. HTTP 协议:方便与外部系统集成。
    3. gRPC 协议:支持跨语言调用。
  4. 远程调用(RPCRemote Procedure Call Dubbo 实现了 RPC,允许你在一台机器上调用另一台机器上的方法,就像调用本地方法一样。 RPC 的特点:
    1. 透明性:屏蔽了底层网络通信,调用者无需关心网络传输细节。
    2. 高效性:使用二进制序列化协议,性能优于基于文本的 HTTP 请求。

Dubbo 的特性

  1. 高性能
    1. 使用 Netty 作为通信框架。
    2. 支持多种序列化方式(如 Hessian、JSON)。
  2. 动态服务发现 服务启动时自动注册到注册中心,消费者根据需要动态获取服务列表。
  3. 负载均衡 内置多种负载均衡策略:
    1. 随机(Random):随机选择服务实例。
    2. 轮询(Round Robin):按顺序轮流选择实例。
    3. 一致性哈希Consistent Hashing:确保同一请求分配到同一服务实例。
  4. 服务治理 提供流量控制、熔断机制和服务降级功能,确保系统的高可用性。

Dubbo 示例(使用 Nacos 作为注册中心)

环境准备
  1. 依赖配置(Mavenpom.xml 中添加 Dubbo 和 Nacos 的相关依赖。
复制代码
<dependencies>
    <!-- Dubbo 依赖 -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>3.2.0</version>
    </dependency>
    <!-- Spring Boot Dubbo Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>3.2.0</version>
    </dependency>
    <!-- Nacos 注册中心依赖 -->
    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
        <version>2.2.3</version>
    </dependency>
</dependencies>
复制代码

服务提供者
  1. 定义接口
同样定义 Dubbo 服务接口,用于服务提供者和消费者之间的通信。
package com.example.dubbo.api;
public interface UserService {
    String getUserById(Long id);
}
  1. 服务实现
实现接口,并通过 @DubboService 暴露服务。
复制代码
package com.example.dubbo.provider;
import com.example.dubbo.api.UserService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService // 使用 Dubbo 暴露服务
public class UserServiceImpl implements UserService {
    @Override
    public String getUserById(Long id) {
        return "用户ID:" + id + ",姓名:张三";
    }
}
复制代码
  1. 配置文件
application.yml 中配置 Dubbo 和 Nacos 的注册中心。
dubbo:
  application:
    name: dubbo-provider
  registry:
    address: nacos://127.0.0.1:8848 # 指定 Nacos 地址
  protocol:
    name: dubbo
    port: 20880

服务消费者
  1. 接口引用
通过 @DubboReference 引用远程服务。
复制代码
package com.example.dubbo.consumer;
import com.example.dubbo.api.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @DubboReference // 引用远程服务
    private UserService userService;
    @GetMapping("/user")
    public String getUser(Long id) {
        return userService.getUserById(id);
    }
}
复制代码
  1. 配置文件
application.yml 中配置 Dubbo 和 Nacos。
dubbo:
  application:
    name: dubbo-consumer
  registry:
    address: nacos://127.0.0.1:8848 # 指定 Nacos 地址

关键说明
  1. Nacos 安装 确保已启动 Nacos 服务。如果未安装,可以从 Nacos 官方下载,并通过以下命令启动:
sh startup.sh -m standalone
  1. Nacos 作为注册中心 Dubbo 会自动在 Nacos 中注册服务,无需额外配置。
  2. 启动与测试
    1. 先启动服务提供者(dubbo-provider)。
    2. 再启动服务消费者(dubbo-consumer)。
    3. 访问 http://localhost:8080/user?id=1,验证服务调用是否成功。

常见术语扩展

RPCRemote Procedure Call
远程过程调用,指一台计算机上的程序调用另一台计算机上的服务或方法,就像调用本地方法一样。
  • 实现方式:序列化请求数据 -> 网络传输 -> 反序列化请求 -> 执行方法 -> 返回结果。
  • Dubbo 的优势:通过 Netty 和 Dubbo 协议优化了数据传输的效率。

 

Feign

Feign 是一个声明式 HTTP 客户端,用于简化微服务之间的 REST API 调用。它是 Spring Cloud 体系中的重要组件,与 Ribbon、Hystrix 等工具无缝集成。

Feign 的工作原理

  1. 定义一个接口,用注解标记 API 的路径和请求方法。
  2. Feign 动态代理根据接口定义生成实现类。
  3. 调用接口时,Feign 自动发起 HTTP 请求并返回结果。
@FeignClient(name = "user-service")
public interface UserServiceClient {
    @GetMapping("/users/{id}")
    User getUserById(@PathVariable("id") Long id);
}

Feign 的特点

  1. 声明式调用 通过注解(如 @GetMapping@PostMapping)定义请求,无需手动构建 HTTP 请求。
  2. Ribbon 集成 Feign 默认使用 Ribbon 作为客户端负载均衡器,支持多种负载均衡策略(如随机、轮询)。
  3. Hystrix 支持(熔断机制) Feign 可以结合 Hystrix 实现服务的熔断和降级,当调用超时或失败时返回默认值。
  4. 编码器解码器 Feign 提供默认的请求/响应编码器(如 JSON),可以自定义处理复杂的序列化和反序列化。

以下是关于 DubboFeign 的详细实现代码示例,分别展示了它们在微服务中的典型使用方式。

Feign 示例

环境准备
  1. 依赖配置(Mavenpom.xml 文件中添加 Feign 和 Spring Cloud 相关依赖。
复制代码
<dependencies>
    <!-- Spring Cloud Starter -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>4.0.0</version>
    </dependency>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
复制代码

服务提供者
  1. 定义 RESTful 接口
创建一个简单的 REST 控制器。
复制代码
package com.example.feign.provider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @GetMapping("/users/{id}")
    public String getUserById(@PathVariable Long id) {
        return "用户ID:" + id + ",姓名:李四";
    }
}
复制代码
  1. 配置文件
application.yml 中配置服务提供者的端口。
server:
  port: 8081
启动后,服务提供者将运行在 http://localhost:8081

服务消费者
  1. 定义 Feign 客户端
通过 @FeignClient 定义一个接口,用于远程调用。
复制代码
package com.example.feign.consumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "user-service", url = "http://localhost:8081") // 绑定到服务提供者
public interface UserServiceClient {
    @GetMapping("/users/{id}")
    String getUserById(@PathVariable("id") Long id);
}
复制代码
  1. 调用服务
在控制器中调用 Feign 客户端。
复制代码
package com.example.feign.consumer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    private final UserServiceClient userServiceClient;
    public UserController(UserServiceClient userServiceClient) {
        this.userServiceClient = userServiceClient;
    }
    @GetMapping("/user")
    public String getUser(Long id) {
        return userServiceClient.getUserById(id);
    }
}
复制代码
  1. 配置文件
配置消费者的端口和其他信息。
server:
  port: 8082
启动后,访问 http://localhost:8082/user?id=1 将返回来自 Feign 服务调用的数据。

常见术语扩展

Hystrix
Hystrix 是 Netflix 开源的服务熔断和容错框架,用于提高分布式系统的容错性和稳定性。 核心功能:
  • 熔断器(Circuit Breaker):当服务故障率过高时,熔断器会中断请求,保护系统。
  • 请求隔离:通过线程池隔离不同服务的请求,防止连锁故障。
  • 降级处理:在服务不可用时,返回默认值。
Ribbon
Ribbon 是 Netflix 提供的客户端负载均衡器,支持多种负载均衡策略,并与 Feign 深度集成。

 

对比总结

特性 Dubbo Feign
通信协议 自定义 RPC(二进制协议,性能高) HTTP(基于 RESTful,灵活性强)
调用方式 面向接口(方法调用) RESTful 风格
注册中心支持 Zookeeper、Nacos 等 Eureka、Nacos 等
负载均衡 内置多种策略 Ribbon
熔断与降级 支持(如 Sentinel) Hystrix 或 Resilience4j
使用场景 内部系统高性能服务间调用 对外 HTTP API 调用
 
posted @   枯藤老樹昏鴉  阅读(170)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示