Skip to content

Commit

Permalink
router: improve router configuration (micro#1745)
Browse files Browse the repository at this point in the history
* router: update default address to :8084

* service: add router to service options

* config/cmd: improve router setup
  • Loading branch information
ben-toogood authored Jun 26, 2020
1 parent ee02511 commit 4f0f432
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
16 changes: 15 additions & 1 deletion config/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ var (
EnvVars: []string{"MICRO_ROUTER"},
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{
Expand Down Expand Up @@ -691,16 +696,25 @@ 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
// such as go.micro.config if no address is specified
routerOpts := []router.Option{
srvRouter.Client(microClient),
router.Network(ctx.String("service_namespace")),
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 {
r, ok := c.opts.Routers[name]
if !ok {
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...)
// todo: set the router in the client
// clientOpts = append(clientOpts, client.Router(*c.opts.Router))
Expand Down
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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"
Expand All @@ -30,6 +31,7 @@ type Options struct {
Server server.Server
Store store.Store
Registry registry.Registry
Router router.Router
Runtime runtime.Runtime
Transport transport.Transport
Profile profile.Profile
Expand Down Expand Up @@ -57,6 +59,7 @@ func newOptions(opts ...Option) Options {
Server: server.DefaultServer,
Store: store.DefaultStore,
Registry: registry.DefaultRegistry,
Router: router.DefaultRouter,
Runtime: runtime.DefaultRuntime,
Transport: transport.DefaultTransport,
Context: context.Background(),
Expand Down
2 changes: 1 addition & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

var (
// DefaultAddress is default router address
DefaultAddress = ":9093"
DefaultAddress = ":8084"
// DefaultName is default router service name
DefaultName = "go.micro.router"
// DefaultNetwork is default micro network
Expand Down

0 comments on commit 4f0f432

Please sign in to comment.