随笔分类 - Go 反向代理
摘要:package main import ( "fmt" "sort" ) type ServerSlice []Server type Server struct { Weight int } //只要实现了下面三个方法就可以传入sort方法使用 func (p ServerSlice) Len() int { return len(p) } fu...
阅读全文
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "sort" "time" ) type HttpServer struct { //目标server类 Host string Weight int CWeight int //当前权重 ...
阅读全文
摘要:package main import ( "log" "net/http" "net/http/httputil" "net/url" "os" "os/signal" "regexp" "反向代理/util" ) type ProxyHandler struct { } func (*ProxyHandler) ServeH...
阅读全文
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "time" ) type HttpServer struct { //目标server类 Host string Weight int } func NewHttpServer(host string, weight int) *Ht...
阅读全文
摘要:func (this *LoadBalance) RoundRobinByWeight3() *HttpServer { //平滑加权轮询 for _, s := range this.Servers { s.CWeight = s.CWeight + s.Weight } sort.Sort(this.Servers) max := this....
阅读全文
摘要:package util import ( "math/rand" "time" ) type HttpServer struct { //目标server类 Host string } func NewHttpServer(host string) *HttpServer { return &HttpServer{Host: host} } type Lo...
阅读全文
摘要:Go实现 在头里设置WWW-Authenticate 返回401 func (webhandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { writer.Header().Set("WWW-Authenticate", `Basic realm="您必须输入用户名和密码"`) writer...
阅读全文
摘要:package main import ( "io/ioutil" "log" "net/http" "os" "os/signal" ) type ProxyHandler struct { } func (*ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { d...
阅读全文
摘要:func (this *LoadBalance) RoundRobinByWeight() *HttpServer { server := this.Servers[ServerIndices[this.CurIndex]] this.CurIndex = (this.CurIndex + 1) % len(ServerIndices) //ServersIndices存放的是按照...
阅读全文
摘要:func (this *LoadBalance) SelectByWeightBetter(ip string) *HttpServer { rand.Seed(time.Now().UnixNano()) sumList := make([]int, len(this.Servers)) //this.servers是服务器列表 sum := 0 for ...
阅读全文
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "time" ) type HttpServer struct { //目标server类 Host string Weight int } func NewHttpServer(host st
阅读全文
摘要:![](https://xiahualou.oss-cn-shanghai.aliyuncs.com/img/20191219143156.png) 来自为知笔记(Wiz)
阅读全文
摘要:package main import ( "io/ioutil" "log" "net/http" "os" "os/signal" . "反向代理/util" ) type ProxyHandler struct { } func (*ProxyHandler) ServeHTTP(w http
阅读全文