netty实现tomcat(简易版)

本篇主要是介绍如何用Netty来实现Tomcat简易功能

一、Netty是什么?

Netty 是由 JBOSS 提供的一个 Java 开源框架。Netty 提供异步的、基于事件驱动的网络应用程序框架,用以快速开发高性能、高可靠性的网络 IO 程序,是目前最流行的 NIO 框架,Netty 在互联网领域、大数据分布式计算领域、游戏行业、通信行业等获得了广泛的应用,知名的 Elasticsearch 、Dubbo 框架内部都采用了 Netty。

Netty不仅支持TCP、UDP协议,同时也有支持http协议类型的消息包,这里不专门介绍Netty就不多作描述。

二、Tomcat核心结构图

三、pom配置

可以只用JDK及依赖netty包就可实现,本文用到fastjson只为将数据类型转换为json格式更加可观。

<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.56.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.75</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>
四、上代码

话不多说,码上安排,步骤有相应注释,还没有用过netty的同学们请移步netty文档简单了解一番。

1.启动类:netty运行核心实现,置入解析http消息包

public class MainServer {
    private static final Logger log = LoggerFactory.getLogger(MainServer.class);
    /** 默认端口 */
    private int port = 9999;
 
    public static Map<String, Map<Method, Class<?>>> servlet = new HashMap<>();
 
    static {
        // 此处目的是装载http接口的处理类,使用反射实现装载Controller注解的类
        // TODO  pack包路径必须拷贝自己项目controller那层的(必须是包路径)
        servlet = new AnnotationScanner().getRequestMapping("com.star.system.netty.controller");
    }
 
    /**
     * 默认端口9999启动
     */
    public void start() {
        doStart();
    }
 
    /**
     * 自定义端口启动
     * @param port
     */
    public void start(int port) {
        this.port = port;
        doStart();
    }
 
    /**
     * netty核心简单实现
     */
    private void doStart() {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        ServerBootstrap server = new ServerBootstrap();
        try {
            server.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    //客户端连接时启动
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel client) throws Exception {
                            // HTTP应答编码器
                            client.pipeline().addLast(new HttpResponseEncoder());
                            // HTTP请求解码器
                            client.pipeline().addLast(new HttpRequestDecoder());
                            // Tomcat之Servlet处理类
                            client.pipeline().addLast(new ServletHandler());
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            ChannelFuture f = server.bind(port).sync();
            //监听关闭状态启动
            log.info("Netty Server Started, Port:" + port);
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //关闭线程池
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
 
    /**
     * Netty启动总入口
     * @param args
     */
    public static void main(String[] args) {
        new MainServer().start();
    }
}
2.Tomcat实现:Http消息包解析后并从servlet中找到已映射的接口方法

public class ServletHandler extends ChannelInboundHandlerAdapter {
 
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 判断消息包是否属于http类型
        if(msg instanceof HttpRequest){
            // 属于既强转为HttpRequest,该类是netty包中自带的。
            HttpRequest req= (HttpRequest) msg;
            // 自定义request、response的处理类
            HttpRequestServlet request=new HttpRequestServlet(ctx,req);
            HttpResponseServlet response=new HttpResponseServlet(ctx,req);
            String url=request.getUri();
            // 查找已装配的servlet集合中是否含有该请求
            if(MainServer.servlet.containsKey(url)){
                // 下方将有多处反射查找请求对应接口的方法及请求参数
                Map<Method, Class<?>> handler = MainServer.servlet.get(url);
                Map<String, List<String>> requestParam = request.getParameters();
                for(Map.Entry<Method, Class<?>> entry : handler.entrySet()) {
                    Method method = entry.getKey();
                    Class<?>[] paramType = method.getParameterTypes();
                    Object clazz = entry.getValue().newInstance();
                    Object paramObj = paramType[0].newInstance();
                    Field[] fields = paramObj.getClass().getDeclaredFields();
                    // 判断请求参数是否在接口方法中入参存在,如存在且转换数据类型。
                    for(String key : requestParam.keySet()) {
                        for(Field field : fields) {
                            if(key.equals(field.getName())) {
                                field.setAccessible(true);
                                Class<?> type = field.getType();
                                if(type == String.class) {
                                    field.set(paramObj, requestParam.get(key).get(0));
                                    continue;
                                }
                                if(type == int.class || type == Integer.class) {
                                    field.set(paramObj, Integer.valueOf(requestParam.get(key).get(0)));
                                    continue;
                                }
                                if(type == long.class || type == Long.class) {
                                    field.set(paramObj, Long.valueOf(requestParam.get(key).get(0)));
                                    continue;
                                }
                                if(type == byte.class || type == Byte.class) {
                                    field.set(paramObj, Byte.valueOf(requestParam.get(key).get(0)));
                                    continue;
                                }
                                if(type == boolean.class || type == Boolean.class) {
                                    field.set(paramObj, Boolean.valueOf(requestParam.get(key).get(0)));
                                    continue;
                                }
                            }
                        }
                    }
                    // 调用接口方法并传递请求参数。
                    Object retData = method.invoke(clazz, paramObj);
                    response.write(retData);
                }
            }else{
                response.write("404");
            }
        }
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
3.HttpRequestServlet、HttpResponseServlet、AnnotationScanner






下面代码就不多写注释说明了

public class HttpRequestServlet {
    private ChannelHandlerContext ctx;
    private HttpRequest request;
    public HttpRequestServlet(ChannelHandlerContext ctx, HttpRequest request){
        this.ctx=ctx;
        this.request=request;
    }
    public String getUri(){
        String uri = request.uri();
        String[] s = uri.split("\\?");
        if(s.length < 2) {
            return uri;
        }
        return s[0];
    }
    public String getMethod(){
        return request.method().name();
    }
 
    public Map<String, List<String>> getParameters(){
        QueryStringDecoder decoder=new QueryStringDecoder(request.uri());
        return decoder.parameters();
    }
    public String getParameter(String name){
        Map<String,List<String>> params=getParameters();
        List<String> param=params.get(name);
        if(param==null)return null;
        else return param.get(0);
    }
}

public class HttpResponseServlet {
    private ChannelHandlerContext ctx;
    private HttpRequest request;
    private String code = "UTF-8";
 
    public HttpResponseServlet(ChannelHandlerContext ctx, HttpRequest request) {
        this.ctx = ctx;
        this.request = request;
    }
 
    public void write(Object out) throws Exception {
        try {
            //设置HTTP及请求头信息
            FullHttpResponse response = null;
            response = new DefaultFullHttpResponse(
                    //设置版本
                    HttpVersion.HTTP_1_1,
                    //设置响应状态码
                    HttpResponseStatus.OK,
                    //设置输出格式
                    Unpooled.wrappedBuffer(out == null ? "".getBytes(code) : JSON.toJSONBytes(out)));
            response.headers().set("Content-Type", "text/html;");
            ctx.write(response);
        } finally {
            ctx.flush();
            ctx.close();
        }
 
 
    }
}
public class AnnotationScanner {
    private static final Logger log = LoggerFactory.getLogger(AnnotationScanner.class);
    private Set<Class<?>> controllers;
 
    public Map<String, Map<Method, Class<?>>> getRequestMapping(String pack) {
        Map<String, Map<Method, Class<?>>> handler = new HashMap<>();
        for (Class<?> cls : getControllers(pack)) {
            Method[] methods = cls.getMethods();
            for (Method method : methods) {
                RequestMapping annotation = method.getAnnotation(RequestMapping.class);
                if (annotation != null) {
                    String urlValue = annotation.value();
                    if (!urlValue.startsWith("/")) {
                        urlValue = "/" + urlValue;
                    }
                    log.info("loaded servlet:{}", urlValue);
                    Map<Method, Class<?>> invoke = new HashMap<>();
                    invoke.put(method, cls);
                    handler.put(urlValue, invoke);
                }
            }
        }
        return handler;
    }
 
    public Set<Class<?>> getControllers(String pack) {
        if(controllers == null) {
            controllers = new HashSet<>();
            Set<Class<?>> clszzList = getClasses(pack);
            if (clszzList != null && clszzList.size() > 0) {
                for (Class<?> cls : clszzList) {
                    if (cls.getAnnotation(Controller.class) != null) {
                        controllers.add(cls);
                    }
                }
            }
        }
        return controllers;
    }
 
    private Set<Class<?>> getClasses(String pack) {
        Set<Class<?>> classes = new HashSet<>();
        boolean recursive = true;
        String packDirName = pack.replace(".", "/");
        Enumeration<URL> dirs;
        try {
            dirs = Thread.currentThread().getContextClassLoader().getResources(packDirName);
            // 循环迭代下去
            while (dirs.hasMoreElements()) {
                // 获取下一个元素
                URL url = dirs.nextElement();
                // 得到协议的名称
                String protocol = url.getProtocol();
                // 如果是以文件的形式保存在服务器上
                if ("file".equals(protocol)) {
                    // 获取包的物理路径
                    String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
                    // 以文件的方式扫描整个包下的文件 并添加到集合中,以下俩种方法都可以
                    //网上的第一种方法,
                    findAndAddClassesInPackageByFile(pack, filePath, recursive, classes);
                    //网上的第二种方法
                    //addClass(classes,filePath,packageName);
                } else if ("jar".equals(protocol)) {
                    // 如果是jar包文件
                    // 定义一个JarFile
                    JarFile jar;
                    try {
                        // 获取jar
                        jar = ((JarURLConnection) url.openConnection()).getJarFile();
                        // 从此jar包 得到一个枚举类
                        Enumeration<JarEntry> entries = jar.entries();
                        // 同样的进行循环迭代
                        while (entries.hasMoreElements()) {
                            // 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
                            JarEntry entry = entries.nextElement();
                            String name = entry.getName();
                            // 如果是以/开头的
                            if (name.charAt(0) == '/') {
                                // 获取后面的字符串
                                name = name.substring(1);
                            }
                            // 如果前半部分和定义的包名相同
                            if (name.startsWith(packDirName)) {
                                int idx = name.lastIndexOf('/');
                                // 如果以"/"结尾 是一个包
                                if (idx != -1) {
                                    // 获取包名 把"/"替换成"."
                                    pack = name.substring(0, idx).replace('/', '.');
                                }
                                // 如果可以迭代下去 并且是一个包
                                if ((idx != -1) || recursive) {
                                    // 如果是一个.class文件 而且不是目录
                                    if (name.endsWith(".class") && !entry.isDirectory()) {
                                        // 去掉后面的".class" 获取真正的类名
                                        String className = name.substring(pack.length() + 1, name.length() - 6);
                                        try {
                                            // 添加到classes
                                            classes.add(Class.forName(pack + '.' + className));
                                        } catch (ClassNotFoundException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return classes;
    }
 
    /**
     * 以文件的形式来获取包下的所有Class
     *
     * @param packageName
     * @param packagePath
     * @param recursive
     * @param classes
     */
    public static void findAndAddClassesInPackageByFile(String packageName,
                                                        String packagePath, final boolean recursive, Set<Class<?>> classes) {
        // 获取此包的目录 建立一个File
        File dir = new File(packagePath);
        // 如果不存在或者 也不是目录就直接返回
        if (!dir.exists() || !dir.isDirectory()) {
            // log.warn("用户定义包名 " + packageName + " 下没有任何文件");
            return;
        }
        // 如果存在 就获取包下的所有文件 包括目录
        File[] dirfiles = dir.listFiles(new FileFilter() {
            // 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
            @Override
            public boolean accept(File file) {
                return (recursive && file.isDirectory())
                        || (file.getName().endsWith(".class"));
            }
        });
        // 循环所有文件
        for (File file : dirfiles) {
            // 如果是目录 则继续扫描
            if (file.isDirectory()) {
                findAndAddClassesInPackageByFile(packageName + "."
                                + file.getName(), file.getAbsolutePath(), recursive,
                        classes);
            } else {
                // 如果是java类文件 去掉后面的.class 只留下类名
                String className = file.getName().substring(0,
                        file.getName().length() - 6);
                try {
                    // 添加到集合中去
                    //classes.add(Class.forName(packageName + '.' + className));
                    classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));
                } catch (ClassNotFoundException e) {
                    // log.error("添加用户自定义视图类错误 找不到此类的.class文件");
                    e.printStackTrace();
                }
            }
        }
    }
 
}
4.HiController(简易@Controller,@RequestMapping)、User(多个数

据类型,自行测试吧)
@Controller
public class HiController {
 
    @RequestMapping(value = "hi")
    public User hi(User user) {
        return user;
    }
 
    @RequestMapping(value = "say")
    public User say(User user) {
        return user;
    }
 
}
@Data
public class User implements Serializable {
    private Long id;
    private String name;
    private int age;
    private boolean status;
 
    public User() {
    }
 
    public User(Long id, String name, int age, boolean status) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.status = status;
    }
}
5.核心注解Controller、RequestMapping

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface Controller {
}
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
 
    String value() default "";
 
}
五、未完待续

ps:首先说明此简易版还有无限扩展空间,也没有进行代码的优化,仅提供一个Tomcat的实现思路。

作者:java知路


欢迎关注微信公众号 :java知路