sentinel openFeign 异常处理

1、过程

若 openFeign 有fallback,则走fallback的兜底回调,否则走全局异常处理

2、fallbcak指定的是类

3、openfeign的客户端

package com.wt.order.feign;

import com.wt.order.feign.fallback.ProductFeignClientFallBack;
import com.wt.product.bean.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "services-product", fallback = ProductFeignClientFallBack.class)
public interface ProductFeignClient {
    @GetMapping("product/{id}")
    Product getProductById(@PathVariable Long id);
}

4、ProductFeignClientFallBack.class

package com.wt.order.feign.fallback;
import java.math.BigDecimal;

import com.wt.order.feign.ProductFeignClient;
import com.wt.product.bean.Product;
import org.springframework.stereotype.Component;

@Component
public class ProductFeignClientFallBack implements ProductFeignClient {
    // 兜底返回
    @Override
    public Product getProductById(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setPrice(new BigDecimal("0"));
        product.setProductName("未知产品");
        product.setNum(0);

        return product;
    }
}

 

posted @ 2025-06-10 23:39  市丸银  阅读(52)  评论(0)    收藏  举报