Default to in-memory registry in single-binary mode
This avoids various issues of the old "mdns" default. At least for the simple single process setup (#3134, #4597). When starting the services individually we still default to "mdns".
This commit is contained in:
committed by
Ralf Haferkamp
parent
01650a5023
commit
8f2bf0ed9c
@@ -3,52 +3,77 @@ package registry
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
consulr "github.com/go-micro/plugins/v4/registry/consul"
|
||||
etcdr "github.com/go-micro/plugins/v4/registry/etcd"
|
||||
kubernetesr "github.com/go-micro/plugins/v4/registry/kubernetes"
|
||||
mdnsr "github.com/go-micro/plugins/v4/registry/mdns"
|
||||
memr "github.com/go-micro/plugins/v4/registry/memory"
|
||||
natsr "github.com/go-micro/plugins/v4/registry/nats"
|
||||
|
||||
"go-micro.dev/v4/registry"
|
||||
"go-micro.dev/v4/registry/cache"
|
||||
)
|
||||
|
||||
var (
|
||||
const (
|
||||
registryEnv = "MICRO_REGISTRY"
|
||||
registryAddressEnv = "MICRO_REGISTRY_ADDRESS"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
regPlugin string
|
||||
reg registry.Registry
|
||||
)
|
||||
|
||||
func Configure(plugin string) {
|
||||
if reg == nil {
|
||||
regPlugin = plugin
|
||||
}
|
||||
}
|
||||
|
||||
// GetRegistry returns a configured micro registry based on Micro env vars.
|
||||
// It defaults to mDNS, so mind that systems with mDNS disabled by default (i.e SUSE) will have a hard time
|
||||
// and it needs to explicitly use etcd. Os awareness for providing a working registry out of the box should be done.
|
||||
func GetRegistry() registry.Registry {
|
||||
addresses := strings.Split(os.Getenv(registryAddressEnv), ",")
|
||||
|
||||
var r registry.Registry
|
||||
switch os.Getenv(registryEnv) {
|
||||
case "nats":
|
||||
r = natsr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
case "kubernetes":
|
||||
r = kubernetesr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
case "etcd":
|
||||
r = etcdr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
case "consul":
|
||||
r = consulr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
default:
|
||||
r = mdnsr.NewRegistry()
|
||||
}
|
||||
once.Do(func() {
|
||||
addresses := strings.Split(os.Getenv(registryAddressEnv), ",")
|
||||
// prefer env of setting from Configure()
|
||||
plugin := os.Getenv(registryEnv)
|
||||
if plugin == "" {
|
||||
plugin = regPlugin
|
||||
}
|
||||
|
||||
switch plugin {
|
||||
case "nats":
|
||||
reg = natsr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
case "kubernetes":
|
||||
reg = kubernetesr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
case "etcd":
|
||||
reg = etcdr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
case "consul":
|
||||
reg = consulr.NewRegistry(
|
||||
registry.Addrs(addresses...),
|
||||
)
|
||||
case "memory":
|
||||
reg = memr.NewRegistry()
|
||||
default:
|
||||
reg = mdnsr.NewRegistry()
|
||||
}
|
||||
// No cache needed for in-memory registry
|
||||
if plugin == "memory" {
|
||||
reg = cache.New(reg, cache.WithTTL(20*time.Second))
|
||||
}
|
||||
})
|
||||
// always use cached registry to prevent registry
|
||||
// lookup for every request
|
||||
return cache.New(r, cache.WithTTL(20*time.Second))
|
||||
return reg
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user