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

xiaoyu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu-nginx.git


The following commit(s) were added to refs/heads/main by this push:
     new 740d4a6  [ISSUE #15] add eureka sync (#16)
740d4a6 is described below

commit 740d4a62806763700deece6fcb9182ae01b9f12f
Author: Sinsy <[email protected]>
AuthorDate: Mon Jul 4 18:45:55 2022 +0800

    [ISSUE #15] add eureka sync (#16)
    
    * add eureka sync
    
    * add nginx.conf
---
 example/eureka/nginx.conf      | 49 ++++++++++++++++++++++++++++
 lib/shenyu/register/eureka.lua | 73 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/example/eureka/nginx.conf b/example/eureka/nginx.conf
new file mode 100644
index 0000000..9004569
--- /dev/null
+++ b/example/eureka/nginx.conf
@@ -0,0 +1,49 @@
+lua_package_path "/usr/local/openresty/lualib/?.lua;;";
+lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
+
+lua_shared_dict upstream_list 10m;
+
+# 第一次初始化
+init_by_lua_block {
+    local eureka = require "eureka";
+    eureka.init({
+        upstream_list = ngx.shared.upstream_list,
+        base_url = "http://192.168.9.252:8761";,
+        path = "/eureka/apps/demo",
+
+    });
+}
+
+# 定时拉取配置
+init_worker_by_lua_block {
+    local eureka = require "eureka";
+    local handle = nil;
+
+    handle = function ()
+        --TODO:控制每次只有一个worker执行
+        eureka.get_server_list();
+        ngx.timer.at(5, handle);
+    end
+    ngx.timer.at(5, handle);
+}
+
+upstream api_server {
+    server 0.0.0.1 down; #占位server
+
+    balancer_by_lua_block {
+        local balancer = require "ngx.balancer";
+        local eureka = require "eureka";
+        local tmp_upstreams = eureka.get_upstreams();
+        local ip_port = tmp_upstreams[math.random(1, 
table.getn(tmp_upstreams))];
+        balancer.set_current_peer(ip_port.ip, ip_port.port);
+    }
+}
+
+server {
+    listen       12000;
+    server_name  localhost;
+    charset utf-8;
+    location / {
+         proxy_pass http://api_server;
+    }
+}
\ No newline at end of file
diff --git a/lib/shenyu/register/eureka.lua b/lib/shenyu/register/eureka.lua
new file mode 100644
index 0000000..bcec77b
--- /dev/null
+++ b/lib/shenyu/register/eureka.lua
@@ -0,0 +1,73 @@
+local _M = {}
+
+local http = require("resty.http")
+local json = require("cjson")
+
+
+local ngx = ngx
+
+local ngx_timer_at = ngx.timer.at
+local ngx_worker_exiting = ngx.worker.exiting
+
+
+local log = ngx.log
+local ERR = ngx.ERR
+local INFO = ngx.INFO
+
+
+function  _M:get_server_list()
+
+    local httpc = http:new()
+
+    local res, err = httpc:request_uri(_M.base_url, {
+        method = "GET",
+        path = _M.path,
+        headers = {["Accept"]="application/json"},
+    })
+    if not res then
+        log(ERR, "failed to get server list from eureka. ", err)
+    end
+
+    local service = {}
+
+    if res.status == 200 then
+
+        local list_inst_resp = json.decode(res.body)
+        local application = list_inst_resp.application
+        for k, v in ipairs(application) do
+
+            local instances = v["instance"]
+
+            for i, instance in pairs(instances) do
+                local status = instance["status"]
+                if status == "UP" then
+                    local ipAddr = instance["ipAddr"]
+                    local port = instance["port"]["$"]
+                    log(INFO, "ipAddr: ", ipAddr)
+                    log(INFO, "port: ", port)
+                    service[i] = {ip=ipAddr, port=port}
+                end
+            end
+        end
+    end
+
+    _M.storage:set("demo", json.encode(service))
+
+
+end
+
+
+function _M:get_upstreams()
+    local upstreams_str = _M.storage:get("demo");
+    local tmp_upstreams = json.decode(upstreams_str);
+    return tmp_upstreams;
+end
+
+
+
+function _M.init(conf)
+    _M.storage = conf.upstream_list
+    _M.base_url = conf.base_url
+    _M.path = conf.path
+
+end
\ No newline at end of file

Reply via email to