SpringBoot项目集成sentinel降级快速失败

8 篇文章 0 订阅
3 篇文章 0 订阅

项目的配置:

pom.xml

 				<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-web-servlet</artifactId>
            <version>1.7.0</version>
        </dependency>

application.yml

  application:
    name: xxxProjectName
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080  #sentinel DashBoard 的端口号和地址
      eager: true

配置sentinel需要的bean 设置降级规则为快速失败等等

package com.lty.fsb.cabintech.config;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SentinelAspectConfiguration {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }

    @PostConstruct
    private void initRules() throws Exception {
        FlowRule rule1 = new FlowRule();
        rule1.setResource("getAllParkList");//@sentinelResource的value的名字
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);  // 规则类型
        rule1.setCount(1);   // 每秒调用最大次数为 1 次


        FlowRule rule2 = new FlowRule();
        rule2.setResource("checkHealthInfo");
        rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule2.setCount(1);   // 每秒调用最大次数为 1 次

        List<FlowRule> rules = new ArrayList<>();
        rules.add(rule1);
        rules.add(rule2);

        // 将控制规则载入到 Sentinel
        com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager.loadRules(rules);
    }
}
public class AmountOptionalController {
@Autowired
AmountOptionalService amountOptionalService;
  
    @RequestMapping("healthInfo")
    public Result health() {
        return  amountOptionalService.checkHealthInfo();
    }
}


   @Override
    @SentinelResource(value = "checkHealthInfo" ,fallback = "healthFallback")
    public Result checkHealthInfo() {
        return ResultUtil.requestSuccess("service health");
    }

    public Result healthFallback(){
        return ResultUtil.requestFaild("进入限流");
    }


@GetMapping("getAllParkList")
@SentinelResource(value = "getAllParkList",fallback = "getFailedParkList")
public Result getAllParkList() throws Exception {
   return ResultUtil.requestSuccess("");
}
    public Result getFailedParkList(){
        return ResultUtil.requestFaild("请求太快,请求已拦截");
    }

启动后请求接口 ,连续请求两次 结果:

在这里插入图片描述

在这里插入图片描述

配置dashboard:

文档   
https://github.com/alibaba/Sentinel/wiki

dashboard release 下载地址:
https://github.com/alibaba/Sentinel/releases

启动Sentinel服务端后台管理

  执行命令:java -Dserver.port=8080 -jar sentinel-dashboard-1.6.0.jar   默认8080端口

可用参数:

-Dcsp.sentinel.dashboard.server=ip:port启动端口号和Ip
-Dcsp.sentinel.api.port=8720
-Dproject.name=report-test

访问 localhost:8080

默认登录用户:sentinel
       pwd:sentinel

在这里插入图片描述 在这里插入图片描述

在编辑规则处可以修改配置的参数

目前本人就学习到这里 还有个问题:

2020-04-02 15:04:53.985 ERROR 80796 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher   : Failed to fetch metric from <http://192.168.18.162:8720/metric?startTime=1585811085000&endTime=1585811091000&refetch=false> (ConnectionException: Connection refused)

不知道如何处理 有大神的话 请私信 !!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值