栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Sentiel控制台使用,Nacos控制限流,集群控制限流使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Sentiel控制台使用,Nacos控制限流,集群控制限流使用

流量控制
  • 并发线程数
  • QPS

  • 资源
  • 规则
  • Entry->请求
限制规则的核心因素FlowRule
  • resoure
  • count
  • grade
  • limitApp
  • ControlBehavior
维度
并发线程数
  • Hystrix线程池隔离
  • Sentinel 并发线程数

QPS(TPS)
策略(流量控制的策略)
  • 直接拒绝

  • warm up(冷启动i)

  • 匀速排队

    通过ControlBehavior属性来控制策略

基于调用关系来做流量控制(维度)
  • FlowRule

  • 根据调用方来限流
    limitApp

  • 根据调用链路入口来限流

  • 针对具有关系的资源来限流

Sentinel控制台

1、下载
github地址::https://github.com/alibaba/Sentinel/releases

2、启动命令

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.4.jar

配置:
3、配置文件挂载到控制台上

挂载成功

4、Jmeter测试

  • 打开Jmeter新建一个Http请求
  • 配置线程池
  • 配置http请求
  • 运行测试,观察控制台可以看见波动

动态限流规则
  • config center: zookeeper / apollo / etcd / redis / Consul / Eureka / Nacos
  • 规则的感知(Pull,Push)
DataSource(接口)
  • pull
  • push

Sentinel扩展(InitFunc实现)

原来代码是放在主方法里面的,例如:

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SpringBootSentinelApplication {

    public static void main(String[] args) {
        initFlowRule();
        SpringApplication.run(SpringBootSentinelApplication.class, args);
    }
    
    private static void initFlowRule(){
        List rules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        //绑定规则名称,针对那个规则设置规则
        flowRule.setResource("doTest");
        //QPS或者并发数
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //QPS=5
        flowRule.setCount(5);
        //添加规则
        rules.add(flowRule);
            FlowRuleManager.loadRules(rules);
    }
}

现在:
1、创建FlowRuleInitFunc 引用InitFunc接口

2、设置Sentinel限流方式

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;


public class FlowRuleInitFunc implements InitFunc {
    @Override
    
    public void init() throws Exception {
        List rules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        //绑定规则名称,针对那个规则设置规则
        flowRule.setResource("resource_Name");
        //QPS或者并发数
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //QPS=5
        flowRule.setCount(5); 
        //添加规则
        rules.add(flowRule);
        FlowRuleManager.loadRules(rules);
    }
}

3、在resources下创建META-INF文件夹

4、在META-INF下创建 services文件夹

5、新建接口文件(SPI)进行扩展
文件名:com.alibaba.csp.sentinel.init.InitFunc

6、写入路径

使用Nacos设置限流规则

1、导入依赖

 
            com.alibaba.csp
            sentinel-datasource-nacos
            1.8.0
        

2、编写方法

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;


public class FlowRuleInitFunc implements InitFunc {

    private final String nacosAddress = "127.0.0.1:8848";
    private final String groupId = "SENTINEL_GROUP";
    private final String dataId ="SpringBoot-sentinel";
    @Override
    
    public void init() throws Exception {
//        List rules = new ArrayList<>();
//        FlowRule flowRule = new FlowRule();
//        //绑定规则名称,针对那个规则设置规则
//        flowRule.setResource("resource_Name");
//        //QPS或者并发数
//        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//        //QPS=5
//        flowRule.setCount(5);
//        //添加规则
//        rules.add(flowRule);
//        FlowRuleManager.loadRules(rules);
        //从远程服务器加载规则(Nacos)
        ReadableDataSource> flowRuleDs =
                new NacosDataSource>(nacosAddress,groupId,dataId,
                        s -> JSON.parseObject(s,new TypeReference>(){}));

        FlowRuleManager.register2Property(flowRuleDs.getProperty());
    }
}

3、在nacos编写规则

使用集成方式利用nacos编写规则

1、在nacos设置规则
2、在配置文件中编辑

#使用nacos编辑规则方便Sentinel熔断降级机制的使用
spring.cloud.sentinel.datasource.dsl.nacos.server-addr=127.0.0.1:8848
spring.cloud.sentinel.datasource.dsl.nacos.data-id=SpringBoot-sentinel
spring.cloud.sentinel.datasource.dsl.nacos.group-id=SENTINEL_GROUP
spring.cloud.sentinel.datasource.dsl.nacos.rule-type=flow
spring.cloud.sentinel.datasource.dsl.nacos.data-type=json
spring.cloud.sentinel.datasource.dsl.nacos.username=nacos
spring.cloud.sentinel.datasource.dsl.nacos.password=nacos

3、流控规则显示成功

使用集群的方式设置限流规则

1、集群示意图

2、建立project sentinel-token-sever
3、添加依赖




  4.0.0

  com.chaoxing.sentinel
  sentinel-token-sever
  1.0-SNAPSHOT

  sentinel-token-sever
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
      com.alibaba.csp
      sentinel-cluster-server-default
      1.8.0
    
    
      com.alibaba.csp
      sentinel-datasource-nacos
      1.8.0
    
    
      com.alibaba.csp
      sentinel-transport-simple-http
      1.8.0
    ***
  

  
    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
      
    
      
          
              org.apache.maven.plugins
              maven-compiler-plugin
              
                  8
                  8
              
          
      
  


4、建立服务类ClusterServer

package com.chaoxing.sentinel;

import com.alibaba.csp.sentinel.cluster.client.ClusterTokenClient;
import com.alibaba.csp.sentinel.cluster.server.ClusterTokenServer;
import com.alibaba.csp.sentinel.cluster.server.SentinelDefaultTokenServer;
import com.alibaba.csp.sentinel.cluster.server.config.ClusterServerConfigManager;
import com.alibaba.csp.sentinel.cluster.server.config.ServerTransportConfig;

import java.util.Collections;


public class ClusterServer {
    public static void main(String[] args) throws Exception {
        ClusterTokenServer tokenServer = new SentinelDefaultTokenServer();
        //手动载入namespace和ClusterServerConfigManager
        //集群限制流服务端通信相关配置
        ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig().setIdleSeconds(600).setPort(9999));
       //加载namespace集合列表
        ClusterServerConfigManager.loadServerNamespaceSet(Collections.singleton("APP-Test"));
        tokenServer.start();
        //Token-client会上报自己的project.name到token-server会根据namespace来统一
    }
}

5、建立获取配置类FlowRuleInitFunc

package com.chaoxing.sentinel;

import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.List;


public class FlowRuleInitFunc implements InitFunc {

    private final String nacosAddress = "127.0.0.1:8848";
    private final String groupId = "SENTINEL_GROUP";
    private final String dataId ="-flow-rules";
    @Override
    
    public void init() throws Exception {
        ClusterFlowRuleManager.setPropertySupplier(namespace->{
            ReadableDataSource> flowRuleDs =
                    new NacosDataSource>(nacosAddress,groupId,namespace+dataId,
                            s -> JSON.parseObject(s,new TypeReference>(){}));
            return flowRuleDs.getProperty();
        });
    }
}

6、添加从nacos获取配置的配置文件

7、添加VM

-Dcsp.sentinel.dashboard.server=127.0.0.1:8080   -Dproject.name=sentinel-token-sever

8、在nacos中配置

9、运行

10.客户端连接
修改FlowRuleInitFunc方法

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientAssignConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfigManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;


public class FlowRuleInitFunc implements InitFunc {

    private final String nacosAddress = "127.0.0.1:8848";
    private final String groupId = "SENTINEL_GROUP";
    private final String dataId ="-flow-rules";
    private  final String clusterServerHost="localhost";
    private final  int clusterServerPort=9999;
    private final int requestTimeOut=20000;
    private final String appname = "App-Test";
    @Override
    
    public void init() throws Exception {
      loadClusterConfig();
      registerFlowRule();
   }
   private  void  loadClusterConfig(){
       ClusterClientAssignConfig assignConfig = new ClusterClientAssignConfig();
       assignConfig.setServerHost(clusterServerHost);//放到配置中心
       assignConfig.setServerPort(clusterServerPort);
       ClusterClientConfigManager.applyNewAssignConfig(assignConfig);
       ClusterClientConfig clientConfig = new ClusterClientConfig();
       clientConfig.setRequestTimeout(requestTimeOut);//放到配置中心
       ClusterClientConfigManager.applyNewConfig(clientConfig);
   }
   private void registerFlowRule(){
        ReadableDataSource> flowRuleDs =
                new NacosDataSource>(nacosAddress,groupId,appname+dataId,
                       s -> JSON.parseObject(s,new TypeReference>(){}));
        FlowRuleManager.register2Property(flowRuleDs.getProperty());
   }
}

11、设置主方法为客户端

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.cluster.ClusterStateManager;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SpringBootSentinelApplication {

    public static void main(String[] args) {
        ClusterStateManager.applyState(ClusterStateManager.CLUSTER_CLIENT);
        SpringApplication.run(SpringBootSentinelApplication.class, args);
    }

}

12 调试vm options

-Dsever.port=8888 -Dproject.name=App-Test -Dcsp.sentinel.dashboard.server=127.0.0.1:8080 

13 确定配置文件可用

14 运行测试



连接成功

集群限流
熔断降级
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995954.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号