hi-nginx-java是一个独立于java官方的servlet规范,它有能力把NGINX直接编成servlet容器服务器。换言之,无需安装tomcat等容器服务器,也无需使用nginx的反向代理功能,只需安装jdk8+和hi-nginx,就能进行java web开发工作,而且性能更好。

先看NGINX的配置文件部分:

hi_java_classpath "-Djava.class.path=.:/usr/local/nginx/java:/usr/local/nginx/java/hi-nginx-java.jar:/usr/local/nginx/java/jdemo.jar"

location ~ \.java {
            rewrite ^/(.*)\.java$ /$1 break;
            hi_need_kvdb on;
            hi_kvdb_size 50;
            hi_kvdb_expires 5m;
            hi_need_session on;
            hi_need_headers on;
            hi_need_cookies on;
            hi_java_servlet hi/jdemo;
        }

应用是jdemo.jar。hi-nginx需要调用的入口servlet是hi.jdemo,配置时用/斜杠代替.点。该类的实现如下:

package hi;

import hi.servlet;
import hi.route;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.regex.Matcher;

public class jdemo implements hi.servlet {

    private static hi.route r = hi.route.get_instance();

    public jdemo() {
        jdemo.r.get("^/(hello|test)/?$", (hi.request req, hi.response res, Matcher m) -> {
            this.do_hello(req, res);
        });
        jdemo.r.get("^/error/?$", (hi.request req, hi.response res, Matcher m) -> {
            this.do_error(req, res);
        });
        jdemo.r.get("^/redirect/?$", (hi.request req, hi.response res, Matcher m) -> {
            this.do_redirect(req, res);
        });
        jdemo.r.add(new ArrayList<String>(Arrays.asList("GET", "POST")), "^/form/?$",
                (hi.request req, hi.response res, Matcher m) -> {
                    this.do_form(req, res);
                });
        jdemo.r.get("^/session/?$", (hi.request req, hi.response res, Matcher m) -> {
            this.do_session(req, res);
        });
        jdemo.r.get("^/md5/?$", (hi.request req, hi.response res, Matcher m) -> {
            this.do_md5(req, res);
        });
    }

    public void handler(hi.request req, hi.response res) {
        jdemo.r.run(req, res);
    }

    private void do_hello(hi.request req, hi.response res) {
        res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
        res.status = 200;
        res.content = "hello,world";
    }

    private void do_error(hi.request req, hi.response res) {
        res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
        res.status = 404;
        res.content = "404 Not found";
    }

    private void do_redirect(hi.request req, hi.response res) {
        res.status = 302;
        ArrayList<String> h = new ArrayList<String>();
        h.add("/hello.java");
        res.headers.put("Location", h);
    }

    private void do_form(hi.request req, hi.response res) {
        res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
        res.status = 200;
        StringBuilder buffer = new StringBuilder();

        buffer.append("head data " + req.headers.size() + "\n");
        buffer.append(this.do_foreach(req.headers));

        buffer.append("\ncookie data " + req.cookies.size() + "\n");
        buffer.append(this.do_foreach(req.cookies));

        buffer.append("\nform data " + req.form.size() + "\n");
        buffer.append(this.do_foreach(req.form));

        buffer.append(String.format("\nclient= %s\nmethod= %s\nuser_agent= %s\nuri= %s\nparam= %s\n", req.client,
                req.method, req.user_agent, req.uri, req.param));

        res.content = buffer.toString();
    }

    private void do_session(hi.request req, hi.response res) {
        res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
        res.status = 200;
        String key = "test";
        int value = 0;
        if (req.session.containsKey(key)) {
            value = Integer.parseInt(req.session.get(key)) + 1;
        }
        res.session.put(key, String.valueOf(value));
        res.content = String.format("hello,%d", value);
        res.status = 200;
    }

    private void do_md5(hi.request req, hi.response res) {
        res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
        String plaintext = "hello,md5!";
        res.status = 200;
        res.content = String.format("%s\nmd5= %s", plaintext, this.md5(plaintext));
    }

    private String md5(String str) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(str.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }

    private String do_foreach(HashMap<String, String> m) {
        StringBuffer buffer = new StringBuffer();
        for (HashMap.Entry<String, String> item : m.entrySet()) {
            buffer.append(String.format("%s\t=\t%s\n", item.getKey(), item.getValue()));
        }
        return buffer.toString();
    }
}

将其用javac和jar编译组装为jdemo.jar文件后,安装至/usr/local/nginx/java目录中。编写好上述nginx配置,restart或者reload nginx即可通过访问http://localhost/*.java即可获得相应的服务。

hi.route的hi-nginx-java自带的一个路由器,可实现类似python Flask框架的功能。如此,可大幅度省去tomcat应用发布时繁琐的配置过程,而且能够获得更好的应用性能。如果通过hi_need_cache开启LRU缓存,无论多短的缓存时间,哪怕仅仅1秒,也能使得应用获得近乎nginx静态文件服务一样的性能表现,并且不会想tomcat等服务器那样大幅度消耗cpu和内存。

11-07 16:48