SpringCloud 使用sentinel( 二 )


所以在填写流控应用的时候,是根据自定义的来填写(比如在请求上添加值来区分是网关过来的请求还是直接访问的服务的请求)
@Componentpublic class HeaderOriginParser implements RequestOriginParser {@Overridepublic String parseOrigin(HttpServletRequest request) {String origin = request.getHeader("origin");if(StringUtils.isEmpty(origin)){return "blank";}return origin;}}
九、自定义返回方法
只需实现r接口即可
@Componentpublic class SentinelBlockHandler implements BlockExceptionHandler {@Overridepublic void handle(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse, BlockException e) throws Exception {String msg = "未知异常";int status = 429;if (e instanceof FlowException) {msg = "请求被限流了!";} else if (e instanceof DegradeException) {msg = "请求被降级了!"; } else if (e instanceof ParamFlowException) {msg = "热点参数限流!";} else if (e instanceof AuthorityException) {msg = "请求没有权限!";status = 401;}httpServletResponse.setContentType("application/json;charset=utf-8");httpServletResponse.setStatus(status);httpServletResponse.getWriter().println("{\"message\": \"" + msg + "\", \"status\": " + status + "}");}}
十、配置规则持久化三种模式
1、原始模式:保存在内存
2、pull模式:保存在本地文件或数据库,定时去读取
3、push模式:保存在nacos,监听变更实时更新
【SpringCloud 使用sentinel】通常生产环境采用push模式,需要修改-的源码