1 Star 1 Fork 2

qiqi / libra

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

libra 一个动态的反向代理均衡器

Build Status Go Report Card GoDoc codecov

English document中文文档

相关特性

  • 动态进行多地址反向代理
  • 动态管理源站地址
  • 支持动态修改定响应头
  • 代码都有严格的单元测试,可靠度高

你可以通过此包来快速构建一个动态的负载均衡服务器,目前有三种负载均衡算法,分别是随机,轮询,带权轮询;此包皆由原生代码构建,不依赖其他第三方包;如果你想快速的获得一个负载均衡服务器,快来使用它吧。

快速开始

下载安装

在控制台输入 go get github.com/zhuCheer/libra

运行示例

进入到包中 example 目录,然后运行 example.go 即可开启一个反向代理服务器

> cd ../src/github.com/zhuCheer/libra/example
> go run example.go

此时可以通过浏览器访问 http://127.0.0.1:5000 即可看到代理效果,会以轮询的方式访问http://127.0.0.1:5001, http://127.0.0.1:5002http://127.0.0.1:5003 这三个 http 服务。

详细步骤说明

import "github.com/zhuCheer/libra"

    
// 注册一个反向代理服务器,反代服务器访问的 ip 端口,和自定义响应头两个个参数
// 然后注册一个站点,包括域名,负载均衡类型,请求源站类型(http/https)
// 负载均衡类型目前有三种可选 random:随机,roundrobin:轮询,wroundrobin:带权轮询
var srv = libra.NewHttpProxySrv("127.0.0.1:5000", nil)
srv.RegistSite("www.yourappdomain.com", "roundrobin", "http")

// 添加代理目标的域名和 ip 地址端口
srv.AddAddr("www.yourappdomain.com", "127.0.0.1:5001", 1)
srv.AddAddr("www.yourappdomain.com", "127.0.0.1:5001", 2)


// 启动反向代理服务
srv.Start()

原理详解

  • 将我们的各类应用得域名都直接解析到此代理服务器,我们可称此服务为一个网关;
  • 反向代理服务启动后,我们通过不同的域名即可访问到我们添加的指定 ip 上,大多数情况我们会有多个服务器,访问请求将按照指定的负载均衡算法进行调度;
  • 访问过程如下图所示:

image

相关操作方法介绍

import "github.com/zhuCheer/libra"
var srv = libra.NewHttpProxySrv("127.0.0.1:5000", nil)
srv.RegistSite("www.yourappdomain.com", "roundrobin", "http")

// 设置响应头
srv.ResetCustomHeader(map[string]string{"X-LIBRA": "the smart ReverseProxy"})

// 切换指定站点的负载均衡类型
srv.ChangeLoadType("www.yourappdomain.com", "random")


// 添加目标服务器节点信息,添加后即生效,无需重启
srv.AddAddr("www.yourappdomain.com","192.168.1.100:8081", 1)

// 删除目标服务器节点信息,添加后即生效,无需重启
srv.DelAddr("www.yourappdomain.com","192.168.1.100:8081")
The MIT License (MIT) Copyright (c) 2016 - 2017 sumory.wu 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.

简介

golang api网关均衡器 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/zhucheer/libra.git
git@gitee.com:zhucheer/libra.git
zhucheer
libra
libra
master

搜索帮助

14c37bed 8189591 565d56ea 8189591