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 被流控了!!!";
    }
}
  • 再次访问 可以正常返回流控处理消息
相关推荐
BerrySen178几秒前
迈向 Next-Gen Java:企业级高并发架构演进与大模型 Agent 落地深度实战
java·开发语言·架构
technology_x16 分钟前
2026年财务报表分析软件测评:兼容与安全解析
java·服务器·前端
兰令水19 分钟前
hot100【acm版】【2026.7.25/26打卡-java版本】
java·算法·排序算法
Nebula_g39 分钟前
Java实现本地Socket通信(三)
java·开发语言·学习·socket·可视化
一水43 分钟前
java运行排错,新码旧jar
java·开发语言·jar
wuqingshun3141591 小时前
JAVA中的注解原理是什么?
java
Python+991 小时前
Java 编程语言入门指南
java·开发语言
huahailing10243 小时前
Spring Boot 集成 XXL-Job 完整实现方案(支持动态CRUD)
java·spring boot·后端
薛定谔的猫-菜鸟程序员3 小时前
基于 Electron 的本地短视频解析与下载工具:架构设计与工程实践
java·electron·音视频
音符犹如代码4 小时前
Arthas Profiler 火焰图实战:CPU 热点在哪一目了然
java·jvm·spring boot