Skip to content

Commit

Permalink
1. commit zookeeper connection (apache#10)
Browse files Browse the repository at this point in the history
* 1. commit zookeeper connection

* Increase the processing of heartbeats

* 1. Send a request to subscribe

* 1. zookeeper watch event...

* 1. zookeeper watch event...

* 1. update zookeeper proto.

* 1. update zookeeper proto.

* update zookeeper proto.

* update zookeeper proto.

* Modify Chinese

* Modify Chinese

* Modify Chinese
  • Loading branch information
prFor authored Jul 2, 2022
1 parent a8dd80b commit eb1bbc8
Show file tree
Hide file tree
Showing 14 changed files with 1,111 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ logs/
*.diff
*.patch
*.tmp

/out/
/.vscode/
55 changes: 55 additions & 0 deletions example/zookeeper/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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.

worker_processes 2;
daemon off;
error_log /dev/stdout debug;

events {
worker_connections 1024;
}
http {
lua_shared_dict shenyu_storage 1m;

# lua_package_path "$prefix/lib/?.lua;;";

init_worker_by_lua_block {
local register = require("shenyu.register.zookeeper")
register.init({
servers = {"127.0.0.1:2181"},
shenyu_storage = ngx.shared.shenyu_storage,
balancer_type = "chash"
});
}

upstream shenyu {
server 0.0.0.1;
balancer_by_lua_block {
require("shenyu.register.zookeeper").pick_and_set_peer()
}
}

server {
listen 80;

location ~ /* {
proxy_pass http://shenyu;
}
}
}


2 changes: 0 additions & 2 deletions lib/shenyu/register/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

local _M = {}
local str_null = string.char(0)

Expand All @@ -25,7 +24,6 @@ function _M.new(balancer_type)
local servers, nodes = {}, {}
for serv, weight in pairs(server_list) do
local id = string.gsub(serv, ":", str_null)

servers[id] = serv
nodes[id] = weight
end
Expand Down
31 changes: 31 additions & 0 deletions lib/shenyu/register/core/string.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--
-- 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 table_insert = table.insert
local _M ={}


function _M.split(str, delimiter)
if not str or str == "" then return {} end
if not delimiter or delimiter == "" then return { str } end
local result = {}
for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do
table_insert(result, match)
end
return result
end

return _M
209 changes: 209 additions & 0 deletions lib/shenyu/register/core/struct.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
--[[
* Copyright (c) 2015-2020 Iryont <https://github.com/iryont/lua-struct>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
]]
local unpack = table.unpack or _G.unpack
local struct = {}

function struct.pack(format, ...)
local stream = {}
local vars = {...}
local endianness = true

for i = 1, format:len() do
local opt = format:sub(i, i)

if opt == "<" then
endianness = true
elseif opt == ">" then
endianness = false
elseif opt:find("[bBhHiIlL]") then
local n = opt:find("[hH]") and 2 or opt:find("[iI]") and 4 or opt:find("[lL]") and 8 or 1
local val = tonumber(table.remove(vars, 1))

local bytes = {}
for j = 1, n do
table.insert(bytes, string.char(val % (2 ^ 8)))
val = math.floor(val / (2 ^ 8))
end

if not endianness then
table.insert(stream, string.reverse(table.concat(bytes)))
else
table.insert(stream, table.concat(bytes))
end
elseif opt:find("[fd]") then
local val = tonumber(table.remove(vars, 1))
local sign = 0

if val < 0 then
sign = 1
val = -val
end

local mantissa, exponent = math.frexp(val)
if val == 0 then
mantissa = 0
exponent = 0
else
mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, (opt == "d") and 53 or 24)
exponent = exponent + ((opt == "d") and 1022 or 126)
end

local bytes = {}
if opt == "d" then
val = mantissa
for i = 1, 6 do
table.insert(bytes, string.char(math.floor(val) % (2 ^ 8)))
val = math.floor(val / (2 ^ 8))
end
else
table.insert(bytes, string.char(math.floor(mantissa) % (2 ^ 8)))
val = math.floor(mantissa / (2 ^ 8))
table.insert(bytes, string.char(math.floor(val) % (2 ^ 8)))
val = math.floor(val / (2 ^ 8))
end

table.insert(bytes, string.char(math.floor(exponent * ((opt == "d") and 16 or 128) + val) % (2 ^ 8)))
val = math.floor((exponent * ((opt == "d") and 16 or 128) + val) / (2 ^ 8))
table.insert(bytes, string.char(math.floor(sign * 128 + val) % (2 ^ 8)))
val = math.floor((sign * 128 + val) / (2 ^ 8))

if not endianness then
table.insert(stream, string.reverse(table.concat(bytes)))
else
table.insert(stream, table.concat(bytes))
end
elseif opt == "s" then
table.insert(stream, tostring(table.remove(vars, 1)))
table.insert(stream, string.char(0))
elseif opt == "c" then
local n = format:sub(i + 1):match("%d+")
local str = tostring(table.remove(vars, 1))
local len = tonumber(n)
if len <= 0 then
len = str:len()
end
if len - str:len() > 0 then
str = str .. string.rep(" ", len - str:len())
end
table.insert(stream, str:sub(1, len))
i = i + n:len()
end
end

return table.concat(stream)
end

function struct.unpack(format, stream, pos)
local vars = {}
local iterator = pos or 1
local endianness = true

for i = 1, format:len() do
local opt = format:sub(i, i)

if opt == "<" then
endianness = true
elseif opt == ">" then
endianness = false
elseif opt:find("[bBhHiIlL]") then
local n = opt:find("[hH]") and 2 or opt:find("[iI]") and 4 or opt:find("[lL]") and 8 or 1
local signed = opt:lower() == opt

local val = 0
for j = 1, n do
local byte = string.byte(stream:sub(iterator, iterator))
if endianness then
val = val + byte * (2 ^ ((j - 1) * 8))
else
val = val + byte * (2 ^ ((n - j) * 8))
end
iterator = iterator + 1
end

if signed and val >= 2 ^ (n * 8 - 1) then
val = val - 2 ^ (n * 8)
end

table.insert(vars, math.floor(val))
elseif opt:find("[fd]") then
local n = (opt == "d") and 8 or 4
local x = stream:sub(iterator, iterator + n - 1)
iterator = iterator + n

if not endianness then
x = string.reverse(x)
end

local sign = 1
local mantissa = string.byte(x, (opt == "d") and 7 or 3) % ((opt == "d") and 16 or 128)
for i = n - 2, 1, -1 do
mantissa = mantissa * (2 ^ 8) + string.byte(x, i)
end

if string.byte(x, n) > 127 then
sign = -1
end

local exponent =
(string.byte(x, n) % 128) * ((opt == "d") and 16 or 2) +
math.floor(string.byte(x, n - 1) / ((opt == "d") and 16 or 128))
if exponent == 0 then
table.insert(vars, 0.0)
else
mantissa = (math.ldexp(mantissa, (opt == "d") and -52 or -23) + 1) * sign
table.insert(vars, math.ldexp(mantissa, exponent - ((opt == "d") and 1023 or 127)))
end
elseif opt == "s" then
local bytes = {}
for j = iterator, stream:len() do
if stream:sub(j, j) == string.char(0) or stream:sub(j) == "" then
break
end

table.insert(bytes, stream:sub(j, j))
end

local str = table.concat(bytes)
iterator = iterator + str:len() + 1
table.insert(vars, str)
elseif opt == "c" then
local n = format:sub(i + 1):match("%d+")
local len = tonumber(n)
if len <= 0 then
len = table.remove(vars)
end

table.insert(vars, stream:sub(iterator, iterator + len - 1))
iterator = iterator + len
i = i + n:len()
end
end

return vars, iterator
end

function struct.tbunpack(vars)
-- body
return unpack(vars)
end

return struct
39 changes: 39 additions & 0 deletions lib/shenyu/register/core/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--
-- 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 str = require("shenyu.register.core.string")
local _M = {}

--Delimited string
function _M.paras_host(host, delimiter)
return str.split(host, delimiter)
end

function _M.long_to_hex_string(long)
return string.format("0X%06X", long)
end

-- table len
function _M.table_len(args)
-- body
local n = 0
if args then
n = #args
end
return n
end

return _M
Loading

0 comments on commit eb1bbc8

Please sign in to comment.