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 被流控了!!!";
    }
}
  • 再次访问 可以正常返回流控处理消息
相关推荐
momo_via5 分钟前
maven下载与安装及在IDEA中配置maven
java·maven·intellij-idea
Deschen15 分钟前
设计模式-适配器模式
java·设计模式·适配器模式
开发游戏的老王19 分钟前
虚幻引擎虚拟制片入门教程 之 模型资源的导入
java·游戏引擎·虚幻
编啊编程啊程40 分钟前
【004】生菜阅读平台
java·spring boot·spring cloud·dubbo·nio
Craaaayon42 分钟前
【数据结构】二叉树-图解广度优先搜索
java·数据结构·后端·算法·宽度优先
岁岁岁平安1 小时前
Java+SpringBoot+Dubbo+Nacos快速入门
java·spring boot·nacos·rpc·dubbo
学习编程的Kitty2 小时前
算法——位运算
java·前端·算法
用户904706683572 小时前
如何使用 Spring MVC 实现 RESTful API 接口
java·后端
刘某某.2 小时前
数组和小于等于k的最长子数组长度b
java·数据结构·算法
程序员飞哥2 小时前
真正使用的超时关单策略是什么?
java·后端·面试