Skip to content

Commit

Permalink
improve nginx proxy yaml parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jarviszeng-zjc committed Sep 30, 2020
1 parent 2d20c75 commit f35c15c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 97 deletions.
62 changes: 1 addition & 61 deletions c/proxy/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ http {

#gzip on;
upstream fate_cluster {
server 127.0.0.1:9360;
server 0.0.0.1; # just an invalid address as a place holder
balancer_by_lua_file 'lua/balancer.lua';
}
lua_package_path "/path/to/lua_dir/?.lua;;";
Expand All @@ -59,65 +59,5 @@ http {
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;
# }
#}

}
19 changes: 13 additions & 6 deletions c/proxy/conf/route_table.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
default:
proxy:
- host: 0.0.0.0
grpc_port: 9380
- host: 127.0.0.1
port: 9373
9999:
proxy:
- host: 0.0.0.0
grpc_port: 9380
- host: 127.0.0.1
port: 9373
fateflow:
- host: 0.0.0.0
grpc_port: 9380
- host: 127.0.0.1
port: 9360
10000:
proxy:
- host: 127.0.0.1
port: 9373
fateflow:
- host: 127.0.0.1
port: 9362
3 changes: 2 additions & 1 deletion c/proxy/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ local function balance()
local fate_cluster_server = ngx.ctx.fate_cluster_server
local ok, err = ngx_balancer.set_current_peer(fate_cluster_server)
if not ok then
utils.exit_abnormally('failed to set current peer: ' .. err, ngx.HTTP_SERVICE_UNAVAILABLE)
ngx.log(ngx.INFO, 'failed to set current peer: ' .. err, ngx.HTTP_SERVICE_UNAVAILABLE)
return ngx.ERROR
end
end

Expand Down
14 changes: 12 additions & 2 deletions c/proxy/lua/route_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
--
local ngx = ngx
local new_timer = ngx.timer.at
local yaml_parser = require "yaml_parser"
local io = io

local _M = {
_VERSION = '0.1'
}
Expand All @@ -30,8 +33,15 @@ if not route_cache then
end

local function reload_route_table()
route_cache:set("9999", { fateflow = "127.0.0.1:9360" })
route_cache:set("10000", { fateflow = "127.0.0.1:9362" })
--route_cache:set("9999", { fateflow = "127.0.0.1:9360" })
--route_cache:set("10000", { fateflow = "127.0.0.1:9362" })
local file = io.open("/Users/jarviszeng/Work/Project/FDN/FATE/c/proxy/conf/route_table.yaml", "r") -- 使用 io.open() 函数,以只读模式打开文件
local content = file:read("*a")
file:close()
local yaml_table = yaml_parser.parse(content)
for k, v in pairs(yaml_table)do
route_cache:set(k, v)
end
ngx.log(ngx.INFO, "reload route table")
end

Expand Down
24 changes: 16 additions & 8 deletions c/proxy/lua/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,27 @@ local _M = {

local ngx = ngx
local route_table = require "route_table"
local math = require "math"
local string = require "string"

local function get_upstream_server(dest_party_id, dest_service)
ngx.log(ngx.INFO, string.format("try to get %s %s upstream", dest_party_id, dest_service))
local function get_server_address(server)
return string.format("%s:%s", server["host"], server["port"])
end

local function get_dest_server(dest_party_id, dest_service)
ngx.log(ngx.INFO, string.format("try to get %s %s server", dest_party_id, dest_service))

local route = route_table.get_route()
local party_services = route:get(dest_party_id)
local server
if party_services ~= nil then
local server = party_services[dest_service]
ngx.log(ngx.INFO, server)
return server
local service = party_services[dest_service]
server = get_server_address(service[math.random(1, #service)])
else
local default_party_services = route:get("default")
local default_proxy = route:get("default")["proxy"]
server = get_server_address(default_proxy[math.random(1, #default_proxy)])
end
ngx.log(ngx.INFO, string.format("get %s %s server: %s", dest_party_id, dest_service, server))
return server
end

Expand All @@ -42,8 +50,8 @@ end

function routing()
local request_headers = get_request_dest()
local forward_server = get_upstream_server(request_headers["dest-party-id"], request_headers["service"])
ngx.ctx.fate_cluster_server = forward_server
local dest_forward_server = get_dest_server(tonumber(request_headers["dest-party-id"]), request_headers["service"])
ngx.ctx.fate_cluster_server = dest_forward_server
end

routing()
Expand Down
34 changes: 15 additions & 19 deletions c/proxy/lua/yaml_parser.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
-- YAMLParserLite = class("YAMLParserLite")

-- function YAMLParserLite:initialize()
-- end

-- function YAMLParserLite:parse(yaml)
-- local lines = {}
-- for line in string.gmatch(yaml..'\n', '(.-)\n') do
-- table.insert(lines, line)
-- end

-- local docs = parse_documents(lines)
-- if #docs == 1 then
-- return docs[1]
-- end
-- return docs
-- end

-- 以上是为了 配合个人已有结构
--
-- Copyright 2019 The FATE Authors. All Rights Reserved.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

local schar = string.char
local ssub, gsub = string.sub, string.gsub
Expand Down

0 comments on commit f35c15c

Please sign in to comment.