博客
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
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
https://blog.csdn.net/m0_45406092/article/details/123338658
上述的限流也可以使用如下方法 ```java @Configuration public class FilterConfig {
@Bean public FilterRegistrationBean sentinelFilterRegistration() {
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
registration.setFilter(new CommonFilter());
registration.addUrlPatterns("/*");
registration.setName("sentinelFilter");
registration.setOrder(1);
return registration;
}
}
```java
@Configuration
@Log4j2
public class SentinelConfiguration {
@PostConstruct
public void init() {
// 统一返回限流或降级响应内容
WebCallbackManager.setUrlBlockHandler((request, response, ex) -> {
if (ex instanceof FlowException) {
log.info("触发限流 > 请求url:{} > 请求参数:{}", request.getRequestURI(), request.getParameterMap());
} else if (ex instanceof DegradeException) {
log.info("触发熔断 > 请求url:{} > 请求参数:{}", request.getRequestURI(), request.getParameterMap());
}
returnData(response, CrowdCode.STATUS_429);
});
}
/**
* 返回数据结果
*
* @param httpResponse
* @param code:提示信息
* @return
*/
private void returnData(HttpServletResponse httpResponse, CrowdCode code) {
try {
ResponseEntity responseEntity = new ResponseEntity();
responseEntity.error(code);
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.setContentType("application/json; charset=utf-8");
httpResponse.setDateHeader("Expires", 0);
PrintWriter out = httpResponse.getWriter();
out.print(JSONObject.toJSONString(responseEntity));
out.flush();
} catch (Exception e) {
log.error("响应内容写入失败,系统异常", e);
System.exit(1);
}
}
}