通过go二次重构,以更简单的方式实现原来在openresty通过lua实现的功能,主要的重构的部分在upstream、healthCheck和weightServerList部分
开发标准restful接口来实现对upstream的增删改查。
下面的实例都将以请求json来进行示例。
获取资源。
获取已有资源。
只会读取upstreamName关键词,即使数据结构传入其他参数。
{
"upstreamName": "vmims"
}
{
"upstreamName": "ALL"
}
更新资源。
只能更新,不能创建。
{
"upstreamName": "vmims",
"algorithms": "round-robin",
"pool": [
{
"ipPort": "192.168.101.59:8080",
"status": "up",
"weight": 2
},
{
"ipPort": "192.168.101.61:8080",
"status": "down",
"weight": 3
},
{
"ipPort": "192.168.101.77:8080",
"status": "down",
"weight": 6
}
]
}
更新相同的upstreamName,会全量覆盖,如果更新不存在资源则会报错。
{
"Error": "No Key [UpstreamVmims]",
"Message": "Upstream PUT: Etcd Get: Key Not Exist Error",
"Code": 2102,
"TimeStamp": "2020-01-10T17:41:55.8043597+08:00",
"ExecutorTime": "7.9785ms"
}
创建资源。
会做校验,只能创建不能更新。
{
"upstreamName": "vmims",
"algorithms": "round-robin",
"pool": [
{
"ipPort": "192.168.101.59:8080",
"status": "up",
"weight": 3
},
{
"ipPort": "192.168.101.61:8080",
"status": "down",
"weight": 4
}
]
}
如果创建已经存在的资源,则会报错:
{
"Error": "Upstream POST: Etcd Get: Repeat Key Error",
"Message": "Upstream POST: Etcd Get: Repeat Key Error",
"Code": 3103,
"TimeStamp": "2020-01-10T17:43:14.9657714+08:00",
"ExecutorTime": "3.9582ms"
}
更新部分已有资源。
{
"upstreamName": "vm",
"algorithms": "ip-hex",
"pool": [
{
"ipPort": "192.168.101.59:8080",
"status": "nohc",
},
{
"ipPort": "192.168.101.61:8080",
"status": "down",
"weight": 7
},
{
"ipPort": "192.168.101.61:8085",
"status": "up"
},
{
"ipPort": "192.168.101.6:9999",
"status": "up",
"weight": 15
}
]
}
上面的示例当中展示了四种不同情况:
- patch修改已经存在资源的pool,status或者weight其中一个。
- patch修改已经存在资源pool,status和weight需要全部,将新创建server
- patch修改不存在资源pool,不会有资源生效。
- patch单独修改algorithms资源,pool可以为空,或者同时修改。
删除资源,无论是否存在。
{
"upstreamName": "vmims"
}
删除upstream的pool,一个或者多个。
{
"upstreamName": "vm",
"algorithms": "round-robin",
"pool": [
{
"ipPort": "192.168.101.59:8080",
"status": "up",
"weight": 2
},
{
"ipPort": "192.168.101.61:8080",
"status": "down",
"weight": 3
}
]
}
delete删除资源pool有三种情况:
- 删除一个或者多个,当etcd存储的资源的pool已经只剩一个server list,就会返回删除失败,保证每个upstream至少有一个server list。即使准备删除的server list和已经存在server list不匹配,也会返回删除失败。
- 删除一个或者多个,当检测到将pool下面的所有server list全部删除,也会返回删除失败。删除之后至少保证,pool当中还有一个server list可以使用,不能全部删除。
- 成功删除一个或者多个server list。
BSD 2-Clause License
Copyright (c) 2019, Zhendong All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.