[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-19 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r615685716



##
File path: apisix/discovery/nacos.lua
##
@@ -275,41 +273,33 @@ end
 
 
 function _M.nodes(service_name)
-local logged = false

Review comment:
   The lazy init is important as the sync is slow.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-19 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r615681419



##
File path: apisix/discovery/nacos.lua
##
@@ -182,47 +179,47 @@ local function get_base_uri()
 end
 
 
-local function get_page_service(infos, base_uri, token_param, page_num)
---  TODO Hardcode:page size=100,will rewrite by spacewander after merge
-local path = str_format(service_list_path, page_num, 100) .. token_param
-local data, err = get_url(base_uri, path)
-if err then
-return data, err, path
-end
-
-for _, service_name in ipairs(data.doms) do
-core.table.insert(infos, service_name)
-end
-return data, err, path
-end
-
-
-local function iter_and_add_service_info(infos, base_uri, token_param)
-local data, err, path = get_page_service(infos, base_uri, token_param, 1)
-if err then
-log.error("get_url:", path, " err:", err)
+local function iter_and_add_service(services, values)
+   if not values then
 return
 end
 
---  TODO Hardcode:page size=100,will rewrite by spacewander after merge
-local maxPage = math.ceil(data.count / 100)
-if maxPage == 0 then
-return
-end
+for _, value in core.config_util.iterate_values(values) do
+local conf = value.value
+if not conf then
+goto CONTINUE
+end
 
--- more than 1 page,continue fetch other pages
-if maxPage > 1 then
-for i = 2, maxPage do
-get_page_service(infos, base_uri, token_param, i)
+local up
+if conf.upstream then
+up = conf.upstream
+else
+up = conf
 end
+
+if up.discovery_type == "nacos" then
+core.table.insert(services, up.service_name)
+end
+::CONTINUE::
 end
 end
 
 
-local function get_services(base_uri, token_param)
-local infos = core.table.new(0, 0)
-iter_and_add_service_info(infos, base_uri, token_param)
-return infos
+local function get_nacos_services()
+local services = {}
+
+-- here we use lazy load to work around circle dependency
+local get_upstreams = require("apisix.upstream").upstreams
+local get_routes = require("apisix.router").http_routes
+local get_services = require("apisix.http.service").services
+
+local values = get_upstreams()
+iter_and_add_service(services, values)
+values = get_routes()
+iter_and_add_service(services, values)

Review comment:
   Those can have embedded upstream field.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-19 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r615639810



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,306 @@
+--
+-- 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 require= require
+local local_conf = require('apisix.core.config_local').local_conf()
+local http   = require('resty.http')
+local core   = require('apisix.core')
+local ipairs = ipairs
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require('ngx.re')
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local log= core.log
+
+local default_weight
+local applications = {}
+local auth_path = 'auth/login'
+local instance_list_path = 'ns/instance/list?healthyOnly=true='
+
+local host_pattern = [[^http(s)?:\/\/[a-zA-Z0-9-_.:\@]+$]]
+local prefix_pattern = [[^[\/a-zA-Z0-9-_.]+$]]
+local schema = {
+type = 'object',
+properties = {
+host = {
+type = 'array',
+minItems = 1,
+items = {
+type = 'string',
+pattern = host_pattern,
+minLength = 2,
+maxLength = 100,
+},
+},
+fetch_interval = {type = 'integer', minimum = 1, default = 30},
+prefix = {type = 'string', pattern = prefix_pattern, maxLength = 100, 
default = '/nacos/v1/'},
+weight = {type = 'integer', minimum = 1, default = 100},
+timeout = {
+type = 'object',
+properties = {
+connect = {type = 'integer', minimum = 1, default = 2000},
+send = {type = 'integer', minimum = 1, default = 2000},
+read = {type = 'integer', minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {'host'}
+}
+
+
+local _M = {}
+
+
+local function request(request_uri, path, body, method, basic_auth)
+local url = request_uri .. path
+log.info('request url:', url)
+local headers = {}
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout.connect
+local send_timeout = timeout.send
+local read_timeout = timeout.read
+log.info('connect_timeout:', connect_timeout, ', send_timeout:', 
send_timeout,
+ ', read_timeout:', read_timeout)
+httpc:set_timeouts(connect_timeout, send_timeout, read_timeout)
+local res, err = httpc:request_uri(url, {
+method = method,
+headers = headers,
+body = body,
+ssl_verify = true,
+})
+if not res then
+return nil, err
+end
+
+if not res.body or res.status ~= 200 then
+return nil, 'status = ' .. res.status
+end
+
+local json_str = res.body
+local data, err = core.json.decode(json_str)
+if not data then
+return nil, err
+end
+return data
+end
+
+
+local function get_url(request_uri, path)
+return request(request_uri, path, nil, 'GET', nil)
+end
+
+
+local function post_url(request_uri, path, body)
+return request(request_uri, path, body, 'POST', nil)
+end
+
+
+local function get_token_param(base_uri, username, password)
+if not username or not password then
+return ''
+end
+
+local args = { username = username, password = password}
+

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-08 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609596820



##
File path: .github/workflows/centos7-ci.yml
##
@@ -101,6 +101,37 @@ jobs:
 docker run --rm --name consul_1 -d -p 8500:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 docker run --rm --name consul_2 -d -p 8600:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 
+# start nacos server
+nohup docker network rm nacos_net > /dev/null 2>&1 &
+nohup docker network create nacos_net > /dev/null 2>&1 &
+# nacos no auth server - for test no auth
+docker run --rm -d --name nacos_no_auth --network nacos_net --hostname 
nacos2 --env NACOS_SERVERS="nacos1:8848 nacos2:8848" --env 
PREFER_HOST_MODE=hostname --env MODE=cluster --env EMBEDDED_STORAGE=embedded  
--env JVM_XMS=512m --env JVM_XMX=512m --env JVM_XMN=256m -p8858:8848 
nacos/nacos-server:latest

Review comment:
   @benx203
   Why can't we support 2.0.0 directly? Is `2.0.0` incompatible with `1.4.1`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-08 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609596820



##
File path: .github/workflows/centos7-ci.yml
##
@@ -101,6 +101,37 @@ jobs:
 docker run --rm --name consul_1 -d -p 8500:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 docker run --rm --name consul_2 -d -p 8600:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 
+# start nacos server
+nohup docker network rm nacos_net > /dev/null 2>&1 &
+nohup docker network create nacos_net > /dev/null 2>&1 &
+# nacos no auth server - for test no auth
+docker run --rm -d --name nacos_no_auth --network nacos_net --hostname 
nacos2 --env NACOS_SERVERS="nacos1:8848 nacos2:8848" --env 
PREFER_HOST_MODE=hostname --env MODE=cluster --env EMBEDDED_STORAGE=embedded  
--env JVM_XMS=512m --env JVM_XMX=512m --env JVM_XMN=256m -p8858:8848 
nacos/nacos-server:latest

Review comment:
   @benx203
   Why we can't support 2.0.0 directly? Is `2.0.0` incompatible with `1.4.1`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-08 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609595479



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,322 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path = "auth/login"
+local service_list_path = "ns/service/list?pageNo=%s=%s"
+local instance_list_path = "ns/instance/list?healthyOnly=true="
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function request(request_uri, path, body, method, basic_auth)
+local url = request_uri .. path
+log.info("request url:", url)
+local headers = core.table.new(0, 0)
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout.connect
+local send_timeout = timeout.send
+local read_timeout = timeout.read
+log.info("connect_timeout:", connect_timeout, ", send_timeout:", 
send_timeout,
+ ", read_timeout:", read_timeout)
+httpc:set_timeouts(connect_timeout, send_timeout, read_timeout)
+local res, err = httpc:request_uri(url, {
+method = method,
+headers = headers,
+body = body,
+ssl_verify = true,
+})
+if not res then
+return nil, err
+end
+
+if not res.body or res.status ~= 200 then
+return nil, "status = " .. res.status
+end
+
+local json_str = res.body
+local data, err = core.json.decode(json_str)
+if not data then
+return nil, err
+end
+return data
+end
+
+
+local function get_url(request_uri, path)
+return request(request_uri, path, nil, "GET", nil)
+end
+
+
+local function post_url(request_uri, path, body)
+return request(request_uri, path, body, "POST", nil)
+end
+
+
+local function get_token_param(base_uri, username, password)
+if username and password then
+local data, err = post_url(base_uri, auth_path .. "?username=" .. 
username
+   .. "=" .. password, nil)
+if err then
+log.error("nacos login fail:", username, " ", 

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-08 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609413722



##
File path: .github/workflows/centos7-ci.yml
##
@@ -101,6 +101,37 @@ jobs:
 docker run --rm --name consul_1 -d -p 8500:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 docker run --rm --name consul_2 -d -p 8600:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 
+# start nacos server
+nohup docker network rm nacos_net > /dev/null 2>&1 &
+nohup docker network create nacos_net > /dev/null 2>&1 &
+# nacos no auth server - for test no auth
+docker run --rm -d --name nacos_no_auth --network nacos_net --hostname 
nacos2 --env NACOS_SERVERS="nacos1:8848 nacos2:8848" --env 
PREFER_HOST_MODE=hostname --env MODE=cluster --env EMBEDDED_STORAGE=embedded  
--env JVM_XMS=512m --env JVM_XMX=512m --env JVM_XMN=256m -p8858:8848 
nacos/nacos-server:latest
+# nacos auth server - for test auth
+docker run --rm -d --name nacos_auth --network nacos_net --hostname 
nacos1 --env NACOS_AUTH_ENABLE=true --env NACOS_SERVERS="nacos1:8848 
nacos2:8848" --env PREFER_HOST_MODE=hostname --env MODE=cluster --env 
EMBEDDED_STORAGE=embedded  --env JVM_XMS=512m --env JVM_XMX=512m --env 
JVM_XMN=256m -p8848:8848 nacos/nacos-server:latest
+url="127.0.0.1:8858/nacos/v1/ns/service/list?pageNo=1=2"
+until  [[ "$(curl -s -o /dev/null -w ''%{http_code}'' $url)"  == "200" 
]]; do
+  echo 'wait nacos server...'
+  sleep 1;
+done
+# register nacos service
+rm -rf tmp
+mkdir tmp
+cd tmp
+wget 
https://raw.githubusercontent.com/benx203/nacos-test-service/main/spring-nacos-1.0-SNAPSHOT.jar

Review comment:
   @benx203 
   Would you also submit the Java code to 
https://github.com/benx203/nacos-test-service? Thanks!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-08 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609413111



##
File path: .github/workflows/centos7-ci.yml
##
@@ -101,6 +101,37 @@ jobs:
 docker run --rm --name consul_1 -d -p 8500:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 docker run --rm --name consul_2 -d -p 8600:8500 consul:1.7 consul 
agent -server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 
+# start nacos server
+nohup docker network rm nacos_net > /dev/null 2>&1 &
+nohup docker network create nacos_net > /dev/null 2>&1 &
+# nacos no auth server - for test no auth
+docker run --rm -d --name nacos_no_auth --network nacos_net --hostname 
nacos2 --env NACOS_SERVERS="nacos1:8848 nacos2:8848" --env 
PREFER_HOST_MODE=hostname --env MODE=cluster --env EMBEDDED_STORAGE=embedded  
--env JVM_XMS=512m --env JVM_XMX=512m --env JVM_XMN=256m -p8858:8848 
nacos/nacos-server:latest

Review comment:
   Would it be better to use `nacos/nacos-server:2.0.0`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-08 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609411973



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,322 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path = "auth/login"
+local service_list_path = "ns/service/list?pageNo=%s=%s"
+local instance_list_path = "ns/instance/list?healthyOnly=true="
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function request(request_uri, path, body, method, basic_auth)
+local url = request_uri .. path
+log.info("request url:", url)
+local headers = core.table.new(0, 0)
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout.connect
+local send_timeout = timeout.send
+local read_timeout = timeout.read
+log.info("connect_timeout:", connect_timeout, ", send_timeout:", 
send_timeout,
+ ", read_timeout:", read_timeout)
+httpc:set_timeouts(connect_timeout, send_timeout, read_timeout)
+local res, err = httpc:request_uri(url, {
+method = method,
+headers = headers,
+body = body,
+ssl_verify = true,
+})
+if not res then
+return nil, err
+end
+
+if not res.body or res.status ~= 200 then
+return nil, "status = " .. res.status
+end
+
+local json_str = res.body
+local data, err = core.json.decode(json_str)
+if not data then
+return nil, err
+end
+return data
+end
+
+
+local function get_url(request_uri, path)
+return request(request_uri, path, nil, "GET", nil)
+end
+
+
+local function post_url(request_uri, path, body)
+return request(request_uri, path, body, "POST", nil)
+end
+
+
+local function get_token_param(base_uri, username, password)
+if username and password then
+local data, err = post_url(base_uri, auth_path .. "?username=" .. 
username
+   .. "=" .. password, nil)
+if err then
+log.error("nacos login fail:", username, " ", 

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-07 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609308381



##
File path: t/discovery/nacos.t
##
@@ -0,0 +1,152 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('info');
+no_root_location();
+no_shuffle();
+

Review comment:
   We can add this block to check the error log:
   ```
   add_block_preprocessor(sub {
   my ($block) = @_;
   
   if (!$block->no_error_log) {
   $block->set_value("no_error_log", "[error]\n[alert]");
   }
   });
   ```
   
   It would be helpful for debugging the 503.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-07 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r609287508



##
File path: t/discovery/nacos.t
##
@@ -0,0 +1,152 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('info');
+no_root_location();
+no_shuffle();
+
+our $yaml_config = <<_EOC_;
+apisix:
+  node_listen: 1984
+  config_center: yaml
+  enable_admin: false
+discovery:
+  nacos:
+  host:
+- "http://127.0.0.1:8858;
+  prefix: "/nacos/v1/"
+  fetch_interval: 30
+  weight: 100
+  timeout:
+connect: 2000
+send: 2000
+read: 5000
+
+_EOC_
+
+our $yaml_auth_config = <<_EOC_;
+apisix:
+  node_listen: 1984
+  config_center: yaml
+  enable_admin: false
+discovery:
+  nacos:
+  host:
+- "http://nacos:nacos\@127.0.0.1:8848;
+  prefix: "/nacos/v1/"
+  fetch_interval: 30
+  weight: 100
+  timeout:
+connect: 2000
+send: 2000
+read: 5000
+
+_EOC_
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: get APISIX-NACOS info from NACOS - no auth
+--- yaml_config eval: $::yaml_config
+--- apisix_yaml
+routes:
+  -
+uri: /hello
+upstream:
+  service_name: APISIX-NACOS
+  discovery_type: nacos
+  type: roundrobin
+
+#END
+--- pipelined_requests eval

Review comment:
   Maybe we can use sleep before sending HTTP requests?
   Like this: 
https://github.com/apache/apisix/blob/252af4143d527b5a644c0a4959b96504099237e2/t/discovery/consul_kv.t#L307
   Don't forget to set a long "--- timeout".
   
   It look like syncing configuration from Nacos is time-consuming job.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-01 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r606045390



##
File path: apisix/discovery/nacos.lua
##
@@ -278,9 +278,13 @@ end
 
 
 function _M.nodes(service_name)
-while( not applications )
+local loged = false

Review comment:
   Should be `logged`

##
File path: apisix/discovery/nacos.lua
##
@@ -278,9 +278,13 @@ end
 
 
 function _M.nodes(service_name)
-while( not applications )
+local loged = false
+while(not applications)

Review comment:
   Style: better to write `while not applications do`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-01 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r606041056



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,316 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path = "auth/login"
+local service_list_path = "ns/service/list?pageNo=%s=%s"
+local instance_list_path = "ns/instance/list?healthyOnly=true="
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function request(request_uri, path, body, method, basic_auth)
+local url = request_uri .. path
+log.info("request url:", url)
+local headers = core.table.new(0, 0)
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+-- log.warn(method, url, body)
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout.connect
+local send_timeout = timeout.send
+local read_timeout = timeout.read
+log.info("connect_timeout:", connect_timeout, ", send_timeout:", 
send_timeout,
+ ", read_timeout:", read_timeout, ".")
+httpc:set_timeouts(connect_timeout, send_timeout, read_timeout)
+local res, err = httpc:request_uri(url, {
+method = method,
+headers = headers,
+body = body,
+ssl_verify = true,
+})
+if not res then
+return nil, err
+end
+
+if not res.body or res.status ~= 200 then
+return nil, "status = " .. res.status
+end
+
+local json_str = res.body
+local data, err = core.json.decode(json_str)
+if not data then
+return nil, err
+end
+return data
+end
+
+
+local function get_url(request_uri, path)
+return request(request_uri, path, nil, "GET", nil)
+end
+
+
+local function post_url(request_uri, path, body)
+return request(request_uri, path, body, "POST", nil)
+end
+
+
+local function get_token_param(base_uri, username, password)
+if username and password then
+local data, err = post_url(base_uri, auth_path .. "?username=" .. 
username
+   .. "=" .. password, nil)
+if err then
+

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-01 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r605524014



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,316 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications

Review comment:
   We can initialize `applications = {}` so that we can save many codes 
like:
   
   ```
   if not applications then
   applications = up_apps
   end
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-04-01 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r605413510



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,326 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local base_uri
+local token_param
+local page_size
+local token_ttl = 18000
+local auth_path = "auth/login"
+local service_list_path = "ns/service/list?pageNo=%s=%s"
+local instance_list_path = "ns/instance/list?healthyOnly=true="
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+page_size = {type = "integer", minimum = 1, default = 100},

Review comment:
   We can remove the page_size now and hardcore a very big number. So I 
don't need to change too many things when I edit this PR.

##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,326 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local base_uri
+local token_param
+local page_size
+local token_ttl = 18000
+local auth_path = "auth/login"
+local service_list_path = "ns/service/list?pageNo=%s=%s"
+local instance_list_path = "ns/instance/list?healthyOnly=true="
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+  

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-30 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r604533670



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,324 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local page_size
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+page_size = {type = "integer", minimum = 1, default = 100},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local username, password
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "#")
+if auth_idx then
+local protocol_idx = str_find(url, "://")
+local protocol = string_sub(url, 1, protocol_idx + 2)
+local user_and_password = string_sub(url, protocol_idx + 3, auth_idx - 
1)
+local arr = ngx_re.split(user_and_password, ":")
+if #arr == 2 then
+username = arr[1]
+password = arr[2]
+end
+local other = string_sub(url, auth_idx + 1)
+url = protocol .. other
+end
+if local_conf.discovery.nacos.prefix then
+url = url .. local_conf.discovery.nacos.prefix
+end
+if str_byte(url, #url) ~= str_byte("/") then
+url = url .. "/"
+end
+
+return url, username, password
+end
+
+
+local function request(request_uri, path, body, method, basic_auth)
+local url = request_uri .. path
+log.info("request url:", url)
+local headers = core.table.new(0, 0)
+headers['Connection'] = 'Keep-Alive'
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+-- log.warn(method, url, body)
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout.connect
+local send_timeout = timeout.send
+local read_timeout = 

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-30 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r603879135



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,324 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local page_size
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+page_size = {type = "integer", minimum = 1, default = 100},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local username, password
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "#")
+if auth_idx then
+local protocol_idx = str_find(url, "://")
+local protocol = string_sub(url, 1, protocol_idx + 2)
+local user_and_password = string_sub(url, protocol_idx + 3, auth_idx - 
1)
+local arr = ngx_re.split(user_and_password, ":")
+if #arr == 2 then
+username = arr[1]
+password = arr[2]
+end
+local other = string_sub(url, auth_idx + 1)
+url = protocol .. other
+end
+if local_conf.discovery.nacos.prefix then
+url = url .. local_conf.discovery.nacos.prefix
+end
+if str_byte(url, #url) ~= str_byte("/") then
+url = url .. "/"
+end
+
+return url, username, password
+end
+
+
+local function request(request_uri, path, body, method, basic_auth)
+local url = request_uri .. path
+log.info("request url:", url)
+local headers = core.table.new(0, 0)
+headers['Connection'] = 'Keep-Alive'
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+-- log.warn(method, url, body)
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout.connect
+local send_timeout = timeout.send
+local read_timeout = 

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-29 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r603719667



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,324 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local page_size
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+page_size = {type = "integer", minimum = 1, default = 100},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local username, password
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "#")

Review comment:
   You need to escape it as `\@` in the template string. The `@` has a 
special meaning in Perl.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-29 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r603719667



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,324 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local page_size
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+page_size = {type = "integer", minimum = 1, default = 100},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local username, password
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "#")

Review comment:
   You need to escape it as `\@` in the template string.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-29 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r603173738



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,324 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local page_size
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+page_size = {type = "integer", minimum = 1, default = 100},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local username, password
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "#")
+if auth_idx then
+local protocol_idx = str_find(url, "://")
+local protocol = string_sub(url, 1, protocol_idx + 2)
+local user_and_password = string_sub(url, protocol_idx + 3, auth_idx - 
1)
+local arr = ngx_re.split(user_and_password, ":")
+if #arr == 2 then
+username = arr[1]
+password = arr[2]
+end
+local other = string_sub(url, auth_idx + 1)
+url = protocol .. other
+end
+if local_conf.discovery.nacos.prefix then
+url = url .. local_conf.discovery.nacos.prefix
+end
+if str_byte(url, #url) ~= str_byte("/") then
+url = url .. "/"
+end
+
+return url, username, password
+end
+
+
+local function request(request_uri, path, body, method, basic_auth)
+local url = request_uri .. path
+log.info("request url:", url)
+local headers = core.table.new(0, 0)
+headers['Connection'] = 'Keep-Alive'
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+-- log.warn(method, url, body)
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout.connect
+local send_timeout = timeout.send
+local read_timeout = 

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-29 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r603167933



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,324 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = require("ngx.re")
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local str_format = string.format
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local page_size
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+page_size = {type = "integer", minimum = 1, default = 100},
+weight = {type = "integer", minimum = 1, default = 100},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+},
+default = {
+connect = 2000,
+send = 2000,
+read = 5000,
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local username, password
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "#")

Review comment:
   Is there any need to change `@` to `#`? Maybe I miss something?
   It is the standard to use `@` to separate the authority part from the rest:
   https://en.wikipedia.org/wiki/URL#Syntax

##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,324 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math   = math
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_re = 

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-26 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r602672250



##
File path: .travis/linux_tengine_runner.sh
##
@@ -40,6 +40,9 @@ before_install() {
 # start consul servers
 docker run --rm --name consul_1 -d -p 8500:8500 consul:1.7 consul agent 
-server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 docker run --rm --name consul_2 -d -p 8600:8500 consul:1.7 consul agent 
-server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
+
+# start nacos server
+docker run --rm --name nacos_1 -d --env PREFER_HOST_MODE=hostname --env 
MODE=standalone --env JVM_XMS=512m --env JVM_XMX=512m --env JVM_XMN=256m 
-p8848:8848 nacos/nacos-server:latest

Review comment:
   You can export the auth function: 
https://github.com/apache/apisix/pull/3820/files#diff-46fd008790f9ea29612a92ca5c94212ae43c81ab6b902ef3014d3d6b4bdfc5b3R202
   and call it in the test file.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-26 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r602663539



##
File path: .travis/linux_tengine_runner.sh
##
@@ -40,6 +40,9 @@ before_install() {
 # start consul servers
 docker run --rm --name consul_1 -d -p 8500:8500 consul:1.7 consul agent 
-server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 docker run --rm --name consul_2 -d -p 8600:8500 consul:1.7 consul agent 
-server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
+
+# start nacos server
+docker run --rm --name nacos_1 -d --env PREFER_HOST_MODE=hostname --env 
MODE=standalone --env JVM_XMS=512m --env JVM_XMX=512m --env JVM_XMN=256m 
-p8848:8848 nacos/nacos-server:latest

Review comment:
   If the auth enable nacos can be used, the auth is successful.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-26 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r602173854



##
File path: .travis/linux_tengine_runner.sh
##
@@ -40,6 +40,9 @@ before_install() {
 # start consul servers
 docker run --rm --name consul_1 -d -p 8500:8500 consul:1.7 consul agent 
-server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
 docker run --rm --name consul_2 -d -p 8600:8500 consul:1.7 consul agent 
-server -bootstrap-expect=1 -client 0.0.0.0 -log-level info 
-data-dir=/consul/data
+
+# start nacos server
+docker run --rm --name nacos_1 -d --env PREFER_HOST_MODE=hostname --env 
MODE=standalone --env JVM_XMS=512m --env JVM_XMX=512m --env JVM_XMN=256m 
-p8848:8848 nacos/nacos-server:latest

Review comment:
   You can use different conf in different tests, one for auth success and 
another for auth fail.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-23 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r600111763



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,285 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+auth_path = {type = "string", default = "auth/login"},
+service_list_path = {type = "string", default = 
"ns/service/list?pageNo=1=20"},
+instance_list_path = {type = "string", default = 
"ns/instance/list?serviceName="},

Review comment:
   @benx203
   I think we can remove it now and add it if needed.
   We need to add a doc and test for this feature if you insist to keep it.
   (I don't recommend to do that as this PR is big enough)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-23 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r600110250



##
File path: t/discovery/nacos.t
##
@@ -0,0 +1,125 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('info');
+no_root_location();
+no_shuffle();
+
+add_block_preprocessor(sub {
+my ($block) = @_;
+
+my $http_config = $block->http_config // <<_EOC_;
+
+server {
+listen 18001;
+
+location /hello {
+content_by_lua_block {
+ngx.say("server 1")
+}
+}
+}
+server {
+listen 18002;
+
+location /hello {
+content_by_lua_block {
+ngx.say("server 2")
+}
+}
+}
+_EOC_
+
+$block->set_value("http_config", $http_config);
+});
+
+our $yaml_config = <<_EOC_;
+apisix:
+  node_listen: 1984
+  config_center: yaml
+  enable_admin: false
+discovery:
+  nacos:
+  host:
+- "http://127.0.0.1:8848;
+  prefix: "/nacos/v1/"
+  fetch_interval: 30
+  weight: 100
+  timeout:
+connect: 2000
+send: 2000
+read: 5000
+
+_EOC_
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: prepare nacos register nodes
+--- config
+location /nacos {
+proxy_pass http://127.0.0.1:8848;
+}
+
+--- pipelined_requests eval
+[
+"POST 
/nacos/v1/ns/instance?port=18001=true=127.0.0.1=1.0=APISIX-NACOS=GBK=true",
+"DELETE /nacos/v1/ns/service?serviceName=APISIX-NACOS",

Review comment:
   But it is not the feature of APISIX. We don't need to test nacos itself.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-23 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r599433211



##
File path: docs/en/latest/discovery/nacos.md
##
@@ -0,0 +1,95 @@
+---
+title: nacos
+---
+
+
+
+### Configuration for Nacos
+
+Add following configuration in `conf/config.yaml` :
+
+```yaml
+discovery:
+  nacos:
+host:
+  - "http://${username}:${password}@${host1}:${port1};
+prefix: "/nacos/v1/"
+auth_path: "auth/login"
+service_list_path: "ns/service/list?pageNo=1=20"
+instance_list_path: "ns/instance/list?serviceName="
+fetch_interval: 30# default 30 sec
+weight: 100   # default 100
+timeout:
+  connect: 2000   # default 2000

Review comment:
   Better to mention the unit is ms.

##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,285 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string = string
+local table  = table
+local string_sub = string.sub
+local str_byte   = string.byte
+local str_find   = core.string.find
+local log= core.log
+
+local default_weight
+local applications
+local auth_path
+local service_list_path
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string", default = "/nacos/v1/"},
+auth_path = {type = "string", default = "auth/login"},
+service_list_path = {type = "string", default = 
"ns/service/list?pageNo=1=20"},
+instance_list_path = {type = "string", default = 
"ns/instance/list?serviceName="},

Review comment:
   Need a test to check the user-defined paths.
   BTW, in what situation people need to redefine the paths?

##
File path: t/discovery/nacos.t
##
@@ -0,0 +1,125 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('info');
+no_root_location();
+no_shuffle();
+
+add_block_preprocessor(sub {
+my ($block) = @_;
+
+my $http_config = $block->http_config // <<_EOC_;
+
+server {
+listen 18001;
+
+location /hello {
+content_by_lua_block {
+ngx.say("server 1")
+}
+}
+}
+server {
+listen 18002;
+
+location /hello {
+content_by_lua_block {
+ngx.say("server 2")
+}
+}
+}
+_EOC_
+
+$block->set_value("http_config", $http_config);
+});
+
+our $yaml_config = <<_EOC_;
+apisix:
+  node_listen: 1984
+  config_center: yaml
+  enable_admin: false
+discovery:
+  nacos:
+  host:
+- "http://127.0.0.1:8848;
+  prefix: "/nacos/v1/"
+  fetch_interval: 30
+  weight: 100
+

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-18 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r597362835



##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,237 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string_sub = string.sub
+local str_find   = core.string.find
+local log= core.log
+
+local default_weight
+local applications
+local service_list_path
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string"},
+weight = {type = "integer", minimum = 0},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local basic_auth
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "@")
+if auth_idx then
+local protocol_idx = str_find(url, "://")
+local protocol = string_sub(url, 1, protocol_idx + 2)
+local user_and_password = string_sub(url, protocol_idx + 3, auth_idx - 
1)
+local other = string_sub(url, auth_idx + 1)
+url = protocol .. other
+basic_auth = "Basic " .. ngx.encode_base64(user_and_password)
+end
+if local_conf.discovery.nacos.prefix then
+url = url .. local_conf.discovery.nacos.prefix
+end
+if string_sub(url, #url) ~= "/" then
+url = url .. "/"
+end
+
+return url, basic_auth
+end
+
+
+local function request(request_uri, basic_auth, method, path, query, body)
+log.info("nacos uri:", request_uri, ".")
+local url = request_uri .. path
+local headers = core.table.new(0, 5)
+headers['Connection'] = 'Keep-Alive'
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+-- log.warn(method, url, body)
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout and timeout.connect or 2000
+local send_timeout = timeout and timeout.send or 2000
+local read_timeout = timeout and timeout.read or 5000
+log.info("connect_timeout:", connect_timeout, ", send_timeout:", 
send_timeout,
+", read_timeout:", read_timeout, ".")
+httpc:set_timeouts(connect_timeout, send_timeout, read_timeout)
+return httpc:request_uri(url, {
+version = 1.1,
+method = method,
+headers = headers,
+query = query,
+body = body,
+ssl_verify = false,
+})
+end
+
+
+local function get_url(request_url,basic_auth,path)
+local res, err = request(request_url, basic_auth, "GET", path)
+

[GitHub] [apisix] spacewander commented on a change in pull request #3820: feat: add nacos support

2021-03-18 Thread GitBox


spacewander commented on a change in pull request #3820:
URL: https://github.com/apache/apisix/pull/3820#discussion_r596598466



##
File path: docs/en/latest/discovery/nacos.md
##
@@ -0,0 +1,93 @@
+---
+title: nacos
+---
+
+
+
+### Configuration for Nacos
+
+Add following configuration in `conf/config.yaml` :
+
+```yaml
+discovery:
+  nacos:
+host:
+  - "http://192.168.33.1:8848;

Review comment:
   Why don't mention the user / password feature?

##
File path: apisix/discovery/nacos.lua
##
@@ -0,0 +1,237 @@
+--
+-- 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 local_conf = require("apisix.core.config_local").local_conf()
+local http   = require("resty.http")
+local core   = require("apisix.core")
+local ipairs = ipairs
+local tostring   = tostring
+local type   = type
+local math_random= math.random
+local error  = error
+local ngx= ngx
+local ngx_timer_at   = ngx.timer.at
+local ngx_timer_every= ngx.timer.every
+local string_sub = string.sub
+local str_find   = core.string.find
+local log= core.log
+
+local default_weight
+local applications
+local service_list_path
+local instance_list_path
+
+local schema = {
+type = "object",
+properties = {
+host = {
+type = "array",
+minItems = 1,
+items = {
+type = "string",
+},
+},
+fetch_interval = {type = "integer", minimum = 1, default = 30},
+prefix = {type = "string"},
+weight = {type = "integer", minimum = 0},
+timeout = {
+type = "object",
+properties = {
+connect = {type = "integer", minimum = 1, default = 2000},
+send = {type = "integer", minimum = 1, default = 2000},
+read = {type = "integer", minimum = 1, default = 5000},
+}
+},
+},
+required = {"host"}
+}
+
+
+local _M = {
+version = 0.1,
+}
+
+
+local function service_info()
+local host = local_conf.discovery and
+local_conf.discovery.nacos and local_conf.discovery.nacos.host
+if not host then
+log.error("do not set nacos.host")
+return
+end
+
+local basic_auth
+-- TODO Add health check to get healthy nodes.
+local url = host[math_random(#host)]
+local auth_idx = str_find(url, "@")
+if auth_idx then
+local protocol_idx = str_find(url, "://")
+local protocol = string_sub(url, 1, protocol_idx + 2)
+local user_and_password = string_sub(url, protocol_idx + 3, auth_idx - 
1)
+local other = string_sub(url, auth_idx + 1)
+url = protocol .. other
+basic_auth = "Basic " .. ngx.encode_base64(user_and_password)
+end
+if local_conf.discovery.nacos.prefix then
+url = url .. local_conf.discovery.nacos.prefix
+end
+if string_sub(url, #url) ~= "/" then
+url = url .. "/"
+end
+
+return url, basic_auth
+end
+
+
+local function request(request_uri, basic_auth, method, path, query, body)
+log.info("nacos uri:", request_uri, ".")
+local url = request_uri .. path
+local headers = core.table.new(0, 5)
+headers['Connection'] = 'Keep-Alive'
+headers['Accept'] = 'application/json'
+
+if basic_auth then
+headers['Authorization'] = basic_auth
+end
+
+if body and 'table' == type(body) then
+local err
+body, err = core.json.encode(body)
+if not body then
+return nil, 'invalid body : ' .. err
+end
+-- log.warn(method, url, body)
+headers['Content-Type'] = 'application/json'
+end
+
+local httpc = http.new()
+local timeout = local_conf.discovery.nacos.timeout
+local connect_timeout = timeout and timeout.connect or 2000
+local send_timeout = timeout and timeout.send or 2000
+local read_timeout = timeout and timeout.read or 5000
+log.info("connect_timeout:", connect_timeout, ", send_timeout:", 
send_timeout,
+", read_timeout:", read_timeout, ".")
+httpc:set_timeouts(connect_timeout,