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)容。