Deprecate client/selector (#1767)
* client/{grpc,rpc}: depricate selector (wip) * {client,cmd}: remove client/selector * deprecate client/selector * router/static: fix lookup * config/cmd: add support for legacy static selector flag * config/cmd: add support for legacy dns selector flag
This commit is contained in:
@@ -8,9 +8,9 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/micro/go-micro/v2/client/selector"
|
||||
"github.com/micro/go-micro/v2/metadata"
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
"github.com/micro/go-micro/v2/selector/random"
|
||||
)
|
||||
|
||||
// Write sets the status and body on a http ResponseWriter
|
||||
@@ -47,7 +47,7 @@ func WriteInternalServerError(w http.ResponseWriter, err error) {
|
||||
|
||||
func NewRoundTripper(opts ...Option) http.RoundTripper {
|
||||
options := Options{
|
||||
Registry: registry.DefaultRegistry,
|
||||
Router: router.DefaultRouter,
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
@@ -55,7 +55,7 @@ func NewRoundTripper(opts ...Option) http.RoundTripper {
|
||||
|
||||
return &roundTripper{
|
||||
rt: http.DefaultTransport,
|
||||
st: selector.Random,
|
||||
st: random.NewSelector(),
|
||||
opts: options,
|
||||
}
|
||||
}
|
||||
|
@@ -8,14 +8,15 @@ import (
|
||||
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
"github.com/micro/go-micro/v2/registry/memory"
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
regRouter "github.com/micro/go-micro/v2/router/registry"
|
||||
)
|
||||
|
||||
func TestRoundTripper(t *testing.T) {
|
||||
m := memory.NewRegistry()
|
||||
r := regRouter.NewRouter(router.Registry(m))
|
||||
|
||||
rt := NewRoundTripper(
|
||||
WithRegistry(m),
|
||||
)
|
||||
rt := NewRoundTripper(WithRouter(r))
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`hello world`))
|
||||
|
@@ -1,17 +1,15 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
)
|
||||
import "github.com/micro/go-micro/v2/router"
|
||||
|
||||
type Options struct {
|
||||
Registry registry.Registry
|
||||
Router router.Router
|
||||
}
|
||||
|
||||
type Option func(*Options)
|
||||
|
||||
func WithRegistry(r registry.Registry) Option {
|
||||
func WithRouter(r router.Router) Option {
|
||||
return func(o *Options) {
|
||||
o.Registry = r
|
||||
o.Router = r
|
||||
}
|
||||
}
|
||||
|
@@ -4,34 +4,35 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/micro/go-micro/v2/client/selector"
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
"github.com/micro/go-micro/v2/selector"
|
||||
)
|
||||
|
||||
type roundTripper struct {
|
||||
rt http.RoundTripper
|
||||
st selector.Strategy
|
||||
st selector.Selector
|
||||
opts Options
|
||||
}
|
||||
|
||||
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
s, err := r.opts.Registry.GetService(req.URL.Host)
|
||||
routes, err := r.opts.Router.Lookup(router.QueryService(req.URL.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
next := r.st(s)
|
||||
|
||||
// rudimentary retry 3 times
|
||||
for i := 0; i < 3; i++ {
|
||||
n, err := next()
|
||||
route, err := r.st.Select(routes)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
req.URL.Host = n.Address
|
||||
|
||||
req.URL.Host = route.Address
|
||||
w, err := r.rt.RoundTrip(req)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
return w, nil
|
||||
}
|
||||
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
bmemory "github.com/micro/go-micro/v2/broker/memory"
|
||||
"github.com/micro/go-micro/v2/client"
|
||||
rmemory "github.com/micro/go-micro/v2/registry/memory"
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
"github.com/micro/go-micro/v2/server"
|
||||
tmemory "github.com/micro/go-micro/v2/transport/memory"
|
||||
wrapper "github.com/micro/go-micro/v2/util/wrapper"
|
||||
@@ -36,6 +37,7 @@ func TestStaticClientWrapper(t *testing.T) {
|
||||
reg := rmemory.NewRegistry()
|
||||
brk := bmemory.NewBroker(broker.Registry(reg))
|
||||
tr := tmemory.NewTransport()
|
||||
rtr := router.NewRouter(router.Registry(reg))
|
||||
|
||||
srv := server.NewServer(
|
||||
server.Broker(brk),
|
||||
@@ -53,7 +55,7 @@ func TestStaticClientWrapper(t *testing.T) {
|
||||
}
|
||||
|
||||
cli := client.NewClient(
|
||||
client.Registry(reg),
|
||||
client.Router(rtr),
|
||||
client.Broker(brk),
|
||||
client.Transport(tr),
|
||||
)
|
||||
|
Reference in New Issue
Block a user