router: add to service options; add dns and static implementations (#1733)
* config/cmd: add router to service options * router/service: use micro client
This commit is contained in:
22
router/service/options.go
Normal file
22
router/service/options.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/go-micro/v2/client"
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
)
|
||||
|
||||
type clientKey struct{}
|
||||
|
||||
// Client to call router service
|
||||
func Client(c client.Client) router.Option {
|
||||
return func(o *router.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.WithValue(context.Background(), clientKey{}, c)
|
||||
return
|
||||
}
|
||||
|
||||
o.Context = context.WithValue(o.Context, clientKey{}, c)
|
||||
}
|
||||
}
|
@@ -36,9 +36,10 @@ func NewRouter(opts ...router.Option) router.Router {
|
||||
// NOTE: might need some client opts here
|
||||
cli := client.DefaultClient
|
||||
|
||||
// set options client
|
||||
if options.Client != nil {
|
||||
cli = options.Client
|
||||
// get options client from the context. We set this in the context to prevent an import loop, as
|
||||
// the client depends on the router
|
||||
if c, ok := options.Context.Value(clientKey{}).(client.Client); ok {
|
||||
cli = c
|
||||
}
|
||||
|
||||
// NOTE: should we have Client/Service option in router.Options?
|
||||
|
Reference in New Issue
Block a user