add router selector and network defaults

This commit is contained in:
Asim Aslam
2019-06-26 16:12:57 +01:00
parent 1a62c11166
commit ac098e4d78
6 changed files with 292 additions and 2 deletions

View File

@@ -7,10 +7,12 @@ import (
"strings"
"github.com/micro/go-micro/client"
rselect "github.com/micro/go-micro/client/selector/router"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/proxy"
"github.com/micro/go-micro/network/router"
"github.com/micro/go-micro/server"
)
@@ -162,5 +164,18 @@ func NewProxy(opts ...options.Option) proxy.Proxy {
p.Client = c.(client.Client)
}
// get router
r, ok := p.Options.Values().Get("proxy.router")
if ok {
// set the router in the client
p.Client.Init(
// pass new selector as an option to the client
client.Selector(rselect.NewSelector(
// set the router in the selector
rselect.WithRouter(r.(router.Router)),
)),
)
}
return p
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/router"
"github.com/micro/go-micro/server"
)
@@ -29,3 +30,8 @@ func WithEndpoint(e string) options.Option {
func WithClient(c client.Client) options.Option {
return options.WithValue("proxy.client", c)
}
// WithRouter specifies the router to use
func WithRouter(r router.Router) options.Option {
return options.WithValue("proxy.router", r)
}