在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:vulcand/oxy开源软件地址:https://github.com/vulcand/oxy开源编程语言:Go 99.9%开源软件介绍:OxyOxy is a Go library with HTTP handlers that enhance HTTP standard library:
It is designed to be fully compatible with http standard library, easy to customize and reuse. Status
QuickstartEvery handler is Simple reverse proxyimport (
"net/http"
"github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/testutils"
)
// Forwards incoming requests to whatever location URL points to, adds proper forwarding headers
fwd, _ := forward.New()
redirect := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// let us forward this request to another server
req.URL = testutils.ParseURI("http://localhost:63450")
fwd.ServeHTTP(w, req)
})
// that's it! our reverse proxy is ready!
s := &http.Server{
Addr: ":8080",
Handler: redirect,
}
s.ListenAndServe() As a next step, let us add a round robin load-balancer: import (
"net/http"
"github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/roundrobin"
)
// Forwards incoming requests to whatever location URL points to, adds proper forwarding headers
fwd, _ := forward.New()
lb, _ := roundrobin.New(fwd)
lb.UpsertServer(url1)
lb.UpsertServer(url2)
s := &http.Server{
Addr: ":8080",
Handler: lb,
}
s.ListenAndServe() What if we want to handle retries and replay the request in case of errors? import (
"net/http"
"github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/buffer"
"github.com/vulcand/oxy/roundrobin"
)
// Forwards incoming requests to whatever location URL points to, adds proper forwarding headers
fwd, _ := forward.New()
lb, _ := roundrobin.New(fwd)
// buffer will read the request body and will replay the request again in case if forward returned status
// corresponding to nework error (e.g. Gateway Timeout)
buffer, _ := buffer.New(lb, buffer.Retry(`IsNetworkError() && Attempts() < 2`))
lb.UpsertServer(url1)
lb.UpsertServer(url2)
// that's it! our reverse proxy is ready!
s := &http.Server{
Addr: ":8080",
Handler: buffer,
}
s.ListenAndServe() |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论