Add the ability to switch out client/server

This commit is contained in:
Asim 2016-11-18 17:29:26 +00:00
parent f9709ffa6e
commit 20feb95e18
6 changed files with 60 additions and 9 deletions

View File

@ -69,7 +69,7 @@ $ go run examples/service/main.go
### Test Service
```
$ go run examples/service/main.go --client
$ go run examples/service/main.go --run_client
Hello John
```

View File

@ -51,6 +51,11 @@ var (
DefaultCmd = newCmd()
DefaultFlags = []cli.Flag{
cli.StringFlag{
Name: "client",
EnvVar: "MICRO_CLIENT",
Usage: "Client for go-micro; rpc",
},
cli.StringFlag{
Name: "client_request_timeout",
EnvVar: "MICRO_CLIENT_REQUEST_TIMEOUT",
@ -127,6 +132,11 @@ var (
EnvVar: "MICRO_SELECTOR",
Usage: "Selector used to pick nodes for querying",
},
cli.StringFlag{
Name: "server",
EnvVar: "MICRO_SERVER",
Usage: "Server for go-micro; rpc",
},
cli.StringFlag{
Name: "transport",
EnvVar: "MICRO_TRANSPORT",
@ -143,6 +153,10 @@ var (
"http": http.NewBroker,
}
DefaultClients = map[string]func(...client.Option) client.Client{
"rpc": client.NewClient,
}
DefaultRegistries = map[string]func(...registry.Option) registry.Registry{
"consul": consul.NewRegistry,
"mdns": mdns.NewRegistry,
@ -153,11 +167,17 @@ var (
"cache": cache.NewSelector,
}
DefaultServers = map[string]func(...server.Option) server.Server{
"rpc": server.NewServer,
}
DefaultTransports = map[string]func(...transport.Option) transport.Transport{
"http": thttp.NewTransport,
}
// used for default selection as the fall back
defaultClient = "rpc"
defaultServer = "rpc"
defaultBroker = "http"
defaultRegistry = "consul"
defaultSelector = "default"
@ -183,8 +203,10 @@ func newCmd(opts ...Option) Cmd {
Transport: &transport.DefaultTransport,
Brokers: DefaultBrokers,
Clients: DefaultClients,
Registries: DefaultRegistries,
Selectors: DefaultSelectors,
Servers: DefaultServers,
Transports: DefaultTransports,
}
@ -226,6 +248,20 @@ func (c *cmd) Before(ctx *cli.Context) error {
var serverOpts []server.Option
var clientOpts []client.Option
// Set the client
if name := ctx.String("client"); len(name) > 0 {
if cl, ok := c.opts.Clients[name]; ok {
*c.opts.Client = cl()
}
}
// Set the server
if name := ctx.String("server"); len(name) > 0 {
if s, ok := c.opts.Servers[name]; ok {
*c.opts.Server = s()
}
}
// Set the broker
if name := ctx.String("broker"); len(name) > 0 || len(ctx.String("broker_address")) > 0 {
if len(name) == 0 {
@ -241,7 +277,6 @@ func (c *cmd) Before(ctx *cli.Context) error {
serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))
}
// Set the registry

View File

@ -26,8 +26,10 @@ type Options struct {
Server *server.Server
Brokers map[string]func(...broker.Option) broker.Broker
Clients map[string]func(...client.Option) client.Client
Registries map[string]func(...registry.Option) registry.Registry
Selectors map[string]func(...selector.Option) selector.Selector
Servers map[string]func(...server.Option) server.Server
Transports map[string]func(...transport.Option) transport.Transport
// Other options for implementations of the interface
@ -99,6 +101,13 @@ func NewBroker(name string, b func(...broker.Option) broker.Broker) Option {
}
}
// New client func
func NewClient(name string, b func(...client.Option) client.Client) Option {
return func(o *Options) {
o.Clients[name] = b
}
}
// New registry func
func NewRegistry(name string, r func(...registry.Option) registry.Registry) Option {
return func(o *Options) {
@ -113,6 +122,13 @@ func NewSelector(name string, s func(...selector.Option) selector.Selector) Opti
}
}
// New server func
func NewServer(name string, s func(...server.Option) server.Server) Option {
return func(o *Options) {
o.Servers[name] = s
}
}
// New transport func
func NewTransport(name string, t func(...transport.Option) transport.Transport) Option {
return func(o *Options) {

View File

@ -32,7 +32,7 @@ $GOPATH/bin/service
3. Run the client
```shell
$GOPATH/bin/service --client
$GOPATH/bin/service --run_client
```
And that's all there is to it.

View File

@ -48,12 +48,12 @@ func main() {
"type": "helloworld",
}),
// Setup some flags. Specify --client to run the client
// Setup some flags. Specify --run_client to run the client
// Add runtime flags
// We could do this below too
micro.Flags(cli.BoolFlag{
Name: "client",
Name: "run_client",
Usage: "Launch the client",
}),
)
@ -65,7 +65,7 @@ func main() {
// Add runtime action
// We could actually do this above
micro.Action(func(c *cli.Context) {
if c.Bool("client") {
if c.Bool("run_client") {
runClient(service)
os.Exit(0)
}

View File

@ -78,12 +78,12 @@ func main() {
"type": "helloworld",
}),
// Setup some flags. Specify --client to run the client
// Setup some flags. Specify --run_client to run the client
// Add runtime flags
// We could do this below too
micro.Flags(cli.BoolFlag{
Name: "client",
Name: "run_client",
Usage: "Launch the client",
}),
)
@ -95,7 +95,7 @@ func main() {
// Add runtime action
// We could actually do this above
micro.Action(func(c *cli.Context) {
if c.Bool("client") {
if c.Bool("run_client") {
runClient(service)
os.Exit(0)
}