Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护

Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护

在微服务架构中,服务之间的调用往往依赖 Feign,而服务调用的稳定性又至关重要。本文将介绍如何将 Feign 与 Sentinel 结合使用,实现服务的容错保护(如降级与熔断),提升系统的健壮性与可用性。


一、引入依赖

我们创建一个新的微服务,作为 Feign 调用方。pom.xml 中添加如下依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

二、配置 application.yml

在配置文件中开启 Feign 对 Sentinel 的支持,并配置基本信息:

yaml 复制代码
server:
  port: 8380

feign:
  sentinel:
    enabled: true  # 开启 Sentinel 对 Feign 的支持

spring:
  application:
    name: feign
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080  # Sentinel 控制台地址
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  # Nacos 注册中心地址

三、创建 Feign 接口

我们通过 Feign 调用名为 provider 的服务:

java 复制代码
package com.southwind.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient("provider")
public interface ProviderFeign {
    
    @GetMapping("/index")
    String index();
}

四、编写 Controller

通过注入 Feign 接口,实现接口调用:

java 复制代码
package com.southwind.controller;

import com.southwind.feign.ProviderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {

    @Autowired
    private ProviderFeign providerFeign;

    @GetMapping("/index")
    public String index(){
        return providerFeign.index();
    }
}

五、自定义 Fallback 实现服务降级

当服务不可用或 Sentinel 触发熔断时,我们希望返回友好的降级提示,这就需要自定义 fallback 类:

1. 创建 fallback 实现类

java 复制代码
package com.southwind.fallback;

import com.southwind.feign.ProviderFeign;
import org.springframework.stereotype.Component;

@Component
public class ProviderFeignFallback implements ProviderFeign {
    @Override
    public String index() {
        return "index-fallback";
    }
}

2. 修改 FeignClient 注解,指定 fallback

java 复制代码
package com.southwind.feign;

import com.southwind.fallback.ProviderFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "provider", fallback = ProviderFeignFallback.class)
public interface ProviderFeign {

    @GetMapping("/index")
    String index();
}

Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护

在微服务架构中,服务之间的调用往往依赖 Feign,而服务调用的稳定性又至关重要。本文将介绍如何将 Feign 与 Sentinel 结合使用,实现服务的容错保护(如降级与熔断),提升系统的健壮性与可用性。


一、引入依赖

我们创建一个新的微服务,作为 Feign 调用方。pom.xml 中添加如下依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

二、配置 application.yml

在配置文件中开启 Feign 对 Sentinel 的支持,并配置基本信息:

yaml 复制代码
server:
  port: 8380

feign:
  sentinel:
    enabled: true  # 开启 Sentinel 对 Feign 的支持

spring:
  application:
    name: feign
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080  # Sentinel 控制台地址
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  # Nacos 注册中心地址

三、创建 Feign 接口

我们通过 Feign 调用名为 provider 的服务:

java 复制代码
package com.southwind.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient("provider")
public interface ProviderFeign {
    
    @GetMapping("/index")
    String index();
}

四、编写 Controller

通过注入 Feign 接口,实现接口调用:

java 复制代码
package com.southwind.controller;

import com.southwind.feign.ProviderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {

    @Autowired
    private ProviderFeign providerFeign;

    @GetMapping("/index")
    public String index(){
        return providerFeign.index();
    }
}

五、自定义 Fallback 实现服务降级

当服务不可用或 Sentinel 触发熔断时,我们希望返回友好的降级提示,这就需要自定义 fallback 类:

1. 创建 fallback 实现类

java 复制代码
package com.southwind.fallback;

import com.southwind.feign.ProviderFeign;
import org.springframework.stereotype.Component;

@Component
public class ProviderFeignFallback implements ProviderFeign {
    @Override
    public String index() {
        return "index-fallback";
    }
}

2. 修改 FeignClient 注解,指定 fallback

java 复制代码
package com.southwind.feign;

import com.southwind.fallback.ProviderFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "provider", fallback = ProviderFeignFallback.class)
public interface ProviderFeign {

    @GetMapping("/index")
    String index();
}

3.启动类添加@EnableFeignClients注解

java 复制代码
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }

}

六、运行与测试

  1. 启动 Sentinel Dashboard(默认端口为 8080)。
  2. 启动被调用服务 provider
  3. 启动当前项目 feign
  4. 浏览器访问:http://localhost:8380/index
  5. 停止 provider 服务,再次访问,即返回 fallback 内容:index-fallback

七、总结

模块 说明
Feign 简化 HTTP 请求调用
Sentinel 实现限流、熔断与降级保护
fallback 实现服务不可用时的降级逻辑
配置关键点 开启 feign.sentinel.enabled=true 并配置 fallback

通过本文的实践,我们实现了 Feign + Sentinel 的无缝集成,使服务调用具备更强的容错能力。建议大家在生产环境中为每个 Feign 接口都配置 fallback,防止服务雪崩。


如有帮助,欢迎点赞、评论、收藏!

后续将分享更多微服务高可用相关内容,敬请关注。

六、运行与测试

  1. 启动 Sentinel Dashboard(默认端口为 8080)。
  2. 启动被调用服务 provider
  3. 启动当前项目 feign
  4. 浏览器访问:http://localhost:8380/index
  5. 停止 provider 服务,再次访问,即返回 fallback 内容:index-fallback

七、总结

模块 说明
Feign 简化 HTTP 请求调用
Sentinel 实现限流、熔断与降级保护
fallback 实现服务不可用时的降级逻辑
配置关键点 开启 feign.sentinel.enabled=true 并配置 fallback

通过本文的实践,我们实现了 Feign + Sentinel 的无缝集成,使服务调用具备更强的容错能力。建议大家在生产环境中为每个 Feign 接口都配置 fallback,防止服务雪崩。


如有帮助,欢迎点赞、评论、收藏!

后续将分享更多微服务高可用相关内容,敬请关注。

相关推荐
Java成神之路-2 分钟前
通俗易懂理解 Spring MVC 拦截器:概念、流程与简单实现(Spring系列16)
java·spring·mvc
zhanghongbin013 分钟前
AI 采集器:Claude Code、OpenAI、LiteLLM 监控
java·前端·人工智能
计算机毕设vx_bysj68695 分钟前
【免费领源码】77196基于java的手机银行app管理系统的设计与实现 计算机毕业设计项目推荐上万套实战教程JAVA,node.js,C++、python、大屏数据可视化
java·mysql·智能手机·课程设计
忘梓.6 分钟前
墨色规则与血色节点:C++红黑树设计与实现探秘
java·开发语言·c++
hhh3u3u3u6 分钟前
Visual C++ 6.0中文版安装包下载教程及win11安装教程
java·c语言·开发语言·c++·python·c#·vc-1
星河耀银海9 分钟前
C++ 模板进阶:特化、萃取与可变参数模板
java·开发语言·c++
格鸰爱童话26 分钟前
向AI学习项目技能(五)
java·学习
程序员萌萌28 分钟前
Java之mysql实战讲解(三):聚簇索引与非聚簇索引
java·mysql·聚簇索引
好家伙VCC43 分钟前
**发散创新:基于Python与ROS的机器人运动控制实战解析**在现代机器人系统开发中,**运动控制**是实现智能行为的核心
java·开发语言·python·机器人
程途知微1 小时前
ConcurrentHashMap线程安全实现原理全解析
java·后端