Spring Cloud(微服务)学习篇(七)
1.使用代码的方式实现流量限制规则
1.1 变更SentinelController类
1.1.1 加入的代码
@PostConstructpublic void FlowRule(){List<FlowRule> rules = new ArrayList<FlowRule>();FlowRule rule = new FlowRule();rule.setResource("find");rule.setCount(7);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;}@PostConstructpublic void FlowRule(){List<FlowRule> rules = new ArrayList<FlowRule>();FlowRule rule = new FlowRule();rule.setResource("find");rule.setCount(7);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 加入的代码
public String xlHandler(BlockException e){return "当前访问人数过多 请稍后再试";}
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")@RequestMapping("find")public String find(){count++;System.out.println("进入用户查询方法");return "查询用户:"+count;}@PostConstructpublic void FlowRule(){List<FlowRule> rules = new ArrayList<FlowRule>();FlowRule rule = new FlowRule();rule.setResource("find");rule.setCount(7);rule.setGrade(RuleConstant.FLOW_GRADE_QPS);rule.setLimitApp("default");rules.add(rule);FlowRuleManager.loadRules(rules);}
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 加入的代码
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;}@PostConstructpublic void FlowRule(){List<FlowRule> rules = new ArrayList<FlowRule>();FlowRule rule = new FlowRule();rule.setResource("find");rule.setCount(7);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请求的任意一个,都有限流提示
