Skip to content

Commit

Permalink
WIP: support dubbo
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander committed Jan 12, 2021
1 parent d030d3c commit 1d991cb
Show file tree
Hide file tree
Showing 23 changed files with 1,069 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ jobs:
- name: Linux Get dependencies
run: sudo apt install -y cpanminus build-essential libncurses5-dev libreadline-dev libssl-dev perl

- name: Start Dubbo Backend
if: matrix.os_name == 'linux_openresty'
run: |
sudo apt install -y maven
cd t/lib/dubbo_backend
mvn package > build.log
cd dubbo-backend-provider/target
java -Djava.net.preferIPv4Stack=true -jar dubbo-demo-provider.one-jar.jar &
- name: Linux Before install
run: sudo ./.travis/${{ matrix.os_name }}_runner.sh before_install

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ fastcgi_temp
client_body_temp
utils/lj-releng
default.etcd/
t/lib/dubbo_backend/dubbo-backend-interface/target/
t/lib/dubbo_backend/dubbo-backend-provider/target/
.idea/
*.iml
\.*
Expand Down
53 changes: 53 additions & 0 deletions .travis/apisix_cli_test/test_dubbo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

. ./.travis/apisix_cli_test/common.sh

# enable dubbo
echo '
plugins:
- dubbo-proxy
' > conf/config.yaml

make init

if ! grep "location @dubbo_pass " conf/nginx.conf > /dev/null; then
echo "failed: dubbo location not found in nginx.conf"
exit 1
fi

echo "passed: found dubbo location in nginx.conf"

# dubbo multiplex configuration
echo '
plugins:
- dubbo-proxy
plugin_attr:
dubbo-proxy:
upstream_multiplex_count: 16
' > conf/config.yaml

make init

if ! grep "multi 16;" conf/nginx.conf > /dev/null; then
echo "failed: dubbo multiplex configuration not found in nginx.conf"
exit 1
fi

echo "passed: found dubbo multiplex configuration in nginx.conf"
2 changes: 2 additions & 0 deletions .travis/linux_openresty_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
#

export OPENRESTY_VERSION=source
. ./.travis/common.sh

before_install() {
Expand Down Expand Up @@ -137,6 +138,7 @@ script() {
sudo bash ./utils/check-plugins-code.sh

make lint && make license-check || exit 1
lsof -i :20880
# APISIX_ENABLE_LUACOV=1 PERL5LIB=.:$PERL5LIB prove -Itest-nginx/lib -r t
PERL5LIB=.:$PERL5LIB prove -Itest-nginx/lib -r t
}
Expand Down
40 changes: 40 additions & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ http {
keepalive 320;
}
{% if enabled_plugins["dubbo-proxy"] then %}
upstream apisix_dubbo_backend {
server 0.0.0.1;
balancer_by_lua_block {
apisix.http_balancer_phase()
}
multi {* dubbo_upstream_multiplex_count *};
keepalive 320;
}
{% end %}
init_by_lua_block {
require "resty.core"
apisix = require("apisix")
Expand Down Expand Up @@ -398,6 +410,10 @@ http {
set $upstream_scheme 'http';
set $upstream_host $host;
set $upstream_uri '';
set $ctx_ref '';
set $dubbo_service_name '';
set $dubbo_service_version '';
set $dubbo_method '';
{% if with_module_status then %}
location = /apisix/nginx_status {
Expand Down Expand Up @@ -556,6 +572,30 @@ http {
}
}
{% if enabled_plugins["dubbo-proxy"] then %}
location @dubbo_pass {
access_by_lua_block {
apisix.dubbo_access_phase()
}
dubbo_pass_all_headers on;
dubbo_pass_body on;
dubbo_pass $dubbo_service_name $dubbo_service_version $dubbo_method apisix_dubbo_backend;
header_filter_by_lua_block {
apisix.http_header_filter_phase()
}
body_filter_by_lua_block {
apisix.http_body_filter_phase()
}
log_by_lua_block {
apisix.http_log_phase()
}
}
{% end %}
{% if enabled_plugins["proxy-mirror"] then %}
location = /proxy_mirror {
internal;
Expand Down
9 changes: 9 additions & 0 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ Please modify "admin_key" in conf/config.yaml .
yaml_conf.apisix.ssl.ssl_cert = "cert/ssl_PLACE_HOLDER.crt"
yaml_conf.apisix.ssl.ssl_cert_key = "cert/ssl_PLACE_HOLDER.key"

local dubbo_upstream_multiplex_count = 32
if yaml_conf.plugin_attr and yaml_conf.plugin_attr["dubbo-proxy"] then
local dubbo_conf = yaml_conf.plugin_attr["dubbo-proxy"]
if tonumber(dubbo_conf.upstream_multiplex_count) >= 1 then
dubbo_upstream_multiplex_count = dubbo_conf.upstream_multiplex_count
end
end

-- Using template.render
local sys_conf = {
lua_path = env.pkg_path_org,
Expand All @@ -244,6 +252,7 @@ Please modify "admin_key" in conf/config.yaml .
with_module_status = with_module_status,
error_log = {level = "warn"},
enabled_plugins = enabled_plugins,
dubbo_upstream_multiplex_count = dubbo_upstream_multiplex_count,
}

if not yaml_conf.apisix then
Expand Down
12 changes: 12 additions & 0 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local admin_init = require("apisix.admin.init")
local get_var = require("resty.ngxvar").fetch
local router = require("apisix.router")
local set_upstream = require("apisix.upstream").set_by_route
local ctxdump = require("resty.ctxdump")
local ipmatcher = require("resty.ipmatcher")
local ngx = ngx
local get_method = ngx.req.get_method
Expand All @@ -34,6 +35,7 @@ local ipairs = ipairs
local tostring = tostring
local type = type
local ngx_now = ngx.now
local ngx_var = ngx.var
local str_byte = string.byte
local str_sub = string.sub
local tonumber = tonumber
Expand Down Expand Up @@ -543,6 +545,16 @@ function _M.http_access_phase()
end

set_upstream_host(api_ctx)

if api_ctx.dubbo_proxy_enabled then
ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
return ngx.exec("@dubbo_pass")
end
end


function _M.dubbo_access_phase()
ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
end


Expand Down
64 changes: 64 additions & 0 deletions apisix/plugins/dubbo-proxy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You 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 core = require("apisix.core")
local ngx_var = ngx.var


local plugin_name = "dubbo-proxy"

local schema = {
type = "object",
properties = {
service_name = {
type = "string",
minLength = 1,
},
service_version = {
type = "string",
pattern = [[^\d+\.\d+\.\d+]],
},
method = {
type = "string",
minLength = 1,
},
},
required = { "service_name", "service_version", "method" },
}

local _M = {
version = 0.1,
priority = 507,
name = plugin_name,
schema = schema,
}


function _M.check_schema(conf)
return core.schema.check(schema, conf)
end


function _M.access(conf, ctx)
ctx.dubbo_proxy_enabled = true

ngx_var.dubbo_service_name = conf.service_name
ngx_var.dubbo_service_version = conf.service_version
ngx_var.dubbo_method = conf.method
end


return _M
3 changes: 3 additions & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ plugins: # plugin list (sorted in alphabetical order)
- batch-requests
- consumer-restriction
- cors
# - dubbo-proxy
- echo
# - error-log-logger
# - example-plugin
Expand Down Expand Up @@ -270,3 +271,5 @@ plugin_attr:
server-info:
report_interval: 60 # server info report interval (unit: second)
report_ttl: 3600 # live time for server info in etcd (unit: second)
dubbo-proxy:
upstream_multiplex_count: 32
1 change: 1 addition & 0 deletions rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ description = {
}

dependencies = {
"lua-resty-ctxdump = 0.1-0",
"lua-resty-template = 1.9",
"lua-resty-etcd = 1.4.3",
"lua-resty-balancer = 0.02rc5",
Expand Down
52 changes: 52 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,50 @@ if ($profile) {
}


my $dubbo_upstream = "";
my $dubbo_location = "";
my $enable_dubbo = $ENV{"ENABLE_DUBBO"};
if ($enable_dubbo) {
$dubbo_upstream = <<_EOC_;
upstream apisix_dubbo_backend {
server 0.0.0.1;
balancer_by_lua_block {
apisix.http_balancer_phase()
}
multi 1;
keepalive 320;
}
_EOC_

$dubbo_location = <<_EOC_;
location \@dubbo_pass {
access_by_lua_block {
apisix.dubbo_access_phase()
}
dubbo_pass_all_headers on;
dubbo_pass_body on;
dubbo_pass \$dubbo_service_name \$dubbo_service_version \$dubbo_method apisix_dubbo_backend;
header_filter_by_lua_block {
apisix.http_header_filter_phase()
}
body_filter_by_lua_block {
apisix.http_body_filter_phase()
}
log_by_lua_block {
apisix.http_log_phase()
}
}
_EOC_
}


add_block_preprocessor(sub {
my ($block) = @_;
my $wait_etcd_sync = $block->wait_etcd_sync // 0.1;
Expand Down Expand Up @@ -269,6 +313,8 @@ _EOC_
keepalive 32;
}
$dubbo_upstream
init_by_lua_block {
$init_by_lua_block
}
Expand Down Expand Up @@ -362,6 +408,10 @@ _EOC_
set \$upstream_scheme 'http';
set \$upstream_host \$host;
set \$upstream_uri '';
set \$ctx_ref '';
set \$dubbo_service_name '';
set \$dubbo_service_version '';
set \$dubbo_method '';
location = /apisix/nginx_status {
allow 127.0.0.0/24;
Expand Down Expand Up @@ -459,6 +509,8 @@ _EOC_
}
}
$dubbo_location
location = /proxy_mirror {
internal;
Expand Down
Loading

0 comments on commit 1d991cb

Please sign in to comment.