Spring Cloud Alibaba-Feign整合Sentinel

第1步: 引入sentinel的依赖

<!--sentinel客户端-->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

第2步: 在配置文件中开启Feign对Sentinel的支持

feign:
sentinel:
 enabled: true

第3步: 创建容错类

//容错类要求必须实现被容错的接口,并为每个方法实现容错方案
@Component
@Slf4j
public class ProductServiceFallBack implements ProductService {
  @Override
  public Product findByPid(Integer pid) {
    Product product = new Product();
    product.setPid(-1);
    return product;
 }
}

第4步: 为被容器的接口指定容错类

//value用于指定调用nacos下哪个微服务
//fallback用于指定容错类
@FeignClient(value = "service-product", fallback = ProductServiceFallBack.class)
public interface ProductService {
  @RequestMapping("/product/{pid}")//指定请求的URI部分
  Product findByPid(@PathVariable Integer pid);
}

第5步: 修改controller

@RestController
@Slf4j
public class OrderController {
  @Autowired
  private OrderService orderService;
  @Autowired
  private ProductService productService;
  //下单--fegin
  @RequestMapping("/order/prod/{pid}")
  public Order order(@PathVariable("pid") Integer pid) {
    log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);
    //调用商品微服务,查询商品信息
    Product product = productService.findByPid(pid);
   
    if (product.getPid() == -1) {
      Order order = new Order();
      order.setPname("下单失败");
      return order;
   }
    log.info("查询到{}号商品的信息,内容是:{}", pid, JSON.toJSONString(product));
    //下单(创建订单)
    Order order = new Order();
    order.setUid(1);
    order.setUsername("测试用户");
    order.setPid(pid);
    order.setPname(product.getPname());
    order.setPprice(product.getPprice());
    order.setNumber(1);
    orderService.createOrder(order);
    log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
    try {
      Thread.sleep(100);
   } catch (InterruptedException e) {
      e.printStackTrace();
   }
    return order;
 }
}

第6步: 停止所有 shop-product 服务,重启 shop-order 服务,访问请求,观察容错效果

扩展: 如果想在容错类中拿到具体的错误,可以使用下面的方式

@FeignClient(
    value = "service-product",
    //fallback = ProductServiceFallBack.class,
    fallbackFactory = ProductServiceFallBackFactory.class)
public interface ProductService {
  //@FeignClient的value + @RequestMapping的value值 其实就是完成的请求地址
"http://service-product/product/" + pid
  @RequestMapping("/product/{pid}")//指定请求的URI部分
  Product findByPid(@PathVariable Integer pid);
}
@Component
public class ProductServiceFallBackFactory implements
FallbackFactory<ProductService> {
  @Override
  public ProductService create(Throwable throwable) {
    return new ProductService() {
      @Override
      public Product findByPid(Integer pid) {
        throwable.printStackTrace();
        Product product = new Product();
        product.setPid(-1);
        return product;
     }
   };
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuxuezhe_987

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值