9.7Feign与Sentinel集成

分类: Spring Cloud OpenFeign

Feign 与 Sentinel 集成

Feign 与 Sentinel 集成可以实现熔断降级和限流控制。本节将学习 Feign 与 Sentinel 集成。

本节将学习:Sentinel 适配器、熔断降级、限流控制,以及降级处理。

Sentinel 适配器

依赖添加

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId> </dependency>

配置文件

spring: cloud: sentinel: transport: dashboard: localhost:8080 datasource: nacos: server-addr: localhost:8848 dataId: sentinel-rules groupId: DEFAULT_GROUP rule-type: flow

Feign 配置

feign: sentinel: enabled: true

熔断降级

降级规则配置

@Configuration public class SentinelConfig { @PostConstruct public void initRules() { List<DegradeRule> rules = new ArrayList<>(); DegradeRule rule = new DegradeRule(); rule.setResource("GET:http://user-service/api/users/{id}"); rule.setGrade(RuleConstant.DEGRADE_GRADE_RT); rule.setCount(100); rule.setTimeWindow(10); rules.add(rule); DegradeRuleManager.loadRules(rules); } }

降级处理

@FeignClient(name = "user-service", fallback = UserServiceFallback.class) public interface UserServiceClient { @GetMapping("/api/users/{id}") User getUser(@PathVariable Long id); } @Component public class UserServiceFallback implements UserServiceClient { @Override public User getUser(Long id) { return new User(id, "Fallback User"); } }

限流控制

流控规则配置

@PostConstruct public void initFlowRules() { List<FlowRule> rules = new ArrayList<>(); FlowRule rule = new FlowRule(); rule.setResource("GET:http://user-service/api/users/{id}"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); rule.setCount(10); rules.add(rule); FlowRuleManager.loadRules(rules); }

限流处理

@FeignClient(name = "user-service", fallback = UserServiceFallback.class) public interface UserServiceClient { @GetMapping("/api/users/{id}") User getUser(@PathVariable Long id); }

降级处理

Fallback 类

@Component public class UserServiceFallback implements UserServiceClient { @Override public User getUser(Long id) { return new User(id, "Fallback User"); } @Override public List<User> getUsers() { return Collections.emptyList(); } }

FallbackFactory

@Component public class UserServiceFallbackFactory implements FallbackFactory<UserServiceClient> { @Override public UserServiceClient create(Throwable cause) { return new UserServiceClient() { @Override public User getUser(Long id) { log.error("Feign call failed", cause); return new User(id, "Fallback User"); } }; } }

使用 FallbackFactory

@FeignClient(name = "user-service", fallbackFactory = UserServiceFallbackFactory.class) public interface UserServiceClient { @GetMapping("/api/users/{id}") User getUser(@PathVariable Long id); }

规则持久化

Nacos 配置

spring: cloud: sentinel: datasource: flow: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-flow-rules groupId: DEFAULT_GROUP rule-type: flow degrade: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-degrade-rules groupId: DEFAULT_GROUP rule-type: degrade

官方资源

本节小结

在本节中,我们学习了:

第一个是 Sentinel 适配器。 如何集成 Sentinel。

第二个是熔断降级。 如何配置熔断降级规则。

第三个是限流控制。 如何配置限流规则。

第四个是降级处理。 如何使用 Fallback 和 FallbackFactory。

这就是 Feign 与 Sentinel 集成。集成 Sentinel 后,可以实现熔断降级和限流控制。

在下一节,我们将学习 OpenFeign 最佳实践。