基于netty实现简易版的tomcat服务器

运行效果图:

servlet url配置信息保存在application.properties中,通过解析得到对应的url与方法的映射,并保存到内存中去,基于netty实现访问:代码如下

public void init() throws FileNotFoundException {
    String basePath = this.getClass().getResource("/").getPath() ;
    FileInputStream ins = new FileInputStream(new File(basePath + "application.properties")) ;
    try {
        prop.load(ins);
        for (Object obj : prop.keySet()){
            String key = trimToEmpty(obj) ;
            String property = prop.getProperty(key);
            if (key.endsWith(".port")){
                if (!(property == "" || property == null)){
                    this.port = Integer.parseInt(property) ;
                }
            }
            if (key.endsWith(".url")) {
                Class clazz = Class.forName(prop.getProperty(key.replaceAll("\\.url$",".className"))) ;
                Constructor constructor = clazz.getDeclaredConstructor();
                if (!constructor.isAccessible()){
                    constructor.setAccessible(true);
                }
                urlMapping.put(property,(EasyServlet) constructor.newInstance()) ;
            }
        }

        if (this.port <= 0){
            this.port = 8080 ;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

处理器类:

public class EasyTomcatHandler extends ChannelInboundHandlerAdapter {

    private Map<String,EasyServlet> urlMapping ;

    public EasyTomcatHandler(Map<String, EasyServlet> mapping){
        this.urlMapping = mapping ;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
       if (msg instanceof HttpRequest){
           HttpRequest req = (HttpRequest) msg;

           EasyRequest request = new EasyRequest(ctx,req) ;
           EasyResponse response = new EasyResponse(ctx,req) ;

           String url = request.getUrl();
           if (urlMapping.containsKey(url)){
               EasyServlet servlet = urlMapping.get(url) ;
               servlet.service(request,response);
           }
       }
    }
}

当接收到请求的时候队请求进行处理

netty服务端代码:

**
 * @Author: drainli
 **/
public class EasyApplication {

    private int port ;

    Map<String, EasyServlet> urlMapping = new HashMap<>() ;
    Properties prop = new Properties() ;

    public static void main(String[] args) throws InterruptedException {
        new EasyApplication().startUp();
    }

    public void startUp() throws InterruptedException {

        try {
            init();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ServerBootstrap bootstrap = new ServerBootstrap() ;

        EventLoopGroup boss = new NioEventLoopGroup() ;
        EventLoopGroup works = new NioEventLoopGroup() ;

        bootstrap.group(boss,works)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ch.pipeline().addLast(new HttpResponseEncoder())
                                        .addLast(new HttpRequestDecoder())
                                        .addLast(new EasyTomcatHandler(urlMapping)) ;
                    }
                })
                .option(ChannelOption.SO_BACKLOG,128)
                .childOption(ChannelOption.SO_KEEPALIVE,true) ;

        ChannelFuture sync = bootstrap.bind(port).sync();

        System.out.printf("server has startup,the listening port is :%d%n",port);

        sync.channel().closeFuture().sync() ;

        boss.shutdownGracefully() ;
        works.shutdownGracefully() ;
    }

    public void init() throws FileNotFoundException {
        String basePath = this.getClass().getResource("/").getPath() ;
        FileInputStream ins = new FileInputStream(new File(basePath + "application.properties")) ;
        try {
            prop.load(ins);
            for (Object obj : prop.keySet()){
                String key = trimToEmpty(obj) ;
                String property = prop.getProperty(key);
                if (key.endsWith(".port")){
                    if (!(property == "" || property == null)){
                        this.port = Integer.parseInt(property) ;
                    }
                }
                if (key.endsWith(".url")) {
                    Class clazz = Class.forName(prop.getProperty(key.replaceAll("\\.url$",".className"))) ;
                    Constructor constructor = clazz.getDeclaredConstructor();
                    if (!constructor.isAccessible()){
                        constructor.setAccessible(true);
                    }
                    urlMapping.put(property,(EasyServlet) constructor.newInstance()) ;
                }
            }

            if (this.port <= 0){
                this.port = 8080 ;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String trimToEmpty(Object obj){
        return obj == null ? "" : String.valueOf(obj).trim() ;
    }
}
``

完整代码地址:  https://gitee.com/drainli/easy-tomcat
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值