mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
readd the resolver
This commit is contained in:
parent
2b3a87a212
commit
adb6760e21
30
network/resolver/dns/dns.go
Normal file
30
network/resolver/dns/dns.go
Normal file
@ -0,0 +1,30 @@
|
||||
// Package dns resolves ids to dns srv records
|
||||
package dns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/micro/go-micro/network/resolver"
|
||||
)
|
||||
|
||||
type Resolver struct{}
|
||||
|
||||
// Resolve assumes ID is a domain name e.g micro.mu
|
||||
func (r *Resolver) Resolve(id string) ([]*resolver.Record, error) {
|
||||
_, addrs, err := net.LookupSRV("network", "udp", id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var records []*resolver.Record
|
||||
for _, addr := range addrs {
|
||||
address := addr.Target
|
||||
if addr.Port > 0 {
|
||||
address = fmt.Sprintf("%s:%d", addr.Target, addr.Port)
|
||||
}
|
||||
records = append(records, &resolver.Record{
|
||||
Address: address,
|
||||
})
|
||||
}
|
||||
return records, nil
|
||||
}
|
59
network/resolver/http/http.go
Normal file
59
network/resolver/http/http.go
Normal file
@ -0,0 +1,59 @@
|
||||
// Package http resolves ids to network addresses using a http request
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/micro/go-micro/network/resolver"
|
||||
)
|
||||
|
||||
type Resolver struct {
|
||||
// If not set, defaults to http
|
||||
Proto string
|
||||
|
||||
// Path sets the path to lookup. Defaults to /network
|
||||
Path string
|
||||
}
|
||||
|
||||
// Resolve assumes ID is a domain which can be converted to a http://id/network request
|
||||
func (r *Resolver) Resolve(id string) ([]*resolver.Record, error) {
|
||||
proto := "http"
|
||||
path := "/network"
|
||||
|
||||
if len(r.Proto) > 0 {
|
||||
proto = r.Proto
|
||||
}
|
||||
|
||||
if len(r.Path) > 0 {
|
||||
path = r.Path
|
||||
}
|
||||
|
||||
uri := &url.URL{
|
||||
Scheme: proto,
|
||||
Path: path,
|
||||
Host: id,
|
||||
}
|
||||
|
||||
rsp, err := http.Get(uri.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
|
||||
b, err := ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// encoding format is assumed to be json
|
||||
var records []*resolver.Record
|
||||
|
||||
if err := json.Unmarshal(b, &records); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
37
network/resolver/registry/registry.go
Normal file
37
network/resolver/registry/registry.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Package registry resolves ids using the go-micro registry
|
||||
package registry
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/network/resolver"
|
||||
"github.com/micro/go-micro/registry"
|
||||
)
|
||||
|
||||
type Resolver struct {
|
||||
// Registry is the registry to use otherwise we use the defaul
|
||||
Registry registry.Registry
|
||||
}
|
||||
|
||||
// Resolve assumes ID is a domain name e.g micro.mu
|
||||
func (r *Resolver) Resolve(id string) ([]*resolver.Record, error) {
|
||||
reg := r.Registry
|
||||
if reg == nil {
|
||||
reg = registry.DefaultRegistry
|
||||
}
|
||||
|
||||
services, err := reg.GetService(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var records []*resolver.Record
|
||||
|
||||
for _, service := range services {
|
||||
for _, node := range service.Nodes {
|
||||
records = append(records, &resolver.Record{
|
||||
Address: node.Address,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
15
network/resolver/resolver.go
Normal file
15
network/resolver/resolver.go
Normal file
@ -0,0 +1,15 @@
|
||||
// Package resolver resolves network ids to addresses
|
||||
package resolver
|
||||
|
||||
// Resolver is network resolver. It's used to find network nodes
|
||||
// via id to connect to. This is done based on Network.Id().
|
||||
// Before we can be part of any network, we have to connect to it.
|
||||
type Resolver interface {
|
||||
// Resolve returns a list of addresses for an id
|
||||
Resolve(id string) ([]*Record, error)
|
||||
}
|
||||
|
||||
// A resolved record
|
||||
type Record struct {
|
||||
Address string `json:"address"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user