router: improve router configuration (#1745)

* router: update default address to :8084

* service: add router to service options

* config/cmd: improve router setup
This commit is contained in:
ben-toogood 2020-06-26 10:38:11 +01:00 committed by GitHub
parent ee02511658
commit 4f0f4326df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -343,6 +343,11 @@ var (
EnvVars: []string{"MICRO_ROUTER"}, EnvVars: []string{"MICRO_ROUTER"},
Usage: "Router used for client requests", Usage: "Router used for client requests",
}, },
&cli.StringFlag{
Name: "router_address",
Usage: "Comma-separated list of router addresses",
EnvVars: []string{"MICRO_ROUTER_ADDRESS"},
},
} }
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{ DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
@ -691,9 +696,13 @@ func (c *cmd) Before(ctx *cli.Context) error {
// Set the router, this must happen before the rest of the server as it'll route server requests // 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 // such as go.micro.config if no address is specified
routerOpts := []router.Option{ routerOpts := []router.Option{
srvRouter.Client(microClient),
router.Network(ctx.String("service_namespace")), router.Network(ctx.String("service_namespace")),
router.Registry(*c.opts.Registry), router.Registry(*c.opts.Registry),
srvRouter.Client(microClient), router.Id((*c.opts.Server).Options().Id),
}
if len(ctx.String("router_address")) > 0 {
routerOpts = append(routerOpts, router.Address(ctx.String("router_address")))
} }
if name := ctx.String("router"); len(name) > 0 && (*c.opts.Router).String() != name { if name := ctx.String("router"); len(name) > 0 && (*c.opts.Router).String() != name {
r, ok := c.opts.Routers[name] r, ok := c.opts.Routers[name]
@ -701,6 +710,11 @@ func (c *cmd) Before(ctx *cli.Context) error {
return fmt.Errorf("Router %s not found", name) return fmt.Errorf("Router %s not found", name)
} }
// close the default router before replacing it
if err := (*c.opts.Router).Close(); err != nil {
logger.Fatalf("Error closing default router: %s", name)
}
*c.opts.Router = r(routerOpts...) *c.opts.Router = r(routerOpts...)
// todo: set the router in the client // todo: set the router in the client
// clientOpts = append(clientOpts, client.Router(*c.opts.Router)) // clientOpts = append(clientOpts, client.Router(*c.opts.Router))

View File

@ -14,6 +14,7 @@ import (
"github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry" "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/runtime"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/store"
@ -30,6 +31,7 @@ type Options struct {
Server server.Server Server server.Server
Store store.Store Store store.Store
Registry registry.Registry Registry registry.Registry
Router router.Router
Runtime runtime.Runtime Runtime runtime.Runtime
Transport transport.Transport Transport transport.Transport
Profile profile.Profile Profile profile.Profile
@ -57,6 +59,7 @@ func newOptions(opts ...Option) Options {
Server: server.DefaultServer, Server: server.DefaultServer,
Store: store.DefaultStore, Store: store.DefaultStore,
Registry: registry.DefaultRegistry, Registry: registry.DefaultRegistry,
Router: router.DefaultRouter,
Runtime: runtime.DefaultRuntime, Runtime: runtime.DefaultRuntime,
Transport: transport.DefaultTransport, Transport: transport.DefaultTransport,
Context: context.Background(), Context: context.Background(),

View File

@ -7,7 +7,7 @@ import (
var ( var (
// DefaultAddress is default router address // DefaultAddress is default router address
DefaultAddress = ":9093" DefaultAddress = ":8084"
// DefaultName is default router service name // DefaultName is default router service name
DefaultName = "go.micro.router" DefaultName = "go.micro.router"
// DefaultNetwork is default micro network // DefaultNetwork is default micro network