1. 博客

      1. https://blog.csdn.net/weixin_60590058/article/details/120991935?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-120991935-blog-123338658.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-120991935-blog-123338658.pc_relevant_antiscanv2&utm_relevant_index=5
      1. https://blog.csdn.net/m0_45406092/article/details/120396936?spm=1001.2101.3001.6650.12&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-12-120396936-blog-123338658.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-12-120396936-blog-123338658.pc_relevant_antiscanv2&utm_relevant_index=16
      1. https://blog.csdn.net/m0_45406092/article/details/123338658
    2. 上述的限流也可以使用如下方法 ```java @Configuration public class FilterConfig {

      @Bean public FilterRegistrationBean sentinelFilterRegistration() {

      1. FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
      2. registration.setFilter(new CommonFilter());
      3. registration.addUrlPatterns("/*");
      4. registration.setName("sentinelFilter");
      5. registration.setOrder(1);
      6. return registration;

      }

    }

    1. ```java
    2. @Configuration
    3. @Log4j2
    4. public class SentinelConfiguration {
    5. @PostConstruct
    6. public void init() {
    7. // 统一返回限流或降级响应内容
    8. WebCallbackManager.setUrlBlockHandler((request, response, ex) -> {
    9. if (ex instanceof FlowException) {
    10. log.info("触发限流 > 请求url:{} > 请求参数:{}", request.getRequestURI(), request.getParameterMap());
    11. } else if (ex instanceof DegradeException) {
    12. log.info("触发熔断 > 请求url:{} > 请求参数:{}", request.getRequestURI(), request.getParameterMap());
    13. }
    14. returnData(response, CrowdCode.STATUS_429);
    15. });
    16. }
    17. /**
    18. * 返回数据结果
    19. *
    20. * @param httpResponse
    21. * @param code:提示信息
    22. * @return
    23. */
    24. private void returnData(HttpServletResponse httpResponse, CrowdCode code) {
    25. try {
    26. ResponseEntity responseEntity = new ResponseEntity();
    27. responseEntity.error(code);
    28. httpResponse.setCharacterEncoding("UTF-8");
    29. httpResponse.setContentType("application/json; charset=utf-8");
    30. httpResponse.setDateHeader("Expires", 0);
    31. PrintWriter out = httpResponse.getWriter();
    32. out.print(JSONObject.toJSONString(responseEntity));
    33. out.flush();
    34. } catch (Exception e) {
    35. log.error("响应内容写入失败,系统异常", e);
    36. System.exit(1);
    37. }
    38. }
    39. }