标签Sentinel

Sentinel  限流

增加依赖
<!--阿里Sentinel限流-->
        <!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-core -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.7.1</version>
        </dependency>
声明资源
try(Entry entry = SphU.entry("getRole")) {
            //Sentinel要保护的资源定义
            response = restTemplate.postForObject("http://RBAC/role/test",  Arrays.asList("157875196366160022"),String.class);
            log.info("response={}", response);
        }catch (BlockException e) {
            log.info("限流");
            e.printStackTrace();
        }
定义规则

针对资源定义流控,限流,降级,熔断的规则

Component
public class SentinelConfig implements ApplicationListener<ContextRefreshedEvent> {

    /**
     * 系统启动完成后就执行的代码,在这里声明流控规则
     * @param contextRefreshedEvent
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        FlowRule rule = new FlowRule();
        rule.setRefResource("getRole");
        //QPS第秒请求的数量
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //每秒10个
        rule.setCount(10);
        //添加到FlowRuleManager
        List<FlowRule> rules = new ArrayList<FlowRule> ();
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
日志

当前用户下~/logs/csp/

DASHBOARD

https://github.com/alibaba/Sentinel
默认用户名密码都是sentinel
how to download
docker pull bladex/sentinel-dashboard:tagname
how to start
docker run –name sentinel -d -p 8858:8858 -d bladex/sentinel-dashboard:tagname
how to login web-dashboard
visit: http://localhost:8858/
account and password: [sentinel sentinel]
just enjoy 🙂
github
https://github.com/chillzhuang/SpringBlade
website
https://bladex.vip

docker install dashboard
docker run --name sentinel  -d -p 8858:8858  bladex/sentinel-dashboard:1.7.1
springboot
<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.7.1</version>
        </dependency>
项目启动参数
-Dcsp.sentinel.dashboard.server=DASHBOARD的服务地址
使用Spring Cloud Starter Alibaba Sentinel
1. 依赖
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
2. 配制
spring:
  application:
    #应用名
    name: client
  cloud:
    #sentinel流控
    sentinel:
      transport:
        port: 8719
        dashboard: http://192.168.88.108:9001
3. 使用注解定义资源
@GetMapping("/role3")
    //Sentinel要保护的资源定义
    @SentinelResource(value = "getRole",blockHandler = "doOnBlock")
    public Result role3(@AuthenticationPrincipal String userName){
        String response = response = restTemplate.postForObject("http://RBAC/role/test",  Arrays.asList("157875196366160022"),String.class);
        return Result.success(response);
    }
    public Result doOnBlock(@AuthenticationPrincipal String userName,BlockException exception) throws InterruptedException {
        log.info("userName [{}] doOnBlock By [{}]",userName,exception.getClass().getSimpleName());
        return Result.success(userName);
    }
在面版中配制规则更方便
数据持久化

可用zookeeper,apollo,nacos按自己需求配制
1. 下载dashboard源码,选择分支,导入项目,
– 修改pom中相应的 publisher sample 去掉scope
– 实现rule下的数据接口,样列在test包下的rule中,COPY
– 修改controller/v2/FlowControllerV2.java
ruleProvider的注入改flowRuleZookeeperProvider
rulePublisher的注入改flowRuleZookeeperPublisher
– 修改配制文件
application.properties更改默认的用户名及密码,服务器运行端口等
– 修改表态页面
webapp/resources/app/scripts/directives/sidebar/sidebar.html 流控规则的dashboard.flowv1->flow
– 修改数据地址
NacosConfig ConfigService

  1. 修改应用项目
    增加数据源依赖
<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-datasource-zookeeper -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-zookeeper</artifactId>
    <version>1.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-datasource-nacos -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
    <version>1.8.1</version>
    <scope>test</scope>
</dependency>

增加配制文件
#sentinel规则zoopeeker数据源地址

sentinel:
  zookeeper:
    address: 192.168.88.108:2781
    path: /sentinel_rule_config

去掉手动的规则SentinelConfig,改读取配制文件

@Component
public class SentinelConfig {

    @Value("${sentinel.zookeeper.address}")
    private String zkServer;

    @Value("${sentinel.zookeeper.path}")
    private String zkPath;

    @Value("${spring.application.name}")
    private String appName;
    ......
}