您的位置:首页 > 编程语言 > Java开发

springMVC在restful风格的性能优化

2016-11-23 20:41 357 查看
目前,restful的接口风格很流行,使用springMVC来搭配restful也是相得益彰。如下,使用@PathVariable注解便可以获取URL上的值。

@RequestMapping(value = "restful/{name}", method = RequestMethod.GET)
public String restful(@PathVariable String name){
return name;
}


不过如果你认真的研究过springMVC就会发现,restful风格的接口的性能会大大低于正常形式的springMVC接口。比如下面这种方式。

@RequestMapping(value = "norestful", method = RequestMethod.GET)
public String norestful(@RequestParam String name){
return name;
}


测试

为了看到效果,我先进行了测试,工具是Apache-Jmeter,

测试参数,并发量50,总量10000次。

1、非restful接口



2、restful接口



对比很明显,非restful接口的性能是restful接口的1.5倍左右,而且restful接口随着@Requestmapping接口数量的增多会越来越慢,而非restful接口不会。

不止如此,非restful接口的最大响应时间是67ms,而restful接口的最大响应时间达到了381ms,这在极端情况下很可能会造成请求超时。

匹配原理

先讲一下springMVC的路径匹配逻辑吧。springMVC的请求主要在DispatcherServlet中处理,而请求分发规则则在doDispatch()方法中完成。

最后处理逻辑在AbstractHandlerMethodMapping类的lookupHandlerMethod方法中进行。

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
List<Match> matches = new ArrayList<Match>();
List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);
if (directPathMatches != null) {
addMatchingMappings(directPathMatches, matches, request);
}
if (matches.isEmpty()) {
// No choice but to go through all mappings...
addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request);
}


这段代码中匹配逻辑有三:

1、List directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);

这个方法是非常直观的根据URL来获取,springMVC会在初始化的时候建立URL和相应RequestMappingInfo的映射。如果不是restful接口,这里就可以直接获取到了。

2、如果1中已经获取到,则调用方法addMatchingMappings(directPathMatches, matches, request)进行匹配校验。

3、如果1中未获取到匹配方法信息,则调用方法addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request);进行全局(all mappings)扫描匹配(this.mappingRegistry.getMappings().keySet())。且会把所有的RequestMappingInfo都遍历完才会停止,也就是说项目中的@RequestMapping方法越多,这个匹配的效率就越低,性能越差。

在遍历过程中,SpringMVC首先会根据@RequestMapping中的headers, params, produces, consumes, methods与实际的HttpServletRequest中的信息对比,剔除掉一些明显不合格的RequestMapping。

如果以上信息都能够匹配上,那么SpringMVC会对RequestMapping中的path进行正则匹配,剔除不合格的。

接下来会对所有留下来的候选@RequestMapping进行评分并排序。最后选择分数最高的那个作为结果。

评分的优先级为:

path pattern > params > headers > consumes > produces > methods

综上所述,当使用非restful接口时就会直接获取对应的HandlerMethod来处理请求,但使用restful接口时,就会每次遍历所有的方法来查找,性能差由此形成。

优化方案

原理:

1、在每个@RequestMapping中添加接口对应服务名的信息。

2、实现自己定义的HandlerMethod查询逻辑,在HandlerMethod注册时记录与之对应的服务名,在查询时通过HTTP请求头中的服务名查表获得HandlerMethod。

实现:

每次请求都执行这段复杂的匹配逻辑是不可取的。我们要做的就是找办法绕开它。spring是一个符合开闭原则的框架。对扩展开放,对修改关闭。它提供了很多扩展性给我们。

springmvc中,AbstractHandlerMethodMapping.MappingRegistry里提供了@Requestmapping中name属性和HandlerMethod的映射如下

private final Map<T, HandlerMethod> mappingLookup = new LinkedHashMap<T, HandlerMethod>();


我们刚好可以使用它。

另外,我们看到实现匹配逻辑的方法HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);其本身是个protected方法,

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception


由此便可以在子类中扩展它。

代码:

我使用基于java config的注解配置.

1、继承WebMvcConfigurationSupport类,复写createRequestMappingHandlerMapping方法返回自定义的RequestMappingHandlerMapping类。

@Override
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
return new RestfulRequestMappingHandlerMapping();
}


2、继承RequestMappingHandlerMapping类

2.1重写lookupHandlerMethod方法,完成自己的查找逻辑。

@Override
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
//自己的查找逻辑,如果找不到,再执行原有的逻辑,以免出现错误情况
HandlerMethod handlerMethod = lookupHandlerMethodHere(lookupPath, request);
if (handlerMethod == null)
handlerMethod = super.lookupHandlerMethod(lookupPath, request);
return handlerMethod;
}

//自己的查找逻辑,根据从请求头中获取服务名servicename,进行匹配查找
private HandlerMethod lookupHandlerMethodHere(String lookupPath, HttpServletRequest request) {
String servicename = request.getHeader("servicename");
if (!StringUtils.isEmpty(servicename)) {
List<HandlerMethod> methodList = this.getHandlerMethodsForMappingName(servicename);
if (methodList.size() > 0){
HandlerMethod handlerMethod = methodList.get(0);
RequestMappingInfo requestMappingInfo = mappingLookup.get(handlerMethod);
handleMatch(requestMappingInfo, lookupPath, request);
return handlerMethod;
}
}
return null;
}


2.2因为RESTful接口存在@PathVariable,我们还需要调用handleMatch方法来将HTTP请求的path解析成参数。然而这个方法需要的参数是RequestMappingInfo,并不是HandlerMethod,SpringMVC也没有提供任何映射。

做法:重写registerHandlerMethod方法,再初始化的时候构建一个从HandlerMethod—>RequestMappingInfo的反向映射。

//映射map
private final Map<HandlerMethod, RequestMappingInfo> mappingLookup = new LinkedHashMap<HandlerMethod, RequestMappingInfo>();

@Override
protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
mappingLookup.put(handlerMethod, mapping);
super.registerHandlerMethod(handler, method, mapping);
}


由此,springMVC优化逻辑编写完成。看代码量很少,但想通过自己写出来,需要对springMVC有相当了解,深入理解springMVC的可扩展点。

最终测试

看下优化过后的restful接口



吞吐量和非restful接口差不多,各项应能都接近,达到预期效果。

参考(感谢达达技术团队分享出来的很多优化方案):

https://tech.imdada.cn/2015/12/23/springmvc-restful-optimize/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  性能优化 spring mvc