GPTomcat.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package cn.hhj;
  2. import cn.hhj.http.GPRequest;
  3. import cn.hhj.http.GPResponse;
  4. import cn.hhj.http.GPServlet;
  5. import io.netty.bootstrap.ServerBootstrap;
  6. import io.netty.channel.*;
  7. import io.netty.channel.nio.NioEventLoopGroup;
  8. import io.netty.channel.socket.SocketChannel;
  9. import io.netty.channel.socket.nio.NioServerSocketChannel;
  10. import io.netty.handler.codec.http.HttpRequest;
  11. import io.netty.handler.codec.http.HttpRequestDecoder;
  12. import io.netty.handler.codec.http.HttpResponseEncoder;
  13. import java.io.FileInputStream;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.Properties;
  17. //Netty就是一个同时支持多协议的网络通信框架
  18. public class GPTomcat {
  19. //打开Tomcat源码,全局搜索ServerSocket
  20. private int port = 8080;
  21. private Map<String, GPServlet> servletMapping = new HashMap<String,GPServlet>();
  22. private Properties webxml = new Properties();
  23. private void init(){
  24. //加载web.xml文件,同时初始化 ServletMapping对象
  25. try{
  26. String WEB_INF = this.getClass().getResource("/").getPath();
  27. FileInputStream fis = new FileInputStream(WEB_INF + "web.properties");
  28. webxml.load(fis);
  29. for (Object k : webxml.keySet()) {
  30. String key = k.toString();
  31. if(key.endsWith(".url")){
  32. String servletName = key.replaceAll("\\.url$", "");
  33. String url = webxml.getProperty(key);
  34. String className = webxml.getProperty(servletName + ".className");
  35. GPServlet obj = (GPServlet)Class.forName(className).newInstance();
  36. servletMapping.put(url, obj);
  37. }
  38. }
  39. }catch(Exception e){
  40. e.printStackTrace();
  41. }
  42. }
  43. public void start(){
  44. init();
  45. //Netty封装了NIO,Reactor模型,Boss,worker
  46. // Boss线程
  47. EventLoopGroup bossGroup = new NioEventLoopGroup();
  48. // Worker线程
  49. EventLoopGroup workerGroup = new NioEventLoopGroup();
  50. try {
  51. // Netty服务
  52. //ServetBootstrap ServerSocketChannel
  53. ServerBootstrap server = new ServerBootstrap();
  54. // 链路式编程
  55. server.group(bossGroup, workerGroup)
  56. // 主线程处理类,看到这样的写法,底层就是用反射
  57. .channel(NioServerSocketChannel.class)
  58. // 子线程处理类 , Handler
  59. .childHandler(new ChannelInitializer<SocketChannel>() {
  60. // 客户端初始化处理
  61. protected void initChannel(SocketChannel client) throws Exception {
  62. // 无锁化串行编程
  63. //Netty对HTTP协议的封装,顺序有要求
  64. // HttpResponseEncoder 编码器
  65. client.pipeline().addLast(new HttpResponseEncoder());
  66. // HttpRequestDecoder 解码器
  67. client.pipeline().addLast(new HttpRequestDecoder());
  68. // 业务逻辑处理
  69. client.pipeline().addLast(new GPTomcatHandler());
  70. }
  71. })
  72. // 针对主线程的配置 分配线程最大数量 128
  73. .option(ChannelOption.SO_BACKLOG, 128)
  74. // 针对子线程的配置 保持长连接
  75. .childOption(ChannelOption.SO_KEEPALIVE, true);
  76. // 启动服务器
  77. ChannelFuture f = server.bind(port).sync();
  78. System.out.println("GP Tomcat 已启动,监听的端口是:" + port);
  79. f.channel().closeFuture().sync();
  80. }catch (Exception e){
  81. e.printStackTrace();
  82. }finally {
  83. // 关闭线程池
  84. bossGroup.shutdownGracefully();
  85. workerGroup.shutdownGracefully();
  86. }
  87. }
  88. public class GPTomcatHandler extends ChannelInboundHandlerAdapter {
  89. @Override
  90. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  91. if (msg instanceof HttpRequest){
  92. HttpRequest req = (HttpRequest) msg;
  93. // 转交给我们自己的request实现
  94. GPRequest request = new GPRequest(ctx,req);
  95. // 转交给我们自己的response实现
  96. GPResponse response = new GPResponse(ctx,req);
  97. // 实际业务处理
  98. String url = request.getUrl();
  99. if(servletMapping.containsKey(url)){
  100. servletMapping.get(url).service(request, response);
  101. }else{
  102. response.write("404 - Not Found");
  103. }
  104. }
  105. }
  106. @Override
  107. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  108. }
  109. }
  110. public static void main(String[] args) {
  111. new GPTomcat().start();
  112. }
  113. }