基于Nginx&Lua 和Netflix Eureka的微服務(wù)網(wǎng)關(guān)

2F89DFFD-CB32-436B-97A9-281F7E40F02B.png

Zuul for Nginx&Lua

基于Nginx和Lua module白筹。需要安裝Nginx Lua模塊或者直接下載openresty編譯安裝敛摘。

安裝和配置ngx-lua-zuul

下載代碼到/path/to/nginx/lua/lib/

git clone http://github.com/tietang/ngx-lua-zuul --depth=1

配置nginx.conf

從nginx.conf.default 復(fù)制nginx.conf,也可以copy下面的例子并保存為nginx.conf,以快速運(yùn)行。

在nginx.conf的http節(jié)點(diǎn)中添加:


    lua_package_path "/Users/tietang/nginx/nginx/lua/lib/?.lua;;";
    lua_shared_dict discovery 10m;

    lua_shared_dict apps_count 1m;
    lua_shared_dict apps_res_time 10m;
    lua_shared_dict api_count 10m;
    lua_shared_dict api_res_time 10m;

    init_by_lua '
        discovery = require "discovery"
        json=require "cjson"
        balancer=require "robin"
        router = require "router" 



    ';
    init_worker_by_lua '
        discovery:schedule() 
    ';

location 中添加:

 location / {
            set $bk_host '';


            # default_type text/html;
            access_by_lua_file lua/lib/app_route.lua;


            
            proxy_set_header Host   $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_pass http://$bk_host;
            log_by_lua_file lua/lib/stats.lua;
            #
 }

下面是一個(gè)完整的例子:


#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    ##lua 代碼路徑
    lua_package_path "/Users/tietang/nginx/nginx/lua/lib/?.lua;;";
    ##一些共享緩存
    lua_shared_dict discovery 10m;

    lua_shared_dict apps_count 1m;
    lua_shared_dict apps_res_time 10m;
    lua_shared_dict api_count 10m;
    lua_shared_dict api_res_time 10m;
    lua_shared_dict metrics 10m;
    lua_shared_dict metrics_time 10m;
    ##初始化
    init_by_lua '
        discovery = require "discovery"
        json=require "cjson"
        balancer=require "robin"
        router = require "router"



    ';
    ## 初始化定時(shí)器
    init_worker_by_lua '
        discovery:schedule()
    ';

    server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;
         
        #access_log  logs/host.access.log  main;

        location = / {
            set $dir $document_root;
            root   $dir/html;
            index  index.html index.htm;
        }

        # Dashboard路徑
        location ^~ /_dashboard {
            set $dir "./html";

            # root   $dir/html/;
            alias $dir;
            index  index.html index.htm;

             
        }
        # 靜態(tài)文件路徑
        location ^~ /public/ {
            set $dir "./html/public/";
            alias $dir;
        }
        # 一些管理功能-例子統(tǒng)計(jì)
        location = /_admin/stats {
            default_type text/html; 
            content_by_lua_file lua/lib/stats_show.lua;
        }

        location = /_admin/stats.json {
             default_type application/json;
             content_by_lua_file lua/lib/stats_show_json.lua;
        }

        location = /_admin/data.json {
             default_type application/json;
             content_by_lua '
                local v={}
                v.date=ngx.now()*1000
                -- string.gsub(ngx.localtime()," ",":")
                v.close=math.random(2,100) 

                ngx.say( json.encode(v)) 

             ';
        }

        location = /_admin/data.tsv {
             default_type application/json;
             content_by_lua '
                ngx.say("date   close")
                ngx.say(string.gsub(ngx.localtime()," ",":") .. "    ".. math.random(20,600) )

             ';
        }

        ## 請(qǐng)求轉(zhuǎn)發(fā)處理
        location / {
            set $bk_host '';


            # default_type text/html;
            # 路由計(jì)算
            access_by_lua_file lua/lib/app_route.lua;


            
            proxy_set_header Host   $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_pass http://$bk_host;
            # 簡(jiǎn)單統(tǒng)計(jì)收集
            log_by_lua_file lua/lib/stats.lua;
            #
        }


        #用于調(diào)試的例子
        location = /haha {

            default_type application/json;
            # access_by_lua_file demo/demo.lua;
           content_by_lua '
                ngx.say(ngx.var.host)
                ngx.say(ngx.time())
                ngx.say(ngx.now())
                ngx.say( json.encode(router.routingTable) .."<br/>")
                 ngx.say(  "<br/>")
                ngx.say(json.encode(discovery.hosts ) .."<br/>")

            ';
            #
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

例子Eureka 服務(wù)

如果沒(méi)有Eureka環(huán)境,也可以編譯安裝本例子中的EurekaDemo服務(wù),參考:編譯和運(yùn)行eureka-demo服務(wù)

運(yùn)行測(cè)試

啟動(dòng)所有的demo服務(wù):discovery,api,zuul执赡;

啟動(dòng)nginx;

打開(kāi)瀏覽器:http://127.0.0.1:8000/api/test/0/0

其測(cè)試api參考編譯和運(yùn)行eureka-demo服務(wù)中的相關(guān)內(nèi)容。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末佣盒,一起剝皮案震驚了整個(gè)濱河市盯仪,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌滞伟,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鳖轰,死亡現(xiàn)場(chǎng)離奇詭異总处,居然都是意外死亡忆肾,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)渠缕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)棒坏,“玉大人,你說(shuō)我怎么就攤上這事测暗。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵袄友,是天一觀的道長(zhǎng)殿托。 經(jīng)常有香客問(wèn)我,道長(zhǎng)剧蚣,這世上最難降的妖魔是什么鸠按? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任瑟曲,我火速辦了婚禮篮赢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘示辈。我一直安慰自己寥茫,他們只是感情好纱耻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著弄喘,像睡著了一般玖喘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蘑志,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天累奈,我揣著相機(jī)與錄音,去河邊找鬼急但。 笑死澎媒,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的波桩。 我是一名探鬼主播戒努,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼镐躲!你這毒婦竟也來(lái)了储玫?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤匀油,失蹤者是張志新(化名)和其女友劉穎缘缚,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體敌蚜,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年窝爪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了弛车。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蒲每,死狀恐怖纷跛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情邀杏,我是刑警寧澤贫奠,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站望蜡,受9級(jí)特大地震影響唤崭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜脖律,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一谢肾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧小泉,春花似錦芦疏、人聲如沸冕杠。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)分预。三九已至,卻和暖如春薪捍,著一層夾襖步出監(jiān)牢的瞬間笼痹,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工飘诗, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留与倡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓昆稿,卻偏偏與公主長(zhǎng)得像纺座,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子溉潭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容