首页 > 其他分享 >sentinel接入记录

sentinel接入记录

时间:2024-10-11 10:10:51浏览次数:1  
标签:记录 接入 stringSet alibaba sentinel logger com csp

      1.引入pom依赖

     

<!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

<!-- SpringCloud ailibaba sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-web-servlet</artifactId>
        </dependency>

        <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

        <!-- 引入 Sentinel 数据源 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
        </dependency>
<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
    </dependency>

 版本好统一按照

<version>1.8.3</version> 
为例。

2.本机启动
首先需要再idea的启动脚本添加命令
-Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=项目别名

 其中,-Dserver.port是自己的项目名称,-Dcsp.sentinel.dashboard.server  是sentinel开启控制台的端口号,-Dproject是自己的项目名称

3.再项目里面使用sentinel做限流

    sentinel 有两种方式都可以做限流和降级,一种方式是直接再spring bean里面使用fallback以及blockHandler注解,另外一种方式则是和openFeign整合。目前项目里面使用的是第一种方式。

     注意一点,使用sentinel需要相应的bean被spring容器加载进去。

   

@Service
public class QueryRecommentServiceImpl{

@SentinelResource(value = "queryRecommend", fallback = "getStaticData",blockHandler = "blockExceptionHandler")
    public List<String> queryRecommend(HomeRecommendStreamQuery query, HomeRecommendStreamDto streamDto, String sourceApp) {
  return null;
}



public List<String> getStaticData(HomeRecommendStreamQuery query, HomeRecommendStreamDto streamDto, String sourceApp) {
        logger.info("==========start sentinel 降级策略==========");
        try {
            Random random = new Random();
            int r = random.nextInt(100) + 1;
            logger.debug("========随机从缓存获取一组帖子id:" + r);
            Set<String> stringSet = gwmRedisTemplate.opsForValue().get(RedisConstants.THREAD_HOT_SCORE_KEY + r);

            logger.debug("=========随机从缓存获取帖子result=======" + JSONObject.toJSON(stringSet));
            if (CollectionUtils.isNotEmpty(stringSet)) {
                List<String> stringList = new ArrayList<>(stringSet);

                return stringList;
            } else {
                logger.debug("============读取默认配置数据========" + config.getPostIds());
                return Optional.ofNullable(config.getPostIds()).orElse(Collections.emptyList());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * blockHandler需要设置为static
     *
     * @param ex
     * @return
     */
    public  List<String> blockExceptionHandler(HomeRecommendStreamQuery query, HomeRecommendStreamDto streamDto, String sourceApp,BlockException ex) {
        try {
            Random random = new Random();
            int r = random.nextInt(100) + 1;
            logger.debug("========随机从缓存获取一组帖子id:" + r);
            Set<String> stringSet = gwmRedisTemplate.opsForValue().get(RedisConstants.THREAD_HOT_SCORE_KEY + r);

            logger.debug("=========随机从缓存获取帖子result=======" + JSONObject.toJSON(stringSet));
            if (CollectionUtils.isNotEmpty(stringSet)) {
                List<String> stringList = new ArrayList<>(stringSet);

                return stringList;
            } else {
                logger.debug("============读取默认配置数据========" + config.getPostIds());
                return Optional.ofNullable(config.getPostIds()).orElse(Collections.emptyList());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}

  

Sentinel 提供了 @SentinelResource 注解用于定义资源,并提供了 AspectJ 的扩展用于自动定义资源、处理 BlockException 等。使用 Sentinel Annotation AspectJ Extension 的时候需要引入以下依赖:

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-annotation-aspectj</artifactId>
    <version>x.y.z</version>
</dependency>
 之后再控制台界面就可以进行设置了。  另外就是如果想要每次启动让sentinel生效,需要再配置一个sentienl的yaml配置,每次项目启动可以自动生效,大概格式如下:  
[
    {
        "resource":"queryRecommend",
        "controlBehavior":0,
        "count":20,
        "grade":1,
        "limitApp":"default",
        "strategy":0
    }
]

 

标签:记录,接入,stringSet,alibaba,sentinel,logger,com,csp
From: https://www.cnblogs.com/thinkingandworkinghard/p/18457878

相关文章

  • 微信支付接口接入
    微信支付接口接入微信支付接入文档参考(https://pay.weixin.qq.com/docs/merchant/products/jsapi-payment/preparation.html)1.接入前准备具体步骤如下所示:1、选择接入模式:普通商户或普通服务商,官网说明地址:https://pay.weixin.qq.com/docs/merchant/development/glossary/......
  • 大数据-162 Apache Kylin 全量增量Cube的构建 Segment 超详细记录 多图
    点一下关注吧!!!非常感谢!!持续更新!!!目前已经更新到了:Hadoop(已更完)HDFS(已更完)MapReduce(已更完)Hive(已更完)Flume(已更完)Sqoop(已更完)Zookeeper(已更完)HBase(已更完)Redis(已更完)Kafka(已更完)Spark(已更完)Flink(已更完)ClickHouse(已更完)Kudu(已更完)Druid(已更完)Kylin(正在更新…)章节内容上节......
  • CF记录
    CF2019(div2,vp)比赛时网站炸了两次,有一次甚至连那个Oops的页面都没看到。/fnD.Speedbreaker做法比较多的一题。首先有一个带log但是比较简单的做法。求出\(a\)的后缀min\(s_i\)和前缀min\(p_i\),当出发点为\(x\)时,设\[b_i=\begin{cases}a_i=p_i,&i<x\\a_i=\m......
  • The 3rd Universal Cup 做题记录 (2)
    The3rdUniversalCup做题记录Stage0-Stage9:The3rdUniversalCup做题记录(1)Stage10-Stage19:The3rdUniversalCup做题记录(2)The3rdUniversalCup.Stage10:WestLakeA.ItalianCuisine复制一遍,枚举\(i\)维护右端点\(j\)。要求\((x,y)\)到过\((......
  • 2024.10 做题记录
    10.1gym104922I模拟赛T4。wqs二分,维护dp值和取到dp值的\(k\)的区间。倒序记录方案,要满足能落到合法区间中。10.2模拟赛T3建子序列自动机,DAG上dp并按字典序出边贪心记录方案。DAG链剖分。\(u\)向\(2f_v\gef_u\)的\(v\)连边,形成内向树。重边倍增,轻边跳一次......
  • 通信接入技术
    一、xDSL1、xDSL:利用电话线中的高频信息传输数据,高频信号损耗大,容易受噪声干扰。【速率越高,传输距离越近】1.1ADSL虚拟拨号:采用专门的协议PPPoverEthernet,拨号后直接由验证服务器进行检验,用户需输入用户名和密码,检验通过后就建立起一条高速的用户数字并分配相应的动态IP。......
  • 【学校训练记录】十月个人训练赛1题解
    A只需按照题目意思扩展h倍即可,先记录初始字符,打印时扩展为2*h根据题目公式打印`include<bits/stdc++.h>defineintlonglongusingnamespacestd;constintMAXN=100005;intn;inta[MAXN];charmp[105][105];signedmain(){inth,w;cin>>h>>w;for(inti=......
  • 【孤岛划分】分布式能源接入弹性配电网模型研究【IEEE33节点】(Matlab代码实现)
     目录......
  • ant-design 使用Modal组件报错问题记录
    打开modal组件会提示如下报错信息高版本chrome浏览器会出现这个问题 原因是:不能在获得焦点的元素或其祖先上使用aria-hidden解决方案:全局添加如下CSS,暂时将Modal中该属性的元素隐藏掉.ant-modaldiv[aria-hidden="true"]{display:none!important;} ......
  • [问题记录]SQLserver数据库是否可以新建多个.mdf文件?
    结论:1.可以,但只有第一个(.mdf)为当前数据库主文件。2.当有多个(.mdf)文件时,语句不会出现错误,但不符合命名约定,即命名约定不正确。3.数据库扩展名可以任意,官方文档中推荐主数据文件使用(.mdf),辅数据文件使用(.ndf),但如果使用例如:(.abc)作为文件后缀名,也是正确的。(具体官方文件截......