Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: allowed to use different router. #364

Merged
merged 4 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ apisix:
enable_debug: false
allow_admin: # http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
- 127.0.0.0/24
# port_admin: 9180 # use a separate port
real_ip_header: "X-Real-IP" # http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_header
real_ip_from: # http://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
- 127.0.0.1
- 'unix:'
# port_admin: 9180 # use a separate port
route_idx: 'uri' # how to create the route index:
# `uri`: only use `uri` for routing
# `host+uri`: use `host+uri` for routing
router:
http: 'r3_uri' # r3_uri: match route by uri(base on r3)
# r3_host_uri: match route by host + uri(base on r3)
ssl: 'r3_sni'

etcd:
host: "http://127.0.0.1:2379" # etcd address
Expand Down
18 changes: 17 additions & 1 deletion doc/architecture-design-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [**Consumer**](#consumer)
- [**Plugin**](#plugin)
- [**Upstream**](#upstream)
- [**Router**](#router)
- [**Debug mode**](#Debug-mode)

## apisix
Expand Down Expand Up @@ -320,11 +321,26 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
}'
```

更多细节可以参考[健康检查的文档](health-check.md).
更多细节可以参考[健康检查的文档](health-check.md)

[返回目录](#目录)


## Router

APISIX 区别于其他 API 网关的一大特点是允许用户选择不同路由实现来更好匹配自由业务。这样可以更好的在性能、自由之间做最佳选择。

在本地配置 `conf/config.yaml` 中设置最符合自身业务需求的路由,可获得更好的匹配效率。

* `apisix.router.http`: HTTP 请求路由。
* `r3_uri`: 默认值,只使用 `uri` 作为主索引(基于 r3 引擎)。
* `r3_host_uri`: 使用 `host + uri` 作为主索引(基于 r3 引擎),对当前请求会同时匹配 host 和 uri。

* `apisix.router.ssl`: SSL 加载匹配路由。
* `r3_sni`: 默认值,使用 `SNI` (Server Name Indication) 作为主索引(基于 r3 引擎)。

[返回目录](#目录)

## Debug mode

开启调试模式后,会在请求应答时,输出更多的内部信息,比如加载了哪些插件等。
Expand Down
37 changes: 6 additions & 31 deletions lua/apisix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@

local require = require
local core = require("apisix.core")
local router = require("apisix.http.route").get
local plugin = require("apisix.plugin")
local service_fetch = require("apisix.http.service").get
local ssl_match = require("apisix.http.ssl").match
local admin_init = require("apisix.admin.init")
local get_var = require("resty.ngxvar").fetch
local local_conf = core.config.local_conf
local router = require("apisix.http.router")
local ngx = ngx
local get_method = ngx.req.get_method
local ngx_exit = ngx.exit
local ngx_ERROR = ngx.ERROR
local str_reverse = string.reverse
local math = math
local match_opts = {}
local error = error
local load_balancer


local _M = {version = 0.1}
local _M = {version = 0.2}


function _M.http_init()
Expand Down Expand Up @@ -56,11 +52,10 @@ function _M.http_init_worker()
load_balancer = require("apisix.http.balancer").run

require("apisix.admin.init").init_worker()
require("apisix.http.balancer").init_worker()

require("apisix.http.route").init_worker()
router.init_worker()
require("apisix.http.service").init_worker()
require("apisix.http.ssl").init_worker()

require("apisix.plugin").init_worker()
require("apisix.consumer").init_worker()
end
Expand Down Expand Up @@ -133,7 +128,7 @@ function _M.http_ssl_phase()
ngx_ctx.api_ctx = api_ctx
end

local ok, err = ssl_match(api_ctx)
local ok, err = router.router_ssl.match(api_ctx)
if not ok then
if err then
core.log.error("failed to fetch ssl config: ", err)
Expand All @@ -153,28 +148,8 @@ function _M.http_access_phase()
end

core.ctx.set_vars_meta(api_ctx)
core.table.clear(match_opts)
match_opts.method = api_ctx.var.method

local ok

if local_conf().apisix
and local_conf().apisix.route_idx == "host+uri" then
local host = api_ctx.var.host
host = host and str_reverse(host) or "[^/]+"
host = host .. api_ctx.var.uri
ok = router():dispatch2(nil, host, match_opts, api_ctx)
core.log.info("match string: ", host)

else
match_opts.host = api_ctx.var.host
ok = router():dispatch2(nil, api_ctx.var.uri, match_opts, api_ctx)
end

if not ok then
core.log.info("not find any matched route")
return core.response.exit(404)
end
router.router_http.match(api_ctx)

core.log.info("route: ",
core.json.delay_encode(api_ctx.matched_route, true))
Expand Down
6 changes: 3 additions & 3 deletions lua/apisix/admin/services.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local core = require("apisix.core")
local routes = require("apisix.http.route").routes
local get_routes = require("apisix.http.router").http_routes
local schema_plugin = require("apisix.admin.plugins").check_schema
local tostring = tostring
local ipairs = ipairs
Expand All @@ -8,7 +8,7 @@ local type = type


local _M = {
version = 0.1,
version = 0.2,
}


Expand Down Expand Up @@ -126,7 +126,7 @@ function _M.delete(id)
return 400, {error_msg = "missing service id"}
end

local routes, routes_ver = routes()
local routes, routes_ver = get_routes()
core.log.info("routes: ", core.json.delay_encode(routes, true))
core.log.info("routes_ver: ", routes_ver)
if routes_ver and routes then
Expand Down
4 changes: 2 additions & 2 deletions lua/apisix/admin/upstreams.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local core = require("apisix.core")
local get_routes = require("apisix.http.route").routes
local get_routes = require("apisix.http.router").http_routes
local get_services = require("apisix.http.service").services
local tostring = tostring
local ipairs = ipairs
Expand All @@ -8,7 +8,7 @@ local type = type


local _M = {
version = 0.1,
version = 0.2,
}


Expand Down
120 changes: 0 additions & 120 deletions lua/apisix/http/route.lua

This file was deleted.

33 changes: 33 additions & 0 deletions lua/apisix/http/router.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local require = require
local core = require("apisix.core")
local local_conf = core.config.local_conf


local _M = {version = 0.1}


function _M.init_worker()
local conf = local_conf()
local router_http_name = "r3_uri"
local router_ssl_name = "r3_sni"
if conf and conf.apisix and conf.apisix.router then
router_http_name = conf.apisix.router.http or router_http_name
router_ssl_name = conf.apisix.router.ssl or router_ssl_name
end

local router_http = require("apisix.http.router." .. router_http_name)
router_http.init_worker()
_M.router_http = router_http

local router_ssl = require("apisix.http.router." .. router_ssl_name)
router_ssl:init_worker()
_M.router_ssl = router_ssl
end


function _M.http_routes()
return _M.router_http.routes()
end


return _M
Loading