当前位置:   article > 正文

Spring项目集成Sentinel,自定义BlockException异常返回值_sentinel 网关限流 自定义返回

sentinel 网关限流 自定义返回

背景

Sentinel 官网文档:introduction

springmvc项目中集成sentinel,通过sentinel提供的控制台配置限流、熔断等规则,

项目实现UrlBlockHandler,实现在catch到blockexception时进行自定义返回值处理

sentinel的官网文档非常详细的介绍了如何下载部署控制台、以及如何接入各类型的项目框架,本文以mvc项目为例,集成sentinel。

sentinel 控制台jar下载地址:Releases · alibaba/Sentinel · GitHub

启动控制台

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.5.jar

集成

maven配置 

项目maven依赖,sentinel-web-servlet是web项目与sentinel客户端的适配器项目,又引用了sentinel-core核心依赖,web-servlet主要提供一个filter,用来集成sentinel功能

sentinel-transport-simple-http实现与控制台通讯

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-web-servlet</artifactId>
  4. <version>1.8.5</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba.csp</groupId>
  8. <artifactId>sentinel-transport-simple-http</artifactId>
  9. <version>1.8.5</version>
  10. </dependency>

web.xml配置

通过配置web.xml 的filter开启sentinel支持

  1. <filter>
  2. <filter-name>SentinelCommonFilter</filter-name>
  3. <filter-class>com.alibaba.csp.sentinel.adapter.servlet.CommonFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>SentinelCommonFilter</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

 Sentinel配置

SentinelConfiguration配置类主要自定义一个UrlBlockHandler,用来处理异常,要确保这个配置类会被框架扫描到

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.annotation.PostConstruct;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.springframework.context.annotation.Configuration;
  7. import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
  8. import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
  9. import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
  10. import com.alibaba.csp.sentinel.slots.block.BlockException;
  11. import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
  12. import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
  13. import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
  14. import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
  15. import com.alibaba.csp.sentinel.util.StringUtil;
  16. @Configuration
  17. public class SentinelConfiguration {
  18. public UrlBlockHandler urlBlockHandler() {
  19. return new UrlBlockHandler() {
  20. @Override
  21. public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
  22. if (ex instanceof FlowException) {
  23. // 触发接口限流
  24. } else if (ex instanceof DegradeException) {
  25. // 触发熔断降级
  26. } else if (ex instanceof SystemBlockException) {
  27. // 触发系统保护
  28. } else if (ex instanceof AuthorityException) {
  29. // 触发授权规则
  30. }
  31. String errMsg = "Blocked by Sentinel (flow limiting)!" + ex.getRule().getResource();
  32. StringBuffer url = request.getRequestURL();
  33. if ("GET".equals(request.getMethod()) && StringUtil.isNotBlank(request.getQueryString())) {
  34. url.append("?").append(request.getQueryString());
  35. }
  36. if (StringUtil.isBlank(WebServletConfig.getBlockPage())) {
  37. response.setStatus(WebServletConfig.getBlockPageHttpStatus());
  38. PrintWriter out = response.getWriter();
  39. out.print("{\"code\":0,\"message\":\"" + errMsg + "\"}");
  40. out.flush();
  41. out.close();
  42. } else {
  43. String redirectUrl = WebServletConfig.getBlockPage() + "?http_referer=" + url.toString();
  44. // Redirect to the customized block page.
  45. response.sendRedirect(redirectUrl);
  46. }
  47. }
  48. };
  49. }
  50. @PostConstruct
  51. public void init() {
  52. WebCallbackManager.setUrlBlockHandler(urlBlockHandler());
  53. }
  54. }

 启动

项目启动时加入 JVM 参数 -Dcsp.sentinel.dashboard.server=localhost:8080 指定控制台地址和端口

更多启动参数配置参考startup-configuration

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/889273
推荐阅读
相关标签
  

闽ICP备14008679号