hi-nginx-java既可以通過實作hi.servlet抽象來像Flask那樣快速配置路由,例如:
1 hi.route r = hi.route.get_instance(); 2 r.get("^/(hello|test)/?$", this::do_hello);
也能繞過hi.servlet抽象實作無配置路由配置,即通過實作hi.route.run_t抽象直奔業務邏輯而去,例如:
1 package hi; 2 3 import hi.request; 4 import hi.response; 5 import hi.route; 6 import java.util.regex.Matcher; 7 8 public class helloworld implements hi.route.run_t { 9 public helloworld() { 10 } 11 12 public void handler(hi.request req, hi.response res, Matcher m) { 13 res.set_content_type("text/plain;charset=utf-8"); 14 res.content = "hello,world\n"; 15 res.status = 200; 16 } 17 }
要呼叫hi.helloworld類,只需在hi-nginx中配置hi-nginx-java自帶的默認控制器hi.controller即可:
location ~ \.java {
rewrite ^/(.*)\.java$ /$1 break;
hi_java_servlet hi/controller;
}
如此,訪問http://localhost/hi/helloworld.java即可獲得相應服務,
所以,hi-nginx-java業已實作一種URI和class之間的映射關系,如果URI是/a/b/c.java,那么相應的服務類就是a.b.c,這一切完全是自動完成的,無需任何配置,至于.java的后綴,則可任君選取,無任何限制,
另外,hi-nginx-java通過LRU+過期時間的混合演算法更新URI和class之間的映射關系,默認過期時間是300秒,也就是,如果你更新了業務實作,無需做任何事,300秒后,服務器將自動更新相關業務邏輯,從而無需通過restart或者reload重啟hi-nginx服務器,當然,如果著急看更新后的結果,即可通過restart或者reload重啟hi-nginx服務器,也可通過配置較短的過期時間來達到目的,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/230595.html
標籤:Java
上一篇:golang拾遺:嵌入型別
