qiujiayu commented on a change in pull request #1281: feature: Support for 
using Eureka as a service discovery center
URL: https://github.com/apache/incubator-apisix/pull/1281#discussion_r397570035
 
 

 ##########
 File path: lua/apisix/discovery/eureka.lua
 ##########
 @@ -0,0 +1,178 @@
+--
+-- 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 ngx_timer_at       = ngx.timer.at
+local ngx_timer_every    = ngx.timer.every
+local string_sub         = string.sub
+local string_find        = string.find
+local log                = core.log
+
+local applications
+local useragent = 'ngx_lua-apisix/v' .. core.version.VERSION
+
+local _M = {
+    version = 1.0,
+}
+
+
+local function service_info()
+    if not local_conf.eureka or not local_conf.eureka.urls then
+        log.error("do not set eureka.urls")
+        return
+    end
+
+    local urls = local_conf.eureka.urls
+    local basic_auth
+    local url = urls[math.random(#urls)]
+    local user_and_password_idx = string_find(url, "@", 1, true)
+    if user_and_password_idx then
+        local protocol_header_idx = string_find(url, "://", 1, true)
+        local protocol_header = string_sub(url, 1, protocol_header_idx + 2)
+        local user_and_password = string_sub(url, protocol_header_idx + 3, 
user_and_password_idx - 1)
+        local other = string_sub(url, user_and_password_idx + 1)
+        url = protocol_header .. other
+        basic_auth = "Basic " .. ngx.encode_base64(user_and_password)
+    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)
+    local url = request_uri .. path
+    local headers = core.table.new(0, 5)
+    headers['User-Agent'] = useragent
+    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 connect_timeout = 2000
 
 Review comment:
   done

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to