zoukankan      html  css  js  c++  java
  • NIO开发Http服务器(2):项目结构

    最近学习了Java NIO技术,觉得不能再去写一些Hello World的学习demo了,而且也不想再像学习IO时那样编写一个控制台(或者带界面)聊天室。我们是做WEB开发的,整天围着tomcatnginx转,所以选择了一个新的方向,就是自己开发一个简单的Http服务器,在总结Java NIO的同时,也加深一下对http协议的理解。

    项目实现了静态资源(htmlcssjs和图片)和简单动态资源的处理,可以实现监听端口、部署目录、资源过期的配置。涉及到了NIO缓冲区、通道和网络编程的核心知识点,还是比较基础的。

    上一篇文章《NIO开发Http服务器(1):项目下载、打包和部署》介绍了项目的运行环境和部署启动,本文就介绍一下开发环境、项目结构以及如何在eclipse中启动

    文章目录:

    NIO开发Http服务器(1):项目下载、打包和部署

    NIO开发Http服务器(2):项目结构

    NIO开发Http服务器(3):核心配置和Request封装

    NIO开发Http服务器(4):Response封装和响应

    NIO开发Http服务器(5-完结):HttpServer服务器类

    1、结构概览

    把项目导入到eclipse

    • 项目核心类在org.net5ijy.nio.http包下面,使用RunHttpServer启动服务器
    • 服务器主配置文件是server.properties
    • 日志默认存放在工作目录下的logs目录下
    • Web站点默认部署在工作目录下的WebContent目录下

    2、开发环境

    操作系统

    Windows 7 64位操作系统

    JDK

    java version "1.8.0_141"

    maven

    Apache Maven 3.2.1

    工作目录

    D:\_tmp

    Eclipse

    Luna Service Release 2 (4.4.2)

    3、maven依赖

      1 <groupId>org.net5ijy.nio</groupId>
      2 <artifactId>http-server</artifactId>
      3 <version>0.0.1-SNAPSHOT</version>
      4 
      5 <name>nio</name>
      6 <url>http://www.example.com</url>
      7 
      8 <properties>
      9     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     10     <maven.compiler.source>1.8</maven.compiler.source>
     11     <maven.compiler.target>1.8</maven.compiler.target>
     12 </properties>
     13 
     14 <dependencies>
     15     <!-- logback日志 -->
     16     <dependency>
     17         <groupId>ch.qos.logback</groupId>
     18         <artifactId>logback-core</artifactId>
     19         <version>1.1.7</version>
     20     </dependency>
     21     <dependency>
     22         <groupId>ch.qos.logback</groupId>
     23         <artifactId>logback-classic</artifactId>
     24         <version>1.1.7</version>
     25     </dependency>
     26         <!-- log4j日志 -->
     27         <!-- <dependency>
     28         <groupId>log4j</groupId>
     29         <artifactId>log4j</artifactId>
     30         <version>1.2.17</version>
     31     </dependency>
     32     <dependency>
     33         <groupId>org.slf4j</groupId>
     34         <artifactId>slf4j-log4j12</artifactId>
     35         <version>1.6.1</version>
     36     </dependency> -->
     37     <!-- JSON解析 -->
     38     <dependency>
     39         <groupId>com.fasterxml.jackson.core</groupId>
     40         <artifactId>jackson-core</artifactId>
     41         <version>2.9.4</version>
     42     </dependency>
     43     <dependency>
     44         <groupId>com.fasterxml.jackson.core</groupId>
     45         <artifactId>jackson-databind</artifactId>
     46         <version>2.9.4</version>
     47     </dependency>
     48     <!-- 测试 -->
     49     <dependency>
     50         <groupId>junit</groupId>
     51         <artifactId>junit</artifactId>
     52         <version>4.10</version>
     53     </dependency>
     54 </dependencies>
     55 
     56 <build>
     57     <plugins>
     58         <plugin>
     59             <groupId>org.apache.maven.plugins</groupId>
     60             <artifactId>maven-compiler-plugin</artifactId>
     61             <version>3.3</version>
     62             <configuration>
     63                 <source>${maven.compiler.source}</source>
     64                 <target>${maven.compiler.target}</target>
     65                 <encoding>${project.build.sourceEncoding}</encoding>
     66             </configuration>
     67         </plugin>
     68         <plugin>
     69             <groupId>org.apache.maven.plugins</groupId>
     70             <artifactId>maven-source-plugin</artifactId>
     71             <version>3.0.1</version>
     72             <executions>
     73                 <execution>
     74                     <phase>compile</phase>
     75                     <goals>
     76                         <goal>jar</goal>
     77                     </goals>
     78                 </execution>
     79             </executions>
     80         </plugin>
     81         <plugin>
     82             <artifactId>maven-antrun-plugin</artifactId>
     83             <executions>
     84                 <execution>
     85                     <phase>package</phase>
     86                     <goals>
     87                         <goal>run</goal>
     88                     </goals>
     89                     <configuration>
     90                         <tasks>
     91                             <mkdir dir="${project.build.directory}/build" />
     92                             <copy todir="${project.build.directory}/build" overwrite="true">
     93                                 <fileset dir="." erroronmissingdir="false">
     94                                     <include name="WebContent/**" />
     95                                 </fileset>
     96                             </copy>
     97                         </tasks>
     98                     </configuration>
     99                 </execution>
    100             </executions>
    101         </plugin>
    102         <plugin>
    103             <groupId>org.codehaus.mojo</groupId>
    104             <artifactId>appassembler-maven-plugin</artifactId>
    105             <version>1.10</version>
    106             <configuration>
    107                 <platforms>
    108                     <platform>windows</platform>
    109                 </platforms>
    110                 <assembleDirectory>${project.build.directory}/build</assembleDirectory>
    111                 <repositoryName>lib</repositoryName>
    112                 <binFolder>.</binFolder>
    113                 <configurationDirectory>conf</configurationDirectory>
    114                 <copyConfigurationDirectory>true</copyConfigurationDirectory>
    115                 <configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
    116                 <repositoryLayout>flat</repositoryLayout>
    117                 <encoding>UTF-8</encoding>
    118                 <logsDirectory>logs</logsDirectory>
    119                 <tempDirectory>tmp</tempDirectory>
    120                 <programs>
    121                     <program>
    122                         <id>httpserver</id>
    123                         <mainClass>org.net5ijy.nio.http.RunHttpServer</mainClass>
    124                         <jvmSettings>
    125                             <extraArguments>
    126                                 <extraArgument>-server</extraArgument>
    127                                 <extraArgument>-Xmx256M</extraArgument>
    128                                 <extraArgument>-Xms256M</extraArgument>
    129                             </extraArguments>
    130                         </jvmSettings>
    131                     </program>
    132                 </programs>
    133             </configuration>
    134         </plugin>
    135     </plugins>
    136 </build>
    View Code

    4、启动服务

    第一种方式

    直接运行RunHttpServer主类,右键Run as -> Java Application

    第二种方式,像上一章使用mvn打包发布,然后运行生成的bat脚本

    右键pom.xml -> Run as -> Maven build...

    5、浏览器访问和日志级别

    http://localhost:8082/nio/test/index.html

    可以看到后台输出了大量的debug日志,如果不需要看日志,可以改为info级别

    6、替换日志框架

    如果您不想使用logback日志框架,可以修改pom.xml文件,把logback的依赖注释掉,再把log4j依赖的注释去掉即可。

    7、Github

    https://github.com/xuguofeng/http-server

  • 相关阅读:
    POJ1296
    BZOJ1003
    POJ1160
    中国剩余定理(转)
    组合数公式
    网络操作系统*习题
    网络操作系统*习题
    网络操作系统习题
    网络操作系统习题
    Access总结
  • 原文地址:https://www.cnblogs.com/xugf/p/9576967.html
Copyright © 2011-2022 走看看