企业网站东莞网站建设制作,国内销售平台有哪些,百度主机做视频网站怎么样,那些网站是伪静态Spring Cloud(微服务)学习篇(七)
1.使用代码的方式实现流量限制规则
1.1 变更SentinelController类
1.1.1 加入的代码
//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则PostConstructpublic void FlowRule(){Li…Spring Cloud(微服务)学习篇(七)
1.使用代码的方式实现流量限制规则
1.1 变更SentinelController类
1.1.1 加入的代码
//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则PostConstructpublic void FlowRule(){ListFlowRule rules new ArrayListFlowRule();FlowRule rule new FlowRule();rule.setResource(find);//资源名// set limit qps to 10rule.setCount(7);//并发数 1s钟最多执行次数rule.setGrade(RuleConstant.FLOW_GRADE_QPS);rule.setLimitApp(default);rules.add(rule);FlowRuleManager.loadRules(rules);}1.2.1 完整的SentinelController类代码
package com.zlz.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;RestController
public class SentinelController {int count0;SentinelResource(find)//资源名称和下方一致RequestMapping(find)public String find(){count;System.out.println(进入用户查询方法);return 查询用户:count;}//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则PostConstructpublic void FlowRule(){ListFlowRule rules new ArrayListFlowRule();FlowRule rule new FlowRule();rule.setResource(find);//资源名// set limit qps to 10rule.setCount(7);//并发数 1s钟最多执行次数rule.setGrade(RuleConstant.FLOW_GRADE_QPS);rule.setLimitApp(default);rules.add(rule);FlowRuleManager.loadRules(rules);}
}1.2 测试
1.2.1 查看Mysql服务是否打开(只有mysql服务打开,启动nacos窗口才正常) 1.2.2 启动nacos服务 1.2.3 启动Sentinel控制台项目
a 找到sentinel控制台jar包所在的位置➡输入java -jar sentinel-dashboard.jar➡回车 b 回车后的界面 c 通过浏览器登录进入Sentinel后台界面
c.1 在浏览器输入地址localhost:8080后跳转的页面 c.2 输入账户和密码后跳转的页面 1.2.4 启动用户服务 1.2.5 点击刷新Sentinal控制台界面➡点击shop-user➡流控规则 1.2.6 点击编辑 1.2.7 点击编辑按钮后跳转的页面 2 对流量限流给出友好提示
2.1 定义方法的形式
2.1.1 更新SentinelController类
a 加入的代码
// blockHandler就是 限流了应该怎么处理,通常是用于查询的请求,因为这样做本质上是丢弃了这个请求
// 必须要有BlockException e
//需要与原来方法(find方法)的返回值一模一样
//①新增xlHandler方法public String xlHandler(BlockException e){//请求太多放弃掉 查询return 当前访问人数过多 请稍后再试;}
//② 在find方法的SentinelResource注解里面加上blockHandler xlHandlerb 完整的SentinelController类
package com.zlz.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
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.zlz.handler.SentinelHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;RestController
public class SentinelController {int count0;//blockHandler指定的是方法SentinelResource(valuefind,blockHandler xlHandler)//资源名称和下方一致RequestMapping(find)public String find(){count;System.out.println(进入用户查询方法);return 查询用户:count;}//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则PostConstructpublic void FlowRule(){ListFlowRule rules new ArrayListFlowRule();FlowRule rule new FlowRule();rule.setResource(find);//资源名// set limit qps to 10rule.setCount(7);//并发数 1s钟最多执行次数rule.setGrade(RuleConstant.FLOW_GRADE_QPS);rule.setLimitApp(default);rules.add(rule);FlowRuleManager.loadRules(rules);}// blockHandler就是 限流了应该怎么处理
//必须要有BlockException e,若find方法有形参,那么这个方法也得有相应顺序的形参,但是最后一个形参一定是e
//需要与原来方法(find方法)的返回值一模一样public String xlHandler(BlockException e){//原理是请求太多放弃掉查询return 当前访问人数过多 请稍后再试;}
}
2.1.2 测试
a 重新启动用户服务 b jemeter压力测试
b.1 添加线程组 b.2 编辑线程组 b.3 创建HTTP请求 b.4 编辑HTTP请求 b.5 在线程组下面创建结果树 b.6 点击绿色按钮➡点击NO按钮 b.7 点击前7个的HTTP请求的任意一个,都是正常访问 b.8 点击后三个的HTTP请求的任意一个,都是显示当前访问人数过多,请稍后再试 2.2 定义类的方式(处理方法在类中)
2.2.1 在zlz包下创建handler包并创建SentinelHandler类
package com.zlz.handler;import com.alibaba.csp.sentinel.slots.block.BlockException;public class SentinelHandler {//这个方法必须是静态方法public static String xlHandler(BlockException e){//请求太多放弃掉 查询return 当前访问人数过多 请稍后再试;}
}2.2.2 更新SentinelController类
a 加入的代码
//① 在find方法的SentinelResource注解里面加上blockHandler xlHandler blockHandlerClass SentinelHandler.classb 完整的SentinelController类
package com.zlz.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
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.zlz.handler.SentinelHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;RestController
public class SentinelController {int count0;SentinelResource(valuefind,blockHandler xlHandler,blockHandlerClass SentinelHandler.class)//资源名称和下方一致RequestMapping(find)public String find(){count;System.out.println(进入用户查询方法);return 查询用户:count;}//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则PostConstructpublic void FlowRule(){ListFlowRule rules new ArrayListFlowRule();FlowRule rule new FlowRule();rule.setResource(find);//资源名rule.setCount(7);//并发数 1s钟最多执行次数rule.setGrade(RuleConstant.FLOW_GRADE_QPS);rule.setLimitApp(default);rules.add(rule);FlowRuleManager.loadRules(rules);}
}2.2.2 测试
a 重新启动用户服务 b jemeter压力测试
b.1 清除之前的结果 b.2 重新点击绿色按钮➡点击NO按钮 b.3 点击前7个的HTTP请求的任意一个,都没有限流提示 b.4 点击后三个的HTTP请求的任意一个,都有限流提示
文章转载自: http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.qpqcq.cn.gov.cn.qpqcq.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.zdxss.cn.gov.cn.zdxss.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.wwkft.cn.gov.cn.wwkft.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.wcqxj.cn.gov.cn.wcqxj.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.ngdkn.cn.gov.cn.ngdkn.cn http://www.morning.rwxtn.cn.gov.cn.rwxtn.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.srprm.cn.gov.cn.srprm.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.zjrnq.cn.gov.cn.zjrnq.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.nptls.cn.gov.cn.nptls.cn http://www.morning.yrjkz.cn.gov.cn.yrjkz.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn