Spring Cloud(微服务)学习篇(七)
创始人
2024-05-28 20:47:25
0

Spring Cloud(微服务)学习篇(七)

1.使用代码的方式实现流量限制规则

1.1 变更SentinelController类

1.1.1 加入的代码

//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则@PostConstructpublic void FlowRule(){List rules = new ArrayList();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 count=0;@SentinelResource("find")//资源名称和下方一致@RequestMapping("find")public String find(){count++;System.out.println("进入用户查询方法");return "查询用户:"+count;}//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则@PostConstructpublic void FlowRule(){List rules = new ArrayList();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 = "xlHandler"
b 完整的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 count=0;//blockHandler指定的是方法@SentinelResource(value="find",blockHandler = "xlHandler")//资源名称和下方一致@RequestMapping("find")public String find(){count++;System.out.println("进入用户查询方法");return "查询用户:"+count;}//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则@PostConstructpublic void FlowRule(){List rules = new ArrayList();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.class
b 完整的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 count=0;@SentinelResource(value="find",blockHandler = "xlHandler",blockHandlerClass = SentinelHandler.class)//资源名称和下方一致@RequestMapping("find")public String find(){count++;System.out.println("进入用户查询方法");return "查询用户:"+count;}//流控限制 (一个或多个资源限流), postConstruct注解的作用是保证项目一启动就会加载,// 一个rule就是一个规则@PostConstructpublic void FlowRule(){List rules = new ArrayList();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请求的任意一个,都有限流提示

在这里插入图片描述

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...