Netty URL路由方案探讨
最近在用Netty做开发,需要提供一个http web server,供调用方调用。采用Netty本身提供的HttpServerCodec
handler进行Http协议的解析,但是需要自己提供路由。
最开始是通过对Http method及uri 采用多层if else 嵌套判断的方法路由到真正的controller类:
String uri = request.uri();
HttpMethod method = request.method();
if (method == HttpMethod.POST) {
if (uri.startsWith("/login")) {
//url参数解析,调用controller的方法
} else if (uri.startsWith("/logout")) {
//同上
}
} else if (method == HttpMethod.GET) {
if (uri.startsWith("/")) {
} else if (uri.startsWith("/status")) {
}
}
在只需提供login
及logout
API时,代码可以完成功能,可是随着API的数量越来越多,需要支持的方法及uri越来越多,else if
越来越多,代码越来越复杂。
在阿里开发手册中也提到过:
因此首先考虑采用状态设计模式及策略设计模式重构。
状态模式
状态模式的角色:
- state状态
表示状态,定义了根据不同状态进行不同处理的接口,该接口是那些处理内容依赖于状态的方法集合,对应实例的state类 - 具体的状态
实现了state接口,对应daystate和nightstate - context
context持有当前状态的具体状态的实例,此外,他还定义了供外部调用者使用的状态模式的接口。
首先我们知道每个http请求都是由method及uri来唯一标识的,所谓路由就是通过这个唯一标识定位到controller类的中的某个方法。
因此把HttpLabel作为状态
@Data
@AllArgsConstructor
public class HttpLabel {
private String uri;
private HttpMethod method;
}
状态接口:
public interface Route {
/**
* 路由
*
* @param request
* @return
*/
GeneralResponse call(FullHttpRequest request);
}
为每个状态添加状态实现:
public void route() {
//单例controller类
final DemoController demoController = DemoController.getInstance();
Map<HttpLabel, Route> map = new HashMap<>();
map.put(new HttpLabel("/login", HttpMethod.POST), demoController::login);
map.put(new HttpLabel("/logout", HttpMethod.POST), demoController::login);
}
接到请求,判断状态,调用不同接口:
public class ServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
String uri = request.uri();
GeneralResponse generalResponse;
if (uri.contains("?")) {
uri = uri.substring(0, uri.indexOf("?"));
}
Route route = map.get(new HttpLabel(uri, request.method()));
if (route != null) {
ResponseUtil.response(ctx, request, route.call(request));
} else {
generalResponse = new GeneralResponse(HttpResponseStatus.BAD_REQUEST, "请检查你的请求方法及url", null);
ResponseUtil.response(ctx, request, generalResponse);
}
}
}
使用状态设计模式重构代码,在增加url时只需要网map里面put一个值就行了。
Netty实现类似SpringMVC路由
后来看了 JAVA反射+运行时注解实现URL路由 发现反射+注解的方式很优雅,代码也不复杂。
下面介绍Netty使用反射实现URL路由。
路由注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
/**
* 路由的uri
*
* @return
*/
String uri();
/**
* 路由的方法
*
* @return
*/
String method();
}
json格式的body
:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestBody {
}
异常类(用于全局异常处理,实现 @ControllerAdvice
异常处理):
@Data
public class MyRuntimeException extends RuntimeException {
private GeneralResponse generalResponse;
public MyRuntimeException(String message) {
generalResponse = new GeneralResponse(HttpResponseStatus.INTERNAL_SERVER_ERROR, message);
}
public MyRuntimeException(HttpResponseStatus status, String message) {
generalResponse = new GeneralResponse(status, message);
}
public MyRuntimeException(GeneralResponse generalResponse) {
this.generalResponse = generalResponse;
}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
扫描classpath下带有@RequestMapping
注解的方法,将这个方法放进一个路由Map:Map<HttpLabel, Action<GeneralResponse>> httpRouterAction
,key为上面提到过的Http唯一标识 HttpLabel
,value为通过反射调用的方法:
@Slf4j
public class HttpRouter extends ClassLoader {
private Map<HttpLabel, Action<GeneralResponse>> httpRouterAction = new HashMap<>();
private String classpath = this.getClass().getResource("").getPath();
private Map<String, Object> controllerBeans = new HashMap<>();
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String path = classpath + name.replaceAll("\\.", "/");
byte[] bytes;
try (InputStream ins = new FileInputStream(path)) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024 * 5];
int b = 0;
while ((b = ins.read(buffer)) != -1) {
out.write(buffer, 0, b);
}
bytes = out.toByteArray();
}
} catch (Exception e) {
throw new ClassNotFoundException();
}
return defineClass(name, bytes, 0, bytes.length);
}
public void addRouter(String controllerClass) {
try {
Class<?> cls = loadClass(controllerClass);
Method[] methods = cls.getDeclaredMethods();
for (Method invokeMethod : methods) {
Annotation[] annotations = invokeMethod.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == RequestMapping.class) {
RequestMapping requestMapping = (RequestMapping) annotation;
String uri = requestMapping.uri();
String httpMethod = requestMapping.method().toUpperCase();
// 保存Bean单例
if (!controllerBeans.containsKey(cls.getName())) {
controllerBeans.put(cls.getName(), cls.newInstance());
}
Action action = new Action(controllerBeans.get(cls.getName()), invokeMethod);
//如果需要FullHttpRequest,就注入FullHttpRequest对象
Class[] params = invokeMethod.getParameterTypes();
if (params.length == 1 && params[0] == FullHttpRequest.class) {
action.setInjectionFullhttprequest(true);
}
// 保存映射关系
httpRouterAction.put(new HttpLabel(uri, new HttpMethod(httpMethod)), action);
}
}
}
} catch (Exception e) {
log.warn("{}", e);
}
}
public Action getRoute(HttpLabel httpLabel) {
return httpRouterAction.get(httpLabel);
}
}
通过反射调用controller
类中的方法:
@Data
@RequiredArgsConstructor
@Slf4j
public class Action {
@NonNull
private Object object;
@NonNull
private Method method;
private List<Class> paramsClassList;
public GeneralResponse call(Object... args) {
try {
return (GeneralResponse) method.invoke(object, args);
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
//实现 `@ControllerAdvice` 异常处理,直接抛出自定义异常
if (targetException instanceof MyRuntimeException) {
return ((MyRuntimeException) targetException).getGeneralResponse();
}
log.warn("method invoke error: {}", e);
return new GeneralResponse(HttpResponseStatus.INTERNAL_SERVER_ERROR, String.format("Internal Error: %s", ExceptionUtils.getRootCause(e)), null);
} catch (IllegalAccessException e) {
log.warn("method invoke error: {}", e);
return new GeneralResponse(HttpResponseStatus.INTERNAL_SERVER_ERROR, String.format("Internal Error: %s", ExceptionUtils.getRootCause(e)), null);
}
}
}
ServerHandler.java
处理如下:
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
String uri = request.uri();
GeneralResponse generalResponse;
if (uri.contains(DELIMITER)) {
uri = uri.substring(0, uri.indexOf(DELIMITER));
}
//根据不同的请求API做不同的处理(路由分发)
Action action = httpRouter.getRoute(new HttpLabel(uri, request.method()));
if (action != null) {
String s = request.uri();
if (request.headers().get(HttpHeaderNames.CONTENT_TYPE.toString()).equals(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
s = s + "&" + request.content().toString(StandardCharsets.UTF_8);
}
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(s);
Map<String, List<String>> parameters = queryStringDecoder.parameters();
Class[] classes = action.getMethod().getParameterTypes();
Object[] objects = new Object[classes.length];
for (int i = 0; i < classes.length; i++) {
Class c = classes[i];
//处理@RequestBody注解
Annotation[] parameterAnnotation = action.getMethod().getParameterAnnotations()[i];
if (parameterAnnotation.length > 0) {
for (int j = 0; j < parameterAnnotation.length; j++) {
if (parameterAnnotation[j].annotationType() == RequestBody.class &&
request.headers().get(HttpHeaderNames.CONTENT_TYPE.toString()).equals(HttpHeaderValues.APPLICATION_JSON.toString())) {
objects[i] = JsonUtil.fromJson(request, c);
}
}
//处理数组类型
} else if (c.isArray()) {
String paramName = action.getMethod().getParameters()[i].getName();
List<String> paramList = parameters.get(paramName);
if (CollectionUtils.isNotEmpty(paramList)) {
objects[i] = ParamParser.INSTANCE.parseArray(c.getComponentType(), paramList);
}
} else {
//处理基本类型和string
String paramName = action.getMethod().getParameters()[i].getName();
List<String> paramList = parameters.get(paramName);
if (CollectionUtils.isNotEmpty(paramList)) {
objects[i] = ParamParser.INSTANCE.parseValue(c, paramList.get(0));
} else {
objects[i] = ParamParser.INSTANCE.parseValue(c, null);
}
}
}
ResponseUtil.response(ctx, HttpUtil.isKeepAlive(request), action.call(objects));
} else {
//错误处理
generalResponse = new GeneralResponse(HttpResponseStatus.BAD_REQUEST, "请检查你的请求方法及url", null);
ResponseUtil.response(ctx, HttpUtil.isKeepAlive(request), generalResponse);
}
}
DemoController
方法配置:
@RequestMapping(uri = "/login", method = "POST")
public GeneralResponse login(@RequestBody User user, FullHttpRequest request,
String test, Integer test1, int test2,
long[] test3, Long test4, String[] test5, int[] test6) {
System.out.println(test2);
log.info("/login called,user: {} ,{} ,{} {} {} {} {} {} {} {} ", user, test, test1, test2, test3, test4, test5, test6);
return new GeneralResponse(null);
}
测试结果如下:
netty-route
得到结果如下:
user=User(username=hah, password=dd),test=111,test1=null,test2=0,test3=[1],test4=null,test5=[d,a, 1],test6=[1, 2]
完整代码在 https://github.com/morethink/Netty-Route
Netty URL路由方案探讨的更多相关文章
- 一种dubbo逻辑路由方案
背景介绍 现在很多的公司都在用dubbo.springcloud做为服务化/微服务的开发框架,服务化之后应用越来越多,链路越来越长,服务环境的治理变的很困难.比如:研发团队的人很多的,同时有几个分支在 ...
- 一种dubbo逻辑路由方案(服务化隔离环境)
背景介绍 现在很多的公司都在用dubbo.springcloud做为服务化/微服务的开发框架,服务化之后应用越来越多,链路越来越长,服务环境的治理变的很困难.比如:研发团队的人很多的,同时有几个分支在 ...
- ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案
ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不大,但从内部运行方式上来说,差别还是很大的.上一篇详细介绍了原版路由方案的运行机制, ...
- vue单页应用前进刷新后退不刷新方案探讨
引言 前端webapp应用为了追求类似于native模式的细致体验,总是在不断的在向native的体验靠拢:比如本文即将要说到的功能,native由于是多页应用,新页面可以启用一个的新的webview ...
- python django基础二URL路由系统
URL配置 基本格式 from django.conf.urls import url #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数执行,就不再往下循环了,并给函数 ...
- ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据
ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...
- 【转】Asp.Net MVC4 之Url路由
MVC4常见路由的处理方式 //直接方法重载+匿名对象 routes.MapRoute( name: "Default", url: "{controller}/{act ...
- Asp.Net MVC4 之Url路由
先来看下面两个个url,对比一下: http://xxx.yyy.com/Admin/UserManager.aspx http://xxx.yyy.com/Admin/DeleteUser/1001 ...
- ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案
原文:ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不大 ...
随机推荐
- 用css 实现凹陷的线条
box-shadow: 0 1px 0 rgba(255,255,255,0.2) inset,0 -1px 0 rgba(0,0,0,.2) inset; 因为颜色为透明颜色,所以颜色是什么样的,不 ...
- 减小Delphi 2010/delphi XE编译出来的文件大小
1.禁用RTTI 禁用的方法很简单,就是要在工程(dpr文件中.Delphi2010下项目文件是dproj文件,但dpr文件仍然是默认的编写代码的项目文件)的Uses语句前添加下面的定义就可以了: { ...
- pygame学习笔记(2)——从画点到动画
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.单个像素(画点)利用pygame画点主要有三种方法:方法一:画长宽为1个像素的正方形 #@小五义 http:/ ...
- C++模板常用功能讲解
前言 泛型编程是C++继面向对象编程之后的又一个重点,是为了编写与具体类型无关的代码.而模板是泛型编程的基础.模板简单来理解,可以看作是用宏来实现的,事实上确实有人用宏来实现了模板类似的功能.模板,也 ...
- Mxnet Windows配置
MXNET Windows 编译安装(Python) 本文只记录Mxnet在windows下的编译安装,更多环境配置请移步官方文档:http://mxnet.readthedocs.io/en/lat ...
- 回车”(carriage return)和”换行”(line feed)的区别和来历-(附:ASCII表)
这两天研究小票打印机编程手册,遇到这样一个问题: LF,即Line Feed,中文意思“换行”:CR,即Carriage Return,中文意思“回车”.但是我们通常把这两个混为一谈.既然设置 ...
- GPU并行编程小结
http://peghoty.blog.163.com/blog/static/493464092013016113254852/ http://blog.csdn.net/augusdi/artic ...
- 【BZOJ4559】成绩比较(动态规划,拉格朗日插值)
[BZOJ4559]成绩比较(动态规划,拉格朗日插值) 题面 BZOJ 洛谷 题解 显然可以每门课顺次考虑, 设\(f[i][j]\)表示前\(i\)门课程\(zsy\)恰好碾压了\(j\)个\(yy ...
- Ants on tree
Description 从前有一个策略游戏, 叫做 蚂蚁上树 游戏中有一棵 nn 个节点, 以 11 为根的有根树 初始始每个节点都为空, 游戏系统会进行两种操作 : 1 x , 表示往 xx 节点放 ...
- digitalworld.local: MERCY靶机入侵
0x01 前言 MERCY是一个致力于PWK课程安全的靶机系统.MERCY是一款游戏名称,与易受攻击的靶机名称无关.本次实验是攻击目标靶机获取root权限并读系统目录中的proof.txt信息 靶机的 ...