从 OpenFeign 到 RestClient:Spring Cloud 新时代的轻量化 HTTP 调用方案

OpenFeign 是 Spring Cloud 生态中最常用的声明式 HTTP 客户端,它通过 @FeignClient 注解让开发者能像调用本地方法一样调用远程服务。

一、为什么要替换 OpenFeign?

1. OpenFeign 的困境

OpenFeign 是 Spring Cloud 生态中最常用的声明式 HTTP 客户端,它通过 @FeignClient 注解让开发者能像调用本地方法一样调用远程服务。然而,随着 Netflix OSS 停止维护,Feign 逐渐陷入以下困境:

• 配置复杂度:当需要为不同服务配置独立的超时参数或编解码规则时,不得不在启动类堆积大量@FeignClient注解。

• 性能问题:动态代理机制在简化开发的同时,也带来了额外的反射开销。通过JProfiler抽样分析发现,在高并发场景下约有8%的CPU时间消耗在Feign的代理逻辑上。

• 异常处理盲区:默认配置下Feign会将4xx错误直接封装成FeignException抛出,需要开发者手动实现ErrorDecoder才能获取原始响应体。这种设计导致排查问题时总要反复查看日志链路,效率实在难以恭维。

因此,从 Spring Framework 6.1 开始,官方推出了全新的 RestClient,意在取代 RestTemplate、部分 WebClient,以及未来的 Feign。

二、RestClient 是什么?

RestClient 是 Spring 官方推出的新一代 HTTP 客户端,它提供:

• 同步调用(类似 RestTemplate)

• 响应式调用(基于 WebClient)

• 集成 Spring Cloud LoadBalancer,实现自动服务发现

• 与 Declarative HTTP Interface 结合,实现 Feign 风格的声明式调用

基本使用示例

@RestController
publicclassUserController {

    privatefinalRestClientrestClient= RestClient.builder()
            .baseUrl("http://user-service")
            .build();

    @GetMapping("/users/{id}")
    public User getUser(@PathVariable Long id) {
        return restClient.get()
                .uri("/users/{id}", id)
                .retrieve()
                .body(User.class);
    }
}

三、Declarative HTTP Interface:声明式调用新时代

Spring 官方提供了新的声明式调用方式,完全替代 Feign 的写法:

@HttpExchange("/users")
public interface UserClient {

    @GetExchange("/{id}")
    User getUser(@PathVariable("id") Long id);

    @PostExchange
    User createUser(@RequestBody User user);
}

创建代理:

@Configuration
publicclassClientConfig {

    @Bean
    public UserClient userClient(RestClient.Builder builder) {
        RestClientrestClient= builder.baseUrl("http://user-service").build();
        HttpServiceProxyFactoryfactory=
                HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient)).build();
        return factory.createClient(UserClient.class);
    }
}

这样调用:

@RestController
publicclassTestController {

    privatefinal UserClient userClient;

    publicTestController(UserClient userClient) {
        this.userClient = userClient;
    }

    @GetMapping("/demo")
    public User demo() {
        return userClient.getUser(1L);
    }
}

四、结合 CircuitBreaker 实现熔断

Spring Boot 3.x 推荐使用 Resilience4j 实现熔断降级。可以直接将其与 Declarative RestClient 结合。

1. 添加依赖

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot3</artifactId>
</dependency>

2. 定义熔断包装器

@Configuration
publicclassResilientClientConfig {

    @Bean
    public UserClient userClient(RestClient.Builder builder, CircuitBreakerRegistry registry) {
        CircuitBreakercb= registry.circuitBreaker("userServiceBreaker");
        RestClientrestClient= builder.baseUrl("http://user-service").build();

        HttpServiceProxyFactoryfactory=
                HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient))
                        .blockTimeout(Duration.ofSeconds(2))
                        .build();

        UserClientbaseClient= factory.createClient(UserClient.class);
        return id -> cb.executeSupplier(() -> baseClient.getUser(id));
    }
}

3. 配置熔断参数

resilience4j:
  circuitbreaker:
    instances:
      userServiceBreaker:
        slidingWindowSize: 20
        failureRateThreshold: 50
        waitDurationInOpenState: 10s

五、支持服务发现与负载均衡

引入 Spring Cloud LoadBalancer 后,RestClient 能像 Feign 一样使用逻辑服务名:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
    @Bean
    public RestClient restClient(RestClient.Builder builder) {
        return builder.baseUrl("http://user-service").build();
    }

    服务名将自动通过 Nacos / Eureka 解析,无需硬编码 IP。

    六、总结

    维度

    OpenFeign

    RestClient + Declarative HTTP Interface

    是否官方维护

    ❌ Netflix 停止维护

    ✅ Spring 官方维护

    性能

    一般

    优秀

    声明式调用

    自动发现

    熔断支持

    ✅(Hystrix/Resilience4j)

    ✅(Resilience4j)

    响应式

    适配 Spring Boot 3+

    ⚠️ 部分兼容

    ✅ 完全兼容

    一句话总结:

    在 Spring Boot 3.2+ 时代,RestClient + Declarative HTTP Interface + Resilience4j 是 Feign 的完美替代方案。

    七、实战项目结构图

    restclient-demo/
    ├── pom.xml
    ├── src/
    │   ├── main/
    │   │   ├── java/
    │   │   │   └── com/example/restclientdemo/
    │   │   │       ├── controller/
    │   │   │       │   └── TestController.java
    │   │   │       ├── client/
    │   │   │       │   ├── UserClient.java
    │   │   │       │   └── ResilientClientConfig.java
    │   │   │       ├── model/
    │   │   │       │   └── User.java
    │   │   │       └── RestclientDemoApplication.java
    │   │   └── resources/
    │   │       ├── application.yml
    │   │       └── logback-spring.xml
    │   └── test/
    │       └── java/
    │           └── com/example/restclientdemo/
    │               └── UserClientTests.java

    八、完整依赖列表(pom.xml 片段)

    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <!-- Spring Boot RestClient (Spring 6.1+) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webclient</artifactId>
        </dependency>
    
        <!-- Declarative HTTP Interface 支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
        <!-- 服务发现与负载均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    
        <!-- 熔断降级 Resilience4j -->
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot3</artifactId>
        </dependency>
    
        <!-- 注册中心(可选)Eureka 或 Nacos -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <optional>true</optional>
        </dependency>
    
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    
        <!-- 测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    AI大模型学习福利

    作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

    一、全套AGI大模型学习路线

    AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

    因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获取

    二、640套AI大模型报告合集

    这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

    因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

    三、AI大模型经典PDF籍

    随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。


    因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

    四、AI大模型商业化落地方案

    因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

    作为普通人,入局大模型时代需要持续学习和实践,不断提高自己的技能和认知水平,同时也需要有责任感和伦理意识,为人工智能的健康发展贡献力量

    评论
    成就一亿技术人!
    拼手气红包6.0元
    还能输入1000个字符
     
    红包 添加红包
    表情包 插入表情
     条评论被折叠 查看
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值