sentinel使用手册

1.引入依赖

xml 复制代码
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

2.yaml

yaml 复制代码
spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8090 #sentinel控制台地址
      http-method-specify: true # 是否设置请求方式作为资源名称
feign:
  sentinel:
    enabled: true # 开启feign对sentinel的支持

3.降级

3.1 引入依赖

xml 复制代码
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

3.2 创建降级方案

触发限流或熔断后的请求不一定要直接报错,也可以返回一些默认数据或者友好提示,用户体验会更好。

给FeignClient编写失败后的降级逻辑有两种方式:

  • 方式一:FallbackClass,无法对远程调用的异常做处理
  • 方式二:FallbackFactory,可以对远程调用的异常做处理,我们一般选择这种方式。

这里我们演示方式二的失败降级处理。

步骤一:在hm-api模块中给ItemClient定义降级处理类,实现FallbackFactory

java 复制代码
@Component
public class ItemClientFallBackFactory implements FallbackFactory<ItemClient> {
    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {
            @Override
            public PageDTO<ItemDTO> queryItemByPage(PageQuery query) {
                //这里写降级逻辑
                return null;
            }

            @Override
            public List<ItemDTO> queryItemByIds(List<Long> ids) {
                //这里写降级逻辑
                return List.of();
            }
        };
    }
}

步骤二: 在hm-api模块中的ItemClient接口中使用ItemClientFallbackFactory:

java 复制代码
@FeignClient("item-service", fallbackFactory = ItemClientFallBackFactory.class)
public interface ItemClient {
    @GetMapping("/items/page")
    PageDTO<ItemDTO> queryItemByPage(PageQuery query);

    @GetMapping("/items")
    List<ItemDTO> queryItemByIds(@RequestParam("ids") List<Long> ids);
相关推荐
Owen__z6 分钟前
GEE统计特定区域特定时间上的Landsat/Sentinel的影像信息
python·sentinel·gee·geemap·landsat
我要去腾讯9 分钟前
Springcloud核心组件之Sentinel详解
java·spring cloud·sentinel
哦你看看5 天前
Redis Sentinel哨兵集群
linux·redis·bootstrap·sentinel
2501_938790076 天前
Spring Cloud Alibaba 2023 版:Nacos 服务发现与 Sentinel 限流的整合方案
sentinel·服务发现
青鱼入云6 天前
Sentinel介绍
微服务·sentinel
青鱼入云6 天前
Feign如何集成Sentinel
spring cloud·微服务·sentinel
一周困⁸天.8 天前
Redis Sentinel哨兵集群
redis·bootstrap·sentinel
一条懒鱼6669 天前
Redis Sentinel哨兵集群
数据库·redis·sentinel
2301_797604249 天前
d44:Sentinel 微服务流量控制与 Seata 分布式事务
分布式·微服务·sentinel
新手小白*9 天前
Redis Sentinel哨兵集群
数据库·redis·sentinel