Article Directory
1 Brief description of authorization rules
Whitelist users use requests to distinguish sources such as different services, apps or pcs.
2 Case simulation
2.1 Add custom source processing rules under config, need to implement RequsetOriginParser
@Component
public class MyRequestOriginParser implements RequestOriginParser {
/**
* 区分来源:本质通过request域获取来源标识
* 根据不同来源如app/pc,最后返回结果值交给sentinel流控匹配处理
*/
@Override
public String parseOrigin(HttpServletRequest request) {
String servName = request.getParameter("servName");
// 这个不是授权规则必须的判断
if (StringUtils.isEmpty(servName)) {
throw new RuntimeException("servName不能为空");
}
return servName;
}
}
2.2 Console
Before setting rules
访问`http://localhost:8091/sentinel/highConcurrency?servName=pc`,正常返回
访问`http://localhost:8091/sentinel/highConcurrency?servName=app`,正常返回
Set the whitelist value of the resource/sentinel/highConcurrency to pc, and then test
访问`http://localhost:8091/sentinel/highConcurrency?servName=pc`,正常返回
访问`http://localhost:8091/sentinel/highConcurrency?servName=app`,提示限流
ghConcurrency?servName=app`,提示限流