随笔分类 -  Go 反向代理

摘要:package main import ( "fmt" "sort" ) type ServerSlice []Server type Server struct { Weight int } //只要实现了下面三个方法就可以传入sort方法使用 func (p ServerSlice) Len() int { return len(p) } fu... 阅读全文
posted @ 2019-12-19 22:49 离地最远的星 阅读(389) 评论(0) 推荐(0) 编辑
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "sort" "time" ) type HttpServer struct { //目标server类 Host string Weight int CWeight int //当前权重 ... 阅读全文
posted @ 2019-12-19 22:48 离地最远的星 阅读(1045) 评论(0) 推荐(0) 编辑
摘要:package main import ( "log" "net/http" "net/http/httputil" "net/url" "os" "os/signal" "regexp" "反向代理/util" ) type ProxyHandler struct { } func (*ProxyHandler) ServeH... 阅读全文
posted @ 2019-12-19 22:47 离地最远的星 阅读(446) 评论(0) 推荐(0) 编辑
摘要:来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 22:46 离地最远的星 阅读(210) 评论(0) 推荐(0) 编辑
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "time" ) type HttpServer struct { //目标server类 Host string Weight int } func NewHttpServer(host string, weight int) *Ht... 阅读全文
posted @ 2019-12-19 22:45 离地最远的星 阅读(301) 评论(0) 推荐(0) 编辑
摘要:func (this *LoadBalance) RoundRobinByWeight3() *HttpServer { //平滑加权轮询 for _, s := range this.Servers { s.CWeight = s.CWeight + s.Weight } sort.Sort(this.Servers) max := this.... 阅读全文
posted @ 2019-12-19 22:44 离地最远的星 阅读(208) 评论(0) 推荐(0) 编辑
摘要:来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 22:43 离地最远的星 阅读(288) 评论(0) 推荐(0) 编辑
摘要:package util import ( "math/rand" "time" ) type HttpServer struct { //目标server类 Host string } func NewHttpServer(host string) *HttpServer { return &HttpServer{Host: host} } type Lo... 阅读全文
posted @ 2019-12-19 22:41 离地最远的星 阅读(244) 评论(0) 推荐(0) 编辑
摘要:Go实现 在头里设置WWW-Authenticate 返回401 func (webhandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { writer.Header().Set("WWW-Authenticate", `Basic realm="您必须输入用户名和密码"`) writer... 阅读全文
posted @ 2019-12-19 22:41 离地最远的星 阅读(178) 评论(0) 推荐(0) 编辑
摘要:package main import ( "io/ioutil" "log" "net/http" "os" "os/signal" ) type ProxyHandler struct { } func (*ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { d... 阅读全文
posted @ 2019-12-19 22:40 离地最远的星 阅读(120) 评论(0) 推荐(0) 编辑
摘要:func (this *LoadBalance) RoundRobinByWeight() *HttpServer { server := this.Servers[ServerIndices[this.CurIndex]] this.CurIndex = (this.CurIndex + 1) % len(ServerIndices) //ServersIndices存放的是按照... 阅读全文
posted @ 2019-12-19 22:39 离地最远的星 阅读(182) 评论(0) 推荐(0) 编辑
摘要:func (this *LoadBalance) SelectByWeightBetter(ip string) *HttpServer { rand.Seed(time.Now().UnixNano()) sumList := make([]int, len(this.Servers)) //this.servers是服务器列表 sum := 0 for ... 阅读全文
posted @ 2019-12-19 22:38 离地最远的星 阅读(156) 评论(0) 推荐(0) 编辑
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "time" ) type HttpServer struct { //目标server类 Host string Weight int } func NewHttpServer(host st 阅读全文
posted @ 2019-12-19 22:11 离地最远的星 阅读(327) 评论(0) 推荐(0) 编辑
摘要:![](https://xiahualou.oss-cn-shanghai.aliyuncs.com/img/20191219143156.png) 来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 22:08 离地最远的星 阅读(528) 评论(0) 推荐(0) 编辑
摘要:package main import ( "io/ioutil" "log" "net/http" "os" "os/signal" . "反向代理/util" ) type ProxyHandler struct { } func (*ProxyHandler) ServeHTTP(w http 阅读全文
posted @ 2019-12-19 22:06 离地最远的星 阅读(233) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示