基于jdk1.8 NIO手写http server,并且部署到阿里云服务器,浏览器可访问

如果没有tomcat,你的java程序是不是单机版?
首先明确一个知识点(大前提),网络中的进程是通过socket来通信的
最近在学习socket(俗称 套接字),然后顺便了解一下http协议,手写一个http服务端出来玩一下,如果想实现其实也不是很复杂,需要了解一下http协议,了解一下使用socket编程,那就一切变得很简单了 ~~

# 1.0 简要了解 “发送” 一个http请求包含的信息

比如说我从浏览器访问我的一篇博客 https://shaines.cn/blog/detail/1183794984409358336
在这里结合使用Fiddler抓取浏览器数据包获取到的数据,如下

GET https://shaines.cn/blog/detail/1183794984409358336 HTTP/1.1
Host: shaines.cn
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Sec-Fetch-Site: cross-site
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9


说明

在这里插入图片描述

# 2.0 简要了解 “响应” 一个http请求包含的信息

HTTP/1.1 200
Server: nginx/1.14.2
Date: Sat, 19 Oct 2019 03:48:26 GMT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Content-Language: zh-CN

1f77
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="如果你知道python的requests有多好用,那么这个封装的就有多舒服!!!类似于一个浏览器端,自动管理cookie,对于爬虫来说,就是一个利器">
    <meta name="keywords" content="模仿jdk11中的HttpClient封装一个基于HttpURLConnection的实现">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>houyu blog | 文章详情</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="icon" type="image/png" href="/favicon.ico">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="Amaze UI"/>
    <meta name="msapplication-TileColor" content="#0e90d2">
    <link rel="stylesheet" href="/assets/css/amazeui.min.css">
    <link rel="stylesheet" href="/assets/css/app.css">
    <link rel="stylesheet" href="/assets/css/toastr.min.css">
</head>
<body id="blog-article-sidebar">
...
</body>
</html>
0

说明

在这里插入图片描述

# 3.0 使用jdk1.8 NIO实现http server

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description http 服务端
 * @date 2019-10-01 18:16:12
 * @author houyu for.houyu@foxmail.com
 */
public class HttpServer {

    private static final Logger logger = LoggerFactory.getLogger(HttpServer.class);
    private Selector selector = null;

    public static void main(String[] args) throws IOException {
        new HttpServer().start();
    }

    private void start() throws IOException {
        int port = 10006;
        ServerSocketChannel channel = ServerSocketChannel.open();
        channel.configureBlocking(false);
        selector = Selector.open();
        channel.register(selector, SelectionKey.OP_ACCEPT);
        channel.bind(new InetSocketAddress(port));
        logger.info("server start... bing port {}", port);
        beginAccept();
    }

    private void beginAccept() {
        while(true) {
            accept();
        }
    }

    private void accept() {
        try {
            int select = selector.select();
            if(select == 0) {
                return;
            }
            Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
            while(iter.hasNext()) {
                SelectionKey key = iter.next();
                HttpRequestHandler.build(key).run();
                iter.remove();
            }
        } catch(IOException e) {
            logger.warn("accept()", e);
        }
    }

    public static class HttpRequestHandler {

        private static final Charset CHARSET = Charset.forName("UTF-8");
        private static final int BUFFER_SIZE = 1024;
        private SelectionKey key;

        HttpRequestHandler(SelectionKey key) {
            this.key = key;
        }

        private static HttpRequestHandler build(SelectionKey key) {
            return new HttpRequestHandler(key);
        }

        private void run() {
            try {
                if(key.isAcceptable()) {
                    acceptHttpRequest();
                } else if(key.isReadable()) {
                    readHttpRequest();
                    returnHttpResponse();
                }
            } catch(IOException e) {
                logger.warn("run()", e);
            }
        }

        private void acceptHttpRequest() throws IOException {
            SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
            socketChannel.configureBlocking(false);
            socketChannel.finishConnect();
            //
            Selector selector = key.selector();
            // 注册读取事件
            socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(BUFFER_SIZE));
        }

        private void readHttpRequest() {
            SocketChannel channel = (SocketChannel) key.channel();
            ByteBuffer byteBuffer = (ByteBuffer) key.attachment();
            try {
                if(channel.read(byteBuffer) == -1) {
                    // 没读到内容关闭连接
                    channel.close();
                } else {
                    // 将channel改为读取状态(重置光标为0)
                    byteBuffer.flip();
                    String requestBody = CHARSET.newDecoder().decode(byteBuffer).toString();
                    InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.getRemoteAddress();
                    logger.debug("get request body from client({}) :\r\n{}", inetSocketAddress, requestBody);
                    byteBuffer.clear();
                }
            } catch(IOException e) {
                logger.warn("服务器读取错误", e);
            }
        }

        private void returnHttpResponse() throws IOException {
            SocketChannel channel = (SocketChannel) key.channel();
            //
            String ipAndPort = channel.getRemoteAddress().toString().replace("/", "");
            //
            StringBuilder builder = new StringBuilder(1024);
            // 增加响应消息行
            builder.append("HTTP/1.1 200 ok\r\n");
            // 增加响应消息头
            builder.append("Content-Type: application/json; charset=utf-8\r\n");
            // 空行
            builder.append("\r\n");
            // 响应消息体
            builder.append("{");
            builder.append("\"code\": 200, ");
            builder.append("\"message\": \"您的IP:").append(ipAndPort).append(", 请求成功\"");
            builder.append("}");
            ByteBuffer byteBuffer = (ByteBuffer) key.attachment();
            byteBuffer.clear();
            byteBuffer.put(builder.toString().getBytes(CHARSET));
            // 设置光标在开始处
            byteBuffer.flip();
            channel.write(byteBuffer);
            channel.close();
        }
    }
}

# 4.0 运行 HttpServer.main

2019-10-01 14:32:04.392 [main] INFO  cn.shaiens.http.server.HttpServer - server start... bing port 10006

# 5.0 使用浏览器访问

在这里插入图片描述

# 6.0 查看http server 后台打印日志

在这里插入图片描述

# 7.0 打成jar启动

IDEA 普通java工程打jar包
https://blog.csdn.net/JinglongSource/article/details/101063948

# 8.0 部署到阿里云服务器

上传到服务端, 进入目录之后, 执行

nohup java -jar http_server.jar >/dev/null 2>&1 &

关闭http_server

ps -ef | grep http_server | grep -v grep | awk '{print $2}' | xargs kill -9

# 9.0 使用logback日志框架可能出现的问题

我在本地测试的时候,发现一直没有出现日志文件,然后我使用win rar打开jar发现根目录下没有logback.xml文件,
因此需要把logback.xml复制进去才可以,具体是什么问题导致logback.xml打包的时候没进入到jar里边我也不知道, 如果你知道的话麻烦留言告诉一下, 感谢!!

在这里插入图片描述

# 10. 源码地址

https://gitee.com/jinglongcode/http_server.git

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值