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:
@@ -2,6 +2,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -22,6 +23,7 @@ import (
|
||||
"github.com/micro/go-micro/v2/logger"
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
registrySrv "github.com/micro/go-micro/v2/registry/service"
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
"github.com/micro/go-micro/v2/runtime"
|
||||
"github.com/micro/go-micro/v2/server"
|
||||
"github.com/micro/go-micro/v2/store"
|
||||
@@ -51,6 +53,12 @@ import (
|
||||
rmem "github.com/micro/go-micro/v2/registry/memory"
|
||||
regSrv "github.com/micro/go-micro/v2/registry/service"
|
||||
|
||||
// routers
|
||||
dnsRouter "github.com/micro/go-micro/v2/router/dns"
|
||||
regRouter "github.com/micro/go-micro/v2/router/registry"
|
||||
srvRouter "github.com/micro/go-micro/v2/router/service"
|
||||
staticRouter "github.com/micro/go-micro/v2/router/static"
|
||||
|
||||
// runtimes
|
||||
kRuntime "github.com/micro/go-micro/v2/runtime/kubernetes"
|
||||
lRuntime "github.com/micro/go-micro/v2/runtime/local"
|
||||
@@ -58,7 +66,7 @@ import (
|
||||
|
||||
// selectors
|
||||
"github.com/micro/go-micro/v2/client/selector/dns"
|
||||
"github.com/micro/go-micro/v2/client/selector/router"
|
||||
sRouter "github.com/micro/go-micro/v2/client/selector/router"
|
||||
"github.com/micro/go-micro/v2/client/selector/static"
|
||||
|
||||
// transports
|
||||
@@ -325,6 +333,11 @@ var (
|
||||
EnvVars: []string{"MICRO_CONFIG"},
|
||||
Usage: "The source of the config to be used to get configuration",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "router",
|
||||
EnvVars: []string{"MICRO_ROUTER"},
|
||||
Usage: "Router used for client requests",
|
||||
},
|
||||
}
|
||||
|
||||
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
|
||||
@@ -346,9 +359,16 @@ var (
|
||||
"memory": rmem.NewRegistry,
|
||||
}
|
||||
|
||||
DefaultRouters = map[string]func(...router.Option) router.Router{
|
||||
"dns": dnsRouter.NewRouter,
|
||||
"registry": regRouter.NewRouter,
|
||||
"static": staticRouter.NewRouter,
|
||||
"service": srvRouter.NewRouter,
|
||||
}
|
||||
|
||||
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
|
||||
"dns": dns.NewSelector,
|
||||
"router": router.NewSelector,
|
||||
"router": sRouter.NewSelector,
|
||||
"static": static.NewSelector,
|
||||
}
|
||||
|
||||
@@ -411,6 +431,7 @@ func newCmd(opts ...Option) Cmd {
|
||||
Server: &server.DefaultServer,
|
||||
Selector: &selector.DefaultSelector,
|
||||
Transport: &transport.DefaultTransport,
|
||||
Router: &router.DefaultRouter,
|
||||
Runtime: &runtime.DefaultRuntime,
|
||||
Store: &store.DefaultStore,
|
||||
Tracer: &trace.DefaultTracer,
|
||||
@@ -423,6 +444,7 @@ func newCmd(opts ...Option) Cmd {
|
||||
Selectors: DefaultSelectors,
|
||||
Servers: DefaultServers,
|
||||
Transports: DefaultTransports,
|
||||
Routers: DefaultRouters,
|
||||
Runtimes: DefaultRuntimes,
|
||||
Stores: DefaultStores,
|
||||
Tracers: DefaultTracers,
|
||||
@@ -548,6 +570,28 @@ func (c *cmd) Before(ctx *cli.Context) error {
|
||||
microClient := wrapper.CacheClient(cacheFn, grpc.NewClient())
|
||||
microClient = wrapper.AuthClient(authFn, microClient)
|
||||
|
||||
// Set the router, this must happen before the rest of the server as it'll route server requests
|
||||
// such as go.micro.config if no address is specified
|
||||
routerOpts := []router.Option{
|
||||
router.Network(ctx.String("service_namespace")),
|
||||
router.Registry(*c.opts.Registry),
|
||||
srvRouter.Client(microClient),
|
||||
}
|
||||
if name := ctx.String("router"); len(name) > 0 && (*c.opts.Router).String() != name {
|
||||
r, ok := c.opts.Routers[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("Router %s not found", name)
|
||||
}
|
||||
|
||||
*c.opts.Router = r(routerOpts...)
|
||||
// todo: set the router in the client
|
||||
// clientOpts = append(clientOpts, client.Router(*c.opts.Router))
|
||||
} else if len(routerOpts) > 0 {
|
||||
if err := (*c.opts.Router).Init(routerOpts...); err != nil {
|
||||
logger.Fatalf("Error configuring router: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Setup store options
|
||||
storeOpts := []store.Option{store.WithClient(microClient)}
|
||||
if len(ctx.String("store_address")) > 0 {
|
||||
|
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/micro/go-micro/v2/debug/profile"
|
||||
"github.com/micro/go-micro/v2/debug/trace"
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
"github.com/micro/go-micro/v2/runtime"
|
||||
"github.com/micro/go-micro/v2/server"
|
||||
"github.com/micro/go-micro/v2/store"
|
||||
@@ -31,6 +32,7 @@ type Options struct {
|
||||
Config *config.Config
|
||||
Client *client.Client
|
||||
Server *server.Server
|
||||
Router *router.Router
|
||||
Runtime *runtime.Runtime
|
||||
Store *store.Store
|
||||
Tracer *trace.Tracer
|
||||
@@ -44,6 +46,7 @@ type Options struct {
|
||||
Selectors map[string]func(...selector.Option) selector.Selector
|
||||
Servers map[string]func(...server.Option) server.Server
|
||||
Transports map[string]func(...transport.Option) transport.Transport
|
||||
Routers map[string]func(...router.Option) router.Router
|
||||
Runtimes map[string]func(...runtime.Option) runtime.Runtime
|
||||
Stores map[string]func(...store.Option) store.Store
|
||||
Tracers map[string]func(...trace.Option) trace.Tracer
|
||||
@@ -100,6 +103,12 @@ func Registry(r *registry.Registry) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func Router(r *router.Router) Option {
|
||||
return func(o *Options) {
|
||||
o.Router = r
|
||||
}
|
||||
}
|
||||
|
||||
func Runtime(r *runtime.Runtime) Option {
|
||||
return func(o *Options) {
|
||||
o.Runtime = r
|
||||
@@ -190,6 +199,13 @@ func NewTransport(name string, t func(...transport.Option) transport.Transport)
|
||||
}
|
||||
}
|
||||
|
||||
// New router func
|
||||
func NewRouter(name string, r func(...router.Option) router.Router) Option {
|
||||
return func(o *Options) {
|
||||
o.Routers[name] = r
|
||||
}
|
||||
}
|
||||
|
||||
// New runtime func
|
||||
func NewRuntime(name string, r func(...runtime.Option) runtime.Runtime) Option {
|
||||
return func(o *Options) {
|
||||
|
Reference in New Issue
Block a user