github.com/creack/goproxy

github.com/creack/goproxy
只支持http反向代理;负载均衡只支持随机算法。
package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/creack/goproxy"
    "github.com/creack/goproxy/registry"
)

var Reg = registry.DefaultRegistry{
    "service1": {
        "v1": {
            "localhost:9091",
            "localhost:9092",
        },
    },
}

func main() {
    http.HandleFunc("/", goproxy.NewMultipleHostReverseProxy(Reg))
    http.HandleFunc("/health", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "%v\n", Reg)
    })
    //增加服务
    Reg.Add("service2", "v1", "localhost:9999")
    //删除服务
    Reg.Delete("service2", "v1", "localhost:9999")
    //根据名称和版本号返回可用的服务列表
    Reg.Lookup("service4", "v1")
    log.Fatal(http.ListenAndServe(":9090", nil))
}

 

posted @ 2022-10-29 20:08  delphi中间件  阅读(66)  评论(0编辑  收藏  举报