Skip to content

Commit 0e29790

Browse files
committed
Merge remote-tracking branch 'origin/master' into moreplugins
2 parents cb46674 + e12504c commit 0e29790

File tree

8 files changed

+148
-61
lines changed

8 files changed

+148
-61
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ curl -XPOST \
106106
http://localhost:8080
107107
```
108108

109-
## Users
109+
## Adopters
110110

111111
- [Sourse](https://sourse.eu) - Work in the field of earth observation, including embedded Kubernetes running onboard aircraft, and we’ve built a mission management SaaS platform using Go Micro.

cmd/cmd.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package cmd
44
import (
55
"fmt"
66
"math/rand"
7+
"os"
8+
"sort"
79
"strings"
810
"time"
911

@@ -23,10 +25,10 @@ import (
2325
"go-micro.dev/v5/debug/profile/pprof"
2426
"go-micro.dev/v5/debug/trace"
2527
"go-micro.dev/v5/logger"
28+
mprofile "go-micro.dev/v5/profile"
2629
"go-micro.dev/v5/registry"
2730
"go-micro.dev/v5/registry/consul"
2831
"go-micro.dev/v5/registry/etcd"
29-
"go-micro.dev/v5/registry/mdns"
3032
"go-micro.dev/v5/registry/nats"
3133
"go-micro.dev/v5/selector"
3234
"go-micro.dev/v5/server"
@@ -145,8 +147,8 @@ var (
145147
},
146148
&cli.StringFlag{
147149
Name: "profile",
148-
Usage: "Debug profiler for cpu and memory stats",
149-
EnvVars: []string{"MICRO_DEBUG_PROFILE"},
150+
Usage: "Plugin profile to use. (local, nats, etc)",
151+
EnvVars: []string{"MICRO_PROFILE"},
150152
},
151153
&cli.StringFlag{
152154
Name: "registry",
@@ -254,7 +256,7 @@ var (
254256
"consul": consul.NewConsulRegistry,
255257
"memory": registry.NewMemoryRegistry,
256258
"nats": nats.NewNatsRegistry,
257-
"mdns": mdns.NewMDNSRegistry,
259+
"mdns": registry.NewMDNSRegistry,
258260
"etcd": etcd.NewEtcdRegistry,
259261
}
260262

@@ -452,6 +454,40 @@ func (c *cmd) Before(ctx *cli.Context) error {
452454
registry.DefaultRegistry = *c.opts.Registry
453455
}
454456

457+
// --- Profile Grouping Extension ---
458+
// Check for new profile flag/env (not just debug profiler)
459+
profileName := ctx.String("profile")
460+
if profileName == "" {
461+
profileName = os.Getenv("MICRO_PROFILE")
462+
}
463+
if profileName != "" {
464+
switch profileName {
465+
case "local":
466+
imported := mprofile.LocalProfile()
467+
*c.opts.Registry = imported.Registry
468+
registry.DefaultRegistry = imported.Registry
469+
*c.opts.Broker = imported.Broker
470+
broker.DefaultBroker = imported.Broker
471+
*c.opts.Store = imported.Store
472+
store.DefaultStore = imported.Store
473+
*c.opts.Transport = imported.Transport
474+
transport.DefaultTransport = imported.Transport
475+
case "nats":
476+
imported := mprofile.NatsProfile()
477+
*c.opts.Registry = imported.Registry
478+
registry.DefaultRegistry = imported.Registry
479+
*c.opts.Broker = imported.Broker
480+
broker.DefaultBroker = imported.Broker
481+
*c.opts.Store = imported.Store
482+
store.DefaultStore = imported.Store
483+
*c.opts.Transport = imported.Transport
484+
transport.DefaultTransport = imported.Transport
485+
// Add more profiles as needed
486+
default:
487+
return fmt.Errorf("unsupported profile: %s", profileName)
488+
}
489+
}
490+
455491
// Set the profile
456492
if name := ctx.String("profile"); len(name) > 0 {
457493
p, ok := c.opts.Profiles[name]
@@ -678,3 +714,16 @@ func Init(opts ...Option) error {
678714
func NewCmd(opts ...Option) Cmd {
679715
return newCmd(opts...)
680716
}
717+
718+
// Register CLI commands
719+
func Register(cmds ...*cli.Command) {
720+
app := DefaultCmd.App()
721+
app.Commands = append(app.Commands, cmds...)
722+
723+
// sort the commands so they're listed in order on the cli
724+
// todo: move this to micro/cli so it's only run when the
725+
// commands are printed during "help"
726+
sort.Slice(app.Commands, func(i, j int) bool {
727+
return app.Commands[i].Name < app.Commands[j].Name
728+
})
729+
}

profile/profile.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Package profileconfig provides grouped plugin profiles for go-micro
2+
package profile
3+
4+
import (
5+
"os"
6+
"go-micro.dev/v5/broker"
7+
"go-micro.dev/v5/broker/http"
8+
"go-micro.dev/v5/broker/nats"
9+
"go-micro.dev/v5/registry"
10+
nreg "go-micro.dev/v5/registry/nats"
11+
"go-micro.dev/v5/store"
12+
13+
"go-micro.dev/v5/transport"
14+
15+
)
16+
17+
type Profile struct {
18+
Registry registry.Registry
19+
Broker broker.Broker
20+
Store store.Store
21+
Transport transport.Transport
22+
}
23+
24+
func LocalProfile() Profile {
25+
return Profile{
26+
Registry: registry.NewMDNSRegistry(),
27+
Broker: http.NewHttpBroker(),
28+
Store: store.NewFileStore(),
29+
Transport: transport.NewHTTPTransport(),
30+
}
31+
}
32+
33+
func NatsProfile() Profile {
34+
addr := os.Getenv("MICRO_NATS_ADDRESS")
35+
return Profile{
36+
Registry: nreg.NewNatsRegistry(registry.Addrs(addr)),
37+
Broker: nats.NewNatsBroker(broker.Addrs(addr)),
38+
Store: store.NewFileStore(), // nats-backed store when available
39+
Transport: transport.NewHTTPTransport(), // nats transport when available
40+
}
41+
}
42+
43+
// Add more profiles as needed, e.g. grpc

registry/mdns/mdns.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

registry/mdns/mdns_registry.go renamed to registry/mdns_registry.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Package mdns is a multicast dns registry
2-
package mdns
2+
package registry
33

44
import (
55
"bytes"
@@ -17,7 +17,6 @@ import (
1717

1818
"github.com/google/uuid"
1919
log "go-micro.dev/v5/logger"
20-
"go-micro.dev/v5/registry"
2120
"go-micro.dev/v5/util/mdns"
2221
)
2322

@@ -30,7 +29,7 @@ type mdnsTxt struct {
3029
Metadata map[string]string
3130
Service string
3231
Version string
33-
Endpoints []*registry.Endpoint
32+
Endpoints []*Endpoint
3433
}
3534

3635
type mdnsEntry struct {
@@ -39,7 +38,7 @@ type mdnsEntry struct {
3938
}
4039

4140
type mdnsRegistry struct {
42-
opts *registry.Options
41+
opts *Options
4342
services map[string][]*mdnsEntry
4443

4544
// watchers
@@ -56,7 +55,7 @@ type mdnsRegistry struct {
5655
}
5756

5857
type mdnsWatcher struct {
59-
wo registry.WatchOptions
58+
wo WatchOptions
6059
ch chan *mdns.ServiceEntry
6160
exit chan struct{}
6261
// the registry
@@ -128,9 +127,9 @@ func decode(record []string) (*mdnsTxt, error) {
128127

129128
return txt, nil
130129
}
131-
func newRegistry(opts ...registry.Option) registry.Registry {
132-
mergedOpts := append([]registry.Option{registry.Timeout(time.Millisecond * 100)}, opts...)
133-
options := registry.NewOptions(mergedOpts...)
130+
func newRegistry(opts ...Option) Registry {
131+
mergedOpts := append([]Option{Timeout(time.Millisecond * 100)}, opts...)
132+
options := NewOptions(mergedOpts...)
134133

135134
// set the domain
136135
domain := mdnsDomain
@@ -148,18 +147,18 @@ func newRegistry(opts ...registry.Option) registry.Registry {
148147
}
149148
}
150149

151-
func (m *mdnsRegistry) Init(opts ...registry.Option) error {
150+
func (m *mdnsRegistry) Init(opts ...Option) error {
152151
for _, o := range opts {
153152
o(m.opts)
154153
}
155154
return nil
156155
}
157156

158-
func (m *mdnsRegistry) Options() registry.Options {
157+
func (m *mdnsRegistry) Options() Options {
159158
return *m.opts
160159
}
161160

162-
func (m *mdnsRegistry) Register(service *registry.Service, opts ...registry.RegisterOption) error {
161+
func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error {
163162
m.Lock()
164163
defer m.Unlock()
165164

@@ -264,7 +263,7 @@ func (m *mdnsRegistry) Register(service *registry.Service, opts ...registry.Regi
264263
return gerr
265264
}
266265

267-
func (m *mdnsRegistry) Deregister(service *registry.Service, opts ...registry.DeregisterOption) error {
266+
func (m *mdnsRegistry) Deregister(service *Service, opts ...DeregisterOption) error {
268267
m.Lock()
269268
defer m.Unlock()
270269

@@ -299,9 +298,9 @@ func (m *mdnsRegistry) Deregister(service *registry.Service, opts ...registry.De
299298
return nil
300299
}
301300

302-
func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([]*registry.Service, error) {
301+
func (m *mdnsRegistry) GetService(service string, opts ...GetOption) ([]*Service, error) {
303302
logger := m.opts.Logger
304-
serviceMap := make(map[string]*registry.Service)
303+
serviceMap := make(map[string]*Service)
305304
entries := make(chan *mdns.ServiceEntry, 10)
306305
done := make(chan bool)
307306

@@ -341,7 +340,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
341340

342341
s, ok := serviceMap[txt.Version]
343342
if !ok {
344-
s = &registry.Service{
343+
s = &Service{
345344
Name: txt.Service,
346345
Version: txt.Version,
347346
Endpoints: txt.Endpoints,
@@ -358,7 +357,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
358357
logger.Logf(log.InfoLevel, "[mdns]: invalid endpoint received: %v", e)
359358
continue
360359
}
361-
s.Nodes = append(s.Nodes, &registry.Node{
360+
s.Nodes = append(s.Nodes, &Node{
362361
Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
363362
Address: addr,
364363
Metadata: txt.Metadata,
@@ -381,7 +380,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
381380
<-done
382381

383382
// create list and return
384-
services := make([]*registry.Service, 0, len(serviceMap))
383+
services := make([]*Service, 0, len(serviceMap))
385384

386385
for _, service := range serviceMap {
387386
services = append(services, service)
@@ -390,7 +389,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
390389
return services, nil
391390
}
392391

393-
func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Service, error) {
392+
func (m *mdnsRegistry) ListServices(opts ...ListOption) ([]*Service, error) {
394393
serviceMap := make(map[string]bool)
395394
entries := make(chan *mdns.ServiceEntry, 10)
396395
done := make(chan bool)
@@ -405,7 +404,7 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
405404
// set domain
406405
p.Domain = m.domain
407406

408-
var services []*registry.Service
407+
var services []*Service
409408

410409
go func() {
411410
for {
@@ -420,7 +419,7 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
420419
name := strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+".")
421420
if !serviceMap[name] {
422421
serviceMap[name] = true
423-
services = append(services, &registry.Service{Name: name})
422+
services = append(services, &Service{Name: name})
424423
}
425424
case <-p.Context.Done():
426425
close(done)
@@ -440,8 +439,8 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
440439
return services, nil
441440
}
442441

443-
func (m *mdnsRegistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
444-
var wo registry.WatchOptions
442+
func (m *mdnsRegistry) Watch(opts ...WatchOption) (Watcher, error) {
443+
var wo WatchOptions
445444
for _, o := range opts {
446445
o(&wo)
447446
}
@@ -538,7 +537,7 @@ func (m *mdnsRegistry) String() string {
538537
return "mdns"
539538
}
540539

541-
func (m *mdnsWatcher) Next() (*registry.Result, error) {
540+
func (m *mdnsWatcher) Next() (*Result, error) {
542541
for {
543542
select {
544543
case e := <-m.ch:
@@ -563,7 +562,7 @@ func (m *mdnsWatcher) Next() (*registry.Result, error) {
563562
action = "create"
564563
}
565564

566-
service := &registry.Service{
565+
service := &Service{
567566
Name: txt.Service,
568567
Version: txt.Version,
569568
Endpoints: txt.Endpoints,
@@ -584,18 +583,18 @@ func (m *mdnsWatcher) Next() (*registry.Result, error) {
584583
addr = e.Addr.String()
585584
}
586585

587-
service.Nodes = append(service.Nodes, &registry.Node{
586+
service.Nodes = append(service.Nodes, &Node{
588587
Id: strings.TrimSuffix(e.Name, suffix),
589588
Address: addr,
590589
Metadata: txt.Metadata,
591590
})
592591

593-
return &registry.Result{
592+
return &Result{
594593
Action: action,
595594
Service: service,
596595
}, nil
597596
case <-m.exit:
598-
return nil, registry.ErrWatcherStopped
597+
return nil, ErrWatcherStopped
599598
}
600599
}
601600
}
@@ -607,13 +606,14 @@ func (m *mdnsWatcher) Stop() {
607606
default:
608607
close(m.exit)
609608
// remove self from the registry
609+
610610
m.registry.mtx.Lock()
611611
delete(m.registry.watchers, m.id)
612612
m.registry.mtx.Unlock()
613613
}
614614
}
615615

616616
// NewRegistry returns a new default registry which is mdns.
617-
func NewMDNSRegistry(opts ...registry.Option) registry.Registry {
617+
func NewMDNSRegistry(opts ...Option) Registry {
618618
return newRegistry(opts...)
619619
}

0 commit comments

Comments
 (0)