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 被流控了!!!";
    }
}
  • 再次访问 可以正常返回流控处理消息
相关推荐
超梦dasgg7 分钟前
Sentinel生产环境实战全解
java·微服务·sentinel
青云计划10 分钟前
MySQL技术文档
java·mysql
qq_25183645710 分钟前
基于java 汽车检修管理系统设计与实现 论文
java·开发语言·汽车
量子炒饭大师14 分钟前
【Linux系统编程】Cyberpunk在霓虹丛林中构建堡垒 ——【基础开发工具(1)】一文带你初步了解 软件包管理器 并 快速上手 yum和apt 工具
java·linux·运维·apt·yum·软件包管理器
Finger#0000FF19 分钟前
从零上手VibeCoding(ClaudeCode+DeepSeek V4.Pro)
java·人工智能·ai编程·vibe coding·claudecode
木子墨51619 分钟前
系统设计面试 | 实现一个限流器:滑动窗口 → 令牌桶 → 漏桶
java·开发语言·数据结构·数据库·面试·职场和发展
吴声子夜歌32 分钟前
Java——synchronized
java·synchronized
不知名的忻38 分钟前
交换排序:冒泡排序 vs 快速排序(Java)
java·算法·排序算法
程序员阿明39 分钟前
spring boot + vue3 实现RSA加密解密
java·spring boot·后端
wok1571 小时前
IDEA 无法识别 OkHttpClient?cannot resolve symbol问题解决
java·ide·intellij-idea