You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/03/19 00:01:49 UTC

[incubator-apisix] branch master updated: feature: support multiple etcd address. (#1283)

This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new cee0322  feature: support multiple etcd address. (#1283)
cee0322 is described below

commit cee03225f8ebe213f8b12c7b7c34c07084369a80
Author: Ayeshmantha Perera <ak...@apache.org>
AuthorDate: Thu Mar 19 01:01:41 2020 +0100

    feature: support multiple etcd address. (#1283)
---
 bin/apisix       | 46 +++++++++++++++++++++++++++++++++-------------
 conf/config.yaml |  3 ++-
 2 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/bin/apisix b/bin/apisix
index 2d21aa0..78c2914 100755
--- a/bin/apisix
+++ b/bin/apisix
@@ -706,28 +706,48 @@ local function init_etcd(show_output)
     end
 
     local etcd_conf = yaml_conf.etcd
-    local uri = etcd_conf.host .. "/v2/keys" .. (etcd_conf.prefix or "")
 
     local timeout = etcd_conf.timeout or 3
+    local uri
+    --convert old single etcd config to multiple etcd config
+    if type(yaml_conf.etcd.host) == "string" then
+        yaml_conf.etcd.host = {yaml_conf.etcd.host}
+    end
+
+    local host_count = #(yaml_conf.etcd.host)
+
+    for index, host in ipairs(yaml_conf.etcd.host) do
+
+        local is_success = true
+        uri = host .. "/v2/keys" .. (etcd_conf.prefix or "")
 
-    for _, dir_name in ipairs({"/routes", "/upstreams", "/services",
-                               "/plugins", "/consumers", "/node_status",
-                               "/ssl", "/global_rules", "/stream_routes",
-                               "/proto"}) do
-        local cmd = "curl " .. uri .. dir_name
+        for _, dir_name in ipairs({"/routes", "/upstreams", "/services",
+                                   "/plugins", "/consumers", "/node_status",
+                                   "/ssl", "/global_rules", "/stream_routes",
+                                   "/proto"}) do
+            local cmd = "curl " .. uri .. dir_name
                     .. "?prev_exist=false -X PUT -d dir=true "
                     .. "--connect-timeout " .. timeout
                     .. " --max-time " .. timeout * 2 .. " --retry 1 2>&1"
 
-        local res = exec(cmd)
-        if not res:find("index", 1, true)
-           and not res:find("createdIndex", 1, true) then
-            error(cmd .. "\n" .. res)
+            local res = exec(cmd)
+            if not res:find("index", 1, true)
+                    and not res:find("createdIndex", 1, true) then
+                is_success = false
+                if (index == hostCount) then
+                    error(cmd .. "\n" .. res)
+                end
+                break
+            end
+
+            if show_output then
+                print(cmd)
+                print(res)
+            end
         end
 
-        if show_output then
-            print(cmd)
-            print(res)
+        if is_success then
+            break
         end
     end
 end
diff --git a/conf/config.yaml b/conf/config.yaml
index 4bb942d..e050092 100644
--- a/conf/config.yaml
+++ b/conf/config.yaml
@@ -113,7 +113,8 @@ nginx_config:                     # config for render the template to genarate n
       - 'unix:'
 
 etcd:
-  host: "http://127.0.0.1:2379"   # etcd address
+  host:                           # it's possible to define multiple etcd hosts addresses of the same etcd cluster.
+    - "http://127.0.0.1:2379"     # multiple etcd address
   prefix: "/apisix"               # apisix configurations prefix
   timeout: 3                      # 3 seconds