diff --git a/cmd/cmd.go b/cmd/cmd.go index dbbc4e28..b7e73e0c 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -4,6 +4,7 @@ package cmd import ( "fmt" "math/rand" + "os" "sort" "strings" "time" @@ -23,6 +24,7 @@ import ( "go-micro.dev/v5/debug/profile/pprof" "go-micro.dev/v5/debug/trace" "go-micro.dev/v5/logger" + mprofile "go-micro.dev/v5/profile" "go-micro.dev/v5/registry" "go-micro.dev/v5/registry/consul" "go-micro.dev/v5/registry/etcd" @@ -141,8 +143,8 @@ var ( }, &cli.StringFlag{ Name: "profile", - Usage: "Debug profiler for cpu and memory stats", - EnvVars: []string{"MICRO_DEBUG_PROFILE"}, + Usage: "Plugin profile to use. (local, nats, etc)", + EnvVars: []string{"MICRO_PROFILE"}, }, &cli.StringFlag{ Name: "registry", @@ -437,6 +439,40 @@ func (c *cmd) Before(ctx *cli.Context) error { } } + // --- Profile Grouping Extension --- + // Check for new profile flag/env (not just debug profiler) + profileName := ctx.String("profile") + if profileName == "" { + profileName = os.Getenv("MICRO_PROFILE") + } + if profileName != "" { + switch profileName { + case "local": + imported := mprofile.LocalProfile() + *c.opts.Registry = imported.Registry + registry.DefaultRegistry = imported.Registry + *c.opts.Broker = imported.Broker + broker.DefaultBroker = imported.Broker + *c.opts.Store = imported.Store + store.DefaultStore = imported.Store + *c.opts.Transport = imported.Transport + transport.DefaultTransport = imported.Transport + case "nats": + imported := mprofile.NatsProfile() + *c.opts.Registry = imported.Registry + registry.DefaultRegistry = imported.Registry + *c.opts.Broker = imported.Broker + broker.DefaultBroker = imported.Broker + *c.opts.Store = imported.Store + store.DefaultStore = imported.Store + *c.opts.Transport = imported.Transport + transport.DefaultTransport = imported.Transport + // Add more profiles as needed + default: + return fmt.Errorf("unsupported profile: %s", profileName) + } + } + // Set the profile if name := ctx.String("profile"); len(name) > 0 { p, ok := c.opts.Profiles[name] diff --git a/profile/profile.go b/profile/profile.go new file mode 100644 index 00000000..52191652 --- /dev/null +++ b/profile/profile.go @@ -0,0 +1,43 @@ +// Package profileconfig provides grouped plugin profiles for go-micro +package profile + +import ( + "os" + "go-micro.dev/v5/broker" + "go-micro.dev/v5/broker/http" + "go-micro.dev/v5/broker/nats" + "go-micro.dev/v5/registry" + nreg "go-micro.dev/v5/registry/nats" + "go-micro.dev/v5/store" + + "go-micro.dev/v5/transport" + +) + +type Profile struct { + Registry registry.Registry + Broker broker.Broker + Store store.Store + Transport transport.Transport +} + +func LocalProfile() Profile { + return Profile{ + Registry: registry.NewMDNSRegistry(), + Broker: http.NewHttpBroker(), + Store: store.NewFileStore(), + Transport: transport.NewHTTPTransport(), + } +} + +func NatsProfile() Profile { + addr := os.Getenv("MICRO_NATS_ADDRESS") + return Profile{ + Registry: nreg.NewNatsRegistry(registry.Addrs(addr)), + Broker: nats.NewNatsBroker(broker.Addrs(addr)), + Store: store.NewFileStore(), // nats-backed store when available + Transport: transport.NewHTTPTransport(), // nats transport when available + } +} + +// Add more profiles as needed, e.g. grpc