在微服务架构中,通常使用 OpenFeign 进行服务间的远程调用,并结合 Sentinel(或 Hystrix)来实现熔断与限流,以防止服务雪崩。
以下是基于 Spring Cloud Alibaba 生态,使用 OpenFeign + Sentinel 实现熔断限流的完整配置示例:
1. 引入相关依赖
在消费方(调用方)的 pom.xml 中引入 OpenFeign 和 Sentinel 依赖:
<!– OpenFeign 依赖 –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!– Sentinel 依赖 –>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!– 用于与 Sentinel 控制台通信(可选) –>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.8</version>
</dependency>
2. 修改配置文件
在消费方的 application.yml 中开启 Feign 对 Sentinel 的支持,并配置 Sentinel 控制台地址(如果需要):
feign:
sentinel:
enabled: true # 开启 Feign 对 Sentinel 的支持
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080 # Sentinel 控制台地址
port: 8719 # 与控制台通信的端口
3. 定义 Feign 接口与降级工厂类
注意: 推荐使用 FallbackFactory 而不是简单的 Fallback,因为 FallbackFactory 可以捕获并记录导致降级的具体异常信息,方便排查问题。
3.1 定义降级处理类
@Component
@Slf4j
public class UserClientFallbackFactory implements FallbackFactory<UserClient> {
@Override
public UserClient create(Throwable throwable) {
log.error("调用用户服务异常,触发降级逻辑", throwable);
return new UserClient() {
@Override
public User findById(Long id) {
// 返回一个默认的友好提示或兜底数据
return new User(id, "默认用户", "Feign调用异常降级返回");
}
};
}
}
3.2 定义 FeignClient 接口
在 @FeignClient 注解中指定 fallbackFactory:
@FeignClient(
value = "user-service", // 目标服务名
contextId = "userClient", // 区分多个 Feign 客户端的标识
fallbackFactory = UserClientFallbackFactory.class // 绑定降级工厂类
)
public interface UserClient {
@GetMapping("/users/{id}")
User findById(@PathVariable("id") Long id);
}
4. 启动类配置
确保在 Spring Boot 启动类上添加 @EnableFeignClients 注解,以开启 Feign 客户端扫描:
@SpringBootApplication
@EnableFeignClients
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
5. 补充:结合 @SentinelResource 注解实现方法级限流/熔断
如果除了 Feign 调用,还需要对 Controller 或 Service 中的具体方法进行限流和熔断,可以使用 @SentinelResource 注解:
@RestController
public class UserController {
@GetMapping("/users/{id}")
@SentinelResource(
value = "getUserById", // 资源名称
blockHandler = "getUserBlockHandler", // 限流/熔断触发时的处理方法
fallback = "getUserFallback" // 业务异常触发时的降级方法
)
public Result<User> getUserById(@PathVariable Long id) {
return Result.success(userClient.findById(id));
}
// 限流/熔断触发时的处理方法
public Result<User> getUserBlockHandler(Long id, BlockException e) {
return Result.fail("服务繁忙,请稍后再试");
}
// 其他异常触发时的降级方法
public Result<User> getUserFallback(Long id, Throwable e) {
return Result.success(new User(id, "默认用户", "异常降级"));
}
}
6. 规则配置(持久化)
Sentinel 默认将规则保存在内存中,重启会丢失。在生产环境中,通常结合 Nacos 进行规则持久化。在 application.yml 中配置数据源:
spring:
cloud:
sentinel:
datasource:
dgrade: # 自定义名称
nacos:
server-addr: ${spring.cloud.nacos.discovery.server–addr}
dataId: market–degrade–control
group-id: DEFAULT_GROUP
rule-type: degrade # 规则类型(如 flow, degrade)
data-type: json
然后在 Nacos 对应的 dataId 下配置 JSON 格式的熔断规则即可。






