上周(2020.02.12)我们写了一篇关于《Spring Cloud Alibaba V2.2.0 发布》 相关的特性,v2.2.0
是基于 Spring Cloud Hoxton
的版本。下面总结一下由 Spring Cloud Alibaba v2.1.0
升级至 v2.2.0
遇到的问题。
主要问题涉及 sentinel
的问题。
Spring Cloud Alibaba Sentinel
不再依赖 sentinel-web-servlet
实现,而是依赖 sentinel-spring-webmvc-dapter
, 基于 Spring 的 Interceptor
拦截资源,而不再是 CommonFilter
。
实现接口发现变化
// 之前版本 public class PigxUrlBlockHandler implements UrlBlockHandler { @Override public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException { // 降级业务处理 } } // v2.2.0 public class PigxUrlBlockHandler implements BlockExceptionHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception { // 降级业务处理 } }复制代码
实现接口的包发生变化
//旧版本 import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser; // v2.2.0 import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser; // 请求解析器实现 public class PigxHeaderRequestOriginParser implements RequestOriginParser { private static final String ALLOW = "Allow"; @Override public String parseOrigin(HttpServletRequest request) { return request.getHeader(ALLOW); } }复制代码
/*
请求上文提到V2.2.0
是基于 Interceptor
实现,默认拦截规则为 /*
, 也就是只有 请求URI 为 / ,/a 的请求可以被sentinel处理,dashboard
也只会显示被拦截的资源,需要配置一下拦截规则。 此问题我们已提交 pull request
修复Modify the sentinelInterceptor default interception rule and backward
spring: cloud: sentinel: filter: url-patterns: /**复制代码
涉及源码: SentinelWebAutoConfiguration.addInterceptors()
spring boot admin
显示部分服务为 down
,控台只显示部分服务访问服务 IP:PORT/actuator/health
显示sentinel 状态为 down
,默认情况下 sentinel 是懒加载的,在启动时不会立即给 sentinel-server
发送心跳,导致SentinelHealthIndicator
将状态判为 false
,进而导致 Spring Boot Admin
读取到 actuator
信息为 down
可以设置饥饿加载,启动加载时就会加载,并发送心跳
spring: cloud: sentinel: eager: true复制代码
源码可以参考: SentinelAutoConfiguration.init()
, 利用SPI
加载对应的 业务
Spring Cloud Alibaba Sentinel
模块源码,处理逻辑相对简单明了建议通读。核心是初始化 sentinel
的dapter
,并且注入了 默认的降级等实现。
『★★★★★』 基于Spring Boot 2.2、 Spring Cloud Hoxton & Alibaba、 OAuth2 的RBAC 权限管理系统