mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-30 08:06:40 +02:00
Merge pull request #744 from micro/mdns-domain
Use .micro domain for mdns
This commit is contained in:
commit
e828a099c5
@ -2,6 +2,8 @@
|
|||||||
package mdns
|
package mdns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,3 +11,13 @@ import (
|
|||||||
func NewRegistry(opts ...registry.Option) registry.Registry {
|
func NewRegistry(opts ...registry.Option) registry.Registry {
|
||||||
return registry.NewRegistry(opts...)
|
return registry.NewRegistry(opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Domain sets the mdnsDomain
|
||||||
|
func Domain(d string) registry.Option {
|
||||||
|
return func(o *registry.Options) {
|
||||||
|
if o.Context == nil {
|
||||||
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, "mdns.domain", d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -14,6 +14,11 @@ import (
|
|||||||
hash "github.com/mitchellh/hashstructure"
|
hash "github.com/mitchellh/hashstructure"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// use a .micro domain rather than .local
|
||||||
|
mdnsDomain = "micro"
|
||||||
|
)
|
||||||
|
|
||||||
type mdnsTxt struct {
|
type mdnsTxt struct {
|
||||||
Service string
|
Service string
|
||||||
Version string
|
Version string
|
||||||
@ -29,6 +34,8 @@ type mdnsEntry struct {
|
|||||||
|
|
||||||
type mdnsRegistry struct {
|
type mdnsRegistry struct {
|
||||||
opts Options
|
opts Options
|
||||||
|
// the mdns domain
|
||||||
|
domain string
|
||||||
|
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
services map[string][]*mdnsEntry
|
services map[string][]*mdnsEntry
|
||||||
@ -36,11 +43,25 @@ type mdnsRegistry struct {
|
|||||||
|
|
||||||
func newRegistry(opts ...Option) Registry {
|
func newRegistry(opts ...Option) Registry {
|
||||||
options := Options{
|
options := Options{
|
||||||
|
Context: context.Background(),
|
||||||
Timeout: time.Millisecond * 100,
|
Timeout: time.Millisecond * 100,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the domain
|
||||||
|
domain := mdnsDomain
|
||||||
|
|
||||||
|
d, ok := options.Context.Value("mdns.domain").(string)
|
||||||
|
if ok {
|
||||||
|
domain = d
|
||||||
|
}
|
||||||
|
|
||||||
return &mdnsRegistry{
|
return &mdnsRegistry{
|
||||||
opts: options,
|
opts: options,
|
||||||
|
domain: domain,
|
||||||
services: make(map[string][]*mdnsEntry),
|
services: make(map[string][]*mdnsEntry),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,7 +87,7 @@ func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error
|
|||||||
s, err := mdns.NewMDNSService(
|
s, err := mdns.NewMDNSService(
|
||||||
service.Name,
|
service.Name,
|
||||||
"_services",
|
"_services",
|
||||||
"",
|
m.domain+".",
|
||||||
"",
|
"",
|
||||||
9999,
|
9999,
|
||||||
[]net.IP{net.ParseIP("0.0.0.0")},
|
[]net.IP{net.ParseIP("0.0.0.0")},
|
||||||
@ -141,7 +162,7 @@ func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error
|
|||||||
s, err := mdns.NewMDNSService(
|
s, err := mdns.NewMDNSService(
|
||||||
node.Id,
|
node.Id,
|
||||||
service.Name,
|
service.Name,
|
||||||
"",
|
m.domain+".",
|
||||||
"",
|
"",
|
||||||
port,
|
port,
|
||||||
[]net.IP{net.ParseIP(host)},
|
[]net.IP{net.ParseIP(host)},
|
||||||
@ -214,6 +235,8 @@ func (m *mdnsRegistry) GetService(service string) ([]*Service, error) {
|
|||||||
p.Context, _ = context.WithTimeout(context.Background(), m.opts.Timeout)
|
p.Context, _ = context.WithTimeout(context.Background(), m.opts.Timeout)
|
||||||
// set entries channel
|
// set entries channel
|
||||||
p.Entries = entries
|
p.Entries = entries
|
||||||
|
// set the domain
|
||||||
|
p.Domain = m.domain
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@ -223,7 +246,9 @@ func (m *mdnsRegistry) GetService(service string) ([]*Service, error) {
|
|||||||
if p.Service == "_services" {
|
if p.Service == "_services" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if p.Domain != m.domain {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if e.TTL == 0 {
|
if e.TTL == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -288,6 +313,8 @@ func (m *mdnsRegistry) ListServices() ([]*Service, error) {
|
|||||||
p.Context, _ = context.WithTimeout(context.Background(), m.opts.Timeout)
|
p.Context, _ = context.WithTimeout(context.Background(), m.opts.Timeout)
|
||||||
// set entries channel
|
// set entries channel
|
||||||
p.Entries = entries
|
p.Entries = entries
|
||||||
|
// set domain
|
||||||
|
p.Domain = m.domain
|
||||||
|
|
||||||
var services []*Service
|
var services []*Service
|
||||||
|
|
||||||
@ -298,7 +325,9 @@ func (m *mdnsRegistry) ListServices() ([]*Service, error) {
|
|||||||
if e.TTL == 0 {
|
if e.TTL == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if !strings.HasSuffix(e.Name, p.Domain+".") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
name := strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+".")
|
name := strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+".")
|
||||||
if !serviceMap[name] {
|
if !serviceMap[name] {
|
||||||
serviceMap[name] = true
|
serviceMap[name] = true
|
||||||
@ -329,9 +358,10 @@ func (m *mdnsRegistry) Watch(opts ...WatchOption) (Watcher, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
md := &mdnsWatcher{
|
md := &mdnsWatcher{
|
||||||
wo: wo,
|
wo: wo,
|
||||||
ch: make(chan *mdns.ServiceEntry, 32),
|
ch: make(chan *mdns.ServiceEntry, 32),
|
||||||
exit: make(chan struct{}),
|
exit: make(chan struct{}),
|
||||||
|
domain: m.domain,
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -11,6 +11,8 @@ type mdnsWatcher struct {
|
|||||||
wo WatchOptions
|
wo WatchOptions
|
||||||
ch chan *mdns.ServiceEntry
|
ch chan *mdns.ServiceEntry
|
||||||
exit chan struct{}
|
exit chan struct{}
|
||||||
|
// the mdns domain
|
||||||
|
domain string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mdnsWatcher) Next() (*Result, error) {
|
func (m *mdnsWatcher) Next() (*Result, error) {
|
||||||
@ -46,13 +48,14 @@ func (m *mdnsWatcher) Next() (*Result, error) {
|
|||||||
Endpoints: txt.Endpoints,
|
Endpoints: txt.Endpoints,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: don't hardcode .local.
|
// skip anything without the domain we care about
|
||||||
if !strings.HasSuffix(e.Name, "."+service.Name+".local.") {
|
suffix := fmt.Sprintf(".%s.%s.", service.Name, m.domain)
|
||||||
|
if !strings.HasSuffix(e.Name, suffix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
service.Nodes = append(service.Nodes, &Node{
|
service.Nodes = append(service.Nodes, &Node{
|
||||||
Id: strings.TrimSuffix(e.Name, "."+service.Name+".local."),
|
Id: strings.TrimSuffix(e.Name, suffix),
|
||||||
Address: fmt.Sprintf("%s:%d", e.AddrV4.String(), e.Port),
|
Address: fmt.Sprintf("%s:%d", e.AddrV4.String(), e.Port),
|
||||||
Metadata: txt.Metadata,
|
Metadata: txt.Metadata,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user