Sentinel 流控-链路模式

链路模式

A B C 三个服务

A 调用 C

B 调用 C

C 设置流控 ->链路模式 -> 入口资源是 A

A、B 服务

java 复制代码
package com.learning.springcloud.order.controller;


import com.learning.springcloud.order.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 链路模式:
 *    条件:
 *      - A ---> C; B -> C
 *      - C 设置流控规则 入口资源是A
 */
@RestController
@RequestMapping("/lianlu")
public class LianLuController {

    @Autowired
    BaseService baseService;

    @RequestMapping("/A")
    public Object A() {
        String s = baseService.queryC();
        return "hi, A;" + s;
    }

    @RequestMapping("/B")
    public Object B() {
        String s = baseService.queryC();
        return "hi, B;" + s;
    }

}

C 服务

java 复制代码
package com.learning.springcloud.order.service;

public interface BaseService {

    public String queryC();
}
java 复制代码
package com.learning.springcloud.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.learning.springcloud.order.service.BaseService;
import org.springframework.stereotype.Service;

@Service
public class BaseServiceImpl implements BaseService {

    @Override
    @SentinelResource(value = "queryC")
    public String queryC() {
       return "查询C";
    }
}

控制台

设置链路收集

java 复制代码
server:
  port: 8061

spring:
  application:
    name: order-sentinel
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
      web-context-unify: false # 默认请求链路进行收敛

设置流控规则

  • 链路 入口 A

访问

**问题:**为啥没有流控处理的消息而是访问报错???

问题解决

分析:

  1. 使用 注解 @SentinelResource 则无法使用全局异常处理

  2. 增加注解 blockHandler 属性以及方法

java 复制代码
package com.learning.springcloud.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.learning.springcloud.order.service.BaseService;
import org.springframework.stereotype.Service;

@Service
public class BaseServiceImpl implements BaseService {
    @Override
    @SentinelResource(value = "queryC", blockHandler = "blockHandlerForQueryC")
    public String queryC() {
       return "查询C";
    }

    public String blockHandlerForQueryC(BlockException be) {
        return "queryC 被流控了!!!";
    }
}
  • 再次访问 可以正常返回流控处理消息
相关推荐
麻芝汤圆2 分钟前
在 IDEA 中写 Spark 程序:从入门到实践
java·大数据·hadoop·分布式·安全·spark·intellij-idea
LUCIAZZZ22 分钟前
分布式链路追踪理论
java·分布式·中间件·操作系统·链路追踪
未来的JAVA高级开发工程师23 分钟前
悲观锁与乐观锁
java·数据库
weixin_4405974525 分钟前
Spring Boot 中的条件注解
java·spring boot·后端
风象南43 分钟前
SpringBoot中6种拦截器使用场景
java·spring boot·后端
梦之马2 小时前
spring boot 2升级3 记录
java·spring boot·后端
yuren_xia3 小时前
Spring MVC中自定义日期类型格式转换器
java·spring·mvc
GottdesKrieges4 小时前
OceanBase数据库磁盘空间管理
java·数据库·oceanbase
Themberfue5 小时前
Redis ⑦-set | Zset
java·开发语言·数据库·redis·sql·缓存
此木|西贝8 小时前
【设计模式】享元模式
java·设计模式·享元模式