sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控

下载仪表盘

首先上官网下载仪表盘源码
https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard

启动仪表盘

最快的方法,直接run sentinel-dashboard项目,我这里1.8.2版本
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第1张图片
输入默认账号密码
sentinel
密码sentinel
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第2张图片
注意初始化是只有首页一个menu的

springCloud项目集成

官网是没有说要添加hibernate-validator依赖,但是跑项目时又提示要添加。

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.18.Final</version>
        </dependency>

application.yml集成,8080是连接仪表盘,port不设置的话默认8719

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080
      eager: true

添加测试的控制层,SentinelResource标记资源名

    @GetMapping("demo/userFeign")
    @SentinelResource("userFeign1")
    public String demoGo(){
        return "ok";
    }

限流配置

记得需要请求一下接口,才能看的到服务
设置为1秒只能有2个请求最多
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第3张图片
这下这个资源的限制就成功了

测试一下,建议使用jmeter测试
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第4张图片

sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第5张图片
可以看到,并发15个请求除了前两次的请求,其余的都失败了,限流完成

持久化配置

我配置的是把配置信息存到nacos上
项目添加依赖

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.8.2</version>
        </dependency>

配置nacos配置文件的地址

  spring:
  	cloud:
      datasource:
        ds2:
          nacos:
            server-addr: 47.112.235.58:8859
            data-id: mybtis-demo-sentinel
            data-type: json
            rule-type: flow

nacos的配置

[
    {
        "resource": "userFeign1",
        "limitApp": "default",
        "grade": 1,
        "count": 1,
        "controlBehavior":0,
        "clusterMode": false
    }
]

grade: threshold type, can be QPS or thread number
选择1就是配置QPS策略
limitApp:default
任意服务器访问都可以
count:单机阈值,举个例子比如有15个线程并发访问,只有1个可以获得数据
controlBehavior:
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第6张图片
再来看看源码,controlBehavior填的1,就直接拒绝了
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第7张图片
当选择RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第8张图片
逐步走完

熔断回调配置

    @GetMapping("userFeign")
    @SentinelResource(value = "userFeign1",fallback = "dofallBack")
    public String demoGo(){
        try {
            Object a= userFeign.demoOne(1);
        }catch (Exception e){
            throw new RuntimeException("xixi");
        }
        return "ok";
    }

    public String dofallBack(){
        return "jiangji";
    }

比如userFeign服务阻塞了,会直接返回jiangji给客户端

持久化熔断配置
nacos配置

[
{
    "resource":"userFeign1",
    "count":30,
    "timeWindow":10
}
]

查看仪表盘,看到了对应的配置,如果对字段含义不清楚的话,可以更新下nacos配置,然后去仪表盘查看配置
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第9张图片
spring配置,ds1,ds2这个任意

  spring:
  	cloud:
      datasource:
        ds1:
          nacos:
            server-addr: 47.112.235.58:8859
            group-id: ftui
            data-id: mybtis-demo-sentinel-degrade
            data-type: json
            rule-type: degrade

linux启动

官网下载jar包

nohup java -Dserver.port=8990 -Dcsp.sentinel.dashboard.server=localhost:8990-Dproject.name=sentinel-dashboard  -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=fantong -jar sentinel-dashboard-1.8.2.jar >sentinel.out 2>&1 &

sentinel与本机无法建立连接

只能将sentinel与本机服务部署在一个ip或者云服务器下才可行,或者想办法让云端能ping到本机ip

feign配置fallBack

feign:
  sentinel:
    enabled: true

请求feign失败了就会回调"失败"

@FeignClient(name = "app-user",fallback = UserFeignFallback.class)
public interface UserFeign {
    @GetMapping("demo/demoOne")
    Object demoOne(@SpringQueryMap Integer i);
}

@Component
public class UserFeignFallback implements UserFeign {
    @Override
    public Object demoOne(Integer i) {
        return "失败";
    }
}

gateway网关配置

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

spring:
  cloud:
    sentinel:
      transport:
        dashboard: http://localhost:8080

发送一个请求经过网关,就可以看到服务了,配置一下网关流控规则测试
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第10张图片

jmester测试,达到限流效果
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第11张图片

网关配置nacos,application.yml的配置和上面一样
配置文档参考官网
https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81
我写的一个限流的json供参考,相当于app-user服务,1秒内最多有两次并发请求
app-user为以下的id的名字

routes:
  - id: app-user
[
    {
        "resource":"app-user",
        "count":10,
        "grade": 1,
        "controlBehavior":0
    }
]

API 定义分组,细粒化的场景会需要到控制
首先要创建分组


@Configuration
public class GatewayConfiguration {
  
    @PostConstruct
    public void doInit()
    {
        // 加载网关限流规则
        initGatewayRules();
    }

    public void initGatewayRules(){
        Set<ApiDefinition> definitions = new HashSet<>();
        ApiDefinition api1 = new ApiDefinition("some_customized_api")
                .setPredicateItems(new HashSet<ApiPredicateItem>() {{
                    add(new ApiPathPredicateItem().setPattern("/userMicro/user/**")
                            .setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_PREFIX));
                }});
        definitions.add(api1);
        GatewayApiDefinitionManager.loadApiDefinitions(definitions);
    }
}

这时候sentinel就能看到这个api分组了
sentinel仪表盘、限流、熔断、持久化配置,gateway网关流控_第12张图片
nacos做配置,记得resourceMode要改下模式,不能用默认的了

[   
    {
        "resource":"some_customized_api",
        "count":2,
        "grade": 1,
        "controlBehavior":0,
        "resourceMode":"RESOURCE_MODE_CUSTOM_API_NAME"
    }
]

压测一下接口,发现限流成功

自启动脚本

#! /bin/bash
#chkconfig: 235 80 30
#description: 设置开机自启动
nohup /data/jdk/bin/java -Dserver.port=8718 -Dcsp.sentinel.dashboard.server=localhost:8718 -Dproject.name=sentinel-dashboard -jar /data/sentinel/sentinel-dashboard-1.8.6.jar > /dev/null 2>&1 &

systemctl启动

需求先编写sh脚本

#!/bin/env sh
nohup /data/soft/jdk/bin/java -Dserver.port=8718 -Dcsp.sentinel.dashboard.server=localhost:8718 -Dproject.name=sentinel-dashboard -jar /data/soft/sentinel/sentinel-dashboard-1.8.6.jar > /dev/null 2>&1 &

再编写sentinel.service文件

[Unit]
Description=sentinel
After=network.target

[Service]
Type=forking
User=holmes
ExecStart=/bin/bash /data/soft/sentinel/sentinel.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

你可能感兴趣的:(java,sentinel,gateway,hibernate)