欢迎光临
我们一直在努力

SpringCloud Alibaba 核心组件解析:服务调用和负载均衡

SpringCloud Alibaba 核心组件解析:服务调用与负载均衡

技术栈:Spring Boot 3.2.0 + Spring Cloud Alibaba 2023.0.0.0-RC1 + Nacos Discovery + OpenFeign + Sentinel


2.1 是什么 — Alibaba 体系的服务调用

Alibaba 体系的服务调用与 Spring Cloud 官方基本一致(RestTemplate + OpenFeign),核心差异在于:

  • 注册中心用 Nacos 替代 Consul/Eureka
  • 熔断降级用 Sentinel 替代 Resilience4J
  • Sentinel 与 OpenFeign 的整合更加原生

  • 2.2 为什么 — Sentinel + OpenFeign 整合的优势

    // 官方:OpenFeign + Resilience4J
    @FeignClient(value = "service-name")
    public interface Api { ... }
    // + yaml 配置 + @CircuitBreaker 注解

    // Alibaba:OpenFeign + Sentinel
    @FeignClient(value = "nacos-payment-provider", fallback = Fallback.class)
    public interface PayFeignSentinelApi { ... }
    // + feign.sentinel.enabled: true
    // → 更简洁,Sentinel 控制台可实时查看 Feign 调用数据

    核心优势:

    • Feign 的调用自动成为 Sentinel 的受保护资源
    • Sentinel Dashboard 可以看到每个 Feign 接口的 QPS、RT、异常数
    • 无需额外注解,只需配置一个 fallback 参数

    2.3 怎么做 — 完整步骤

    2.3.1 公共 Feign 接口(cloud-api-commons)

    // @FeignClient 的 fallback 参数 = Sentinel 降级的兜底类
    @FeignClient(value = "nacos-payment-provider",
    fallback = PayFeignSentinelApiFallBack.class)
    public interface PayFeignSentinelApi {

    @GetMapping("/pay/nacos/get/{orderNo}")
    ResultData getPayByOrderNo(@PathVariable("orderNo") String orderNo);
    }

    2.3.2 Fallback 实现类

    @Component // ← 必须加,否则 Spring 找不到
    public class PayFeignSentinelApiFallBack implements PayFeignSentinelApi {

    @Override
    public ResultData getPayByOrderNo(String orderNo) {
    return ResultData.fail(
    ReturnCodeEnum.RC500.getCode(),
    "对方服务宕机或不可用,FallBack服务降级o(╥﹏╥)o"
    );
    }
    }

    2.3.3 Consumer 配置

    # 激活 Sentinel 对 Feign 的支持(核心开关!)
    feign:
    sentinel:
    enabled: true # 不加这行,fallback 不生效!

    service-url:
    nacos-user-service: http://nacospaymentprovider

    2.3.4 Consumer 调用

    @RestController
    public class OrderNacosController {

    @Resource
    private PayFeignSentinelApi payFeignSentinelApi;

    @GetMapping(value = "/consumer/pay/nacos/get/{orderNo}")
    public ResultData getPayByOrderNo(@PathVariable("orderNo") String orderNo) {
    return payFeignSentinelApi.getPayByOrderNo(orderNo);
    }
    }

    2.3.5 RestTemplate 方式(同样支持)

    @Configuration
    public class RestTemplateConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
    return new RestTemplate();
    }
    }

    // 使用
    @GetMapping("/consumer/pay/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Integer id) {
    return restTemplate.getForObject(
    "http://nacos-payment-provider/pay/nacos/" + id, String.class);
    }


    2.4 降级链路全景

    Consumer 调用 payFeignSentinelApi.getPayByOrderNo()

    ├→ Feign 调用成功 → 返回正常结果 ✅

    └→ Feign 调用失败(超时/异常/Provider 宕机)

    └→ Sentinel 拦截 → 走 PayFeignSentinelApiFallBack.getPayByOrderNo()

    └→ 返回 "对方服务宕机或不可用…"


    2.5 面试题

    Q:@FeignClient 的 fallback 和 fallbackFactory 有什么区别?

    答:fallback 只返回降级结果;fallbackFactory 可以获取到异常信息(Throwable),根据不同的异常类型返回不同的降级数据,更加灵活。


    2.6 踩坑指南

    坑说明
    🔴 Fallback 类忘记加 @Component Spring 找不到实现类,Feign 启动报错
    🔴 忘记配置 feign.sentinel.enabled=true fallback 不生效,远程调用失败直接抛异常
    🔴 Fallback 方法的返回值要一致 Fallback 实现的方法返回值必须与接口完全一致

    2.7 章节总结

    • Sentinel + OpenFeign 三步走:① fallback=XXX.class ② @Component ③ feign.sentinel.enabled=true
    • 降级链路:@FeignClient.fallback(Feign 调用失败) → @SentinelResource.blockHandler(Sentinel 流控) 双层保护
    赞(0)
    未经允许不得转载:171主机测评 » SpringCloud Alibaba 核心组件解析:服务调用和负载均衡
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址