add client address option, fix noop resolver

This commit is contained in:
Adphi 2021-11-22 14:04:47 +01:00
parent c6557d12f2
commit e8f0a2f03d
5 changed files with 28 additions and 20 deletions

View File

@ -36,8 +36,14 @@ func New(opts ...Option) (Client, error) {
if !c.opts.secure { if !c.opts.secure {
c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithInsecure()) c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithInsecure())
} }
if c.opts.addr == "" {
c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name) c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name)
if c.opts.version != "" { } else if strings.HasPrefix(c.opts.addr, "tcp://"){
c.addr = strings.Replace(c.opts.addr, "tcp://", "", 1)
} else {
c.addr = c.opts.addr
}
if c.opts.version != "" && c.opts.addr == "" {
c.addr = c.addr + ":" + strings.TrimSpace(c.opts.version) c.addr = c.addr + ":" + strings.TrimSpace(c.opts.version)
} }
return c, nil return c, nil

View File

@ -36,6 +36,12 @@ func WithVersion(version string) Option {
} }
} }
func WithAddress(address string) Option {
return func(o *options) {
o.addr = address
}
}
func WithTLSConfig(conf *tls.Config) Option { func WithTLSConfig(conf *tls.Config) Option {
return func(o *options) { return func(o *options) {
o.tlsConfig = conf o.tlsConfig = conf
@ -58,6 +64,7 @@ type options struct {
registry registry.Registry registry registry.Registry
name string name string
version string version string
addr string
tlsConfig *tls.Config tlsConfig *tls.Config
secure bool secure bool
dialOptions []grpc.DialOption dialOptions []grpc.DialOption

View File

@ -18,7 +18,6 @@ import (
metrics2 "go.linka.cloud/grpc/interceptors/metrics" metrics2 "go.linka.cloud/grpc/interceptors/metrics"
validation2 "go.linka.cloud/grpc/interceptors/validation" validation2 "go.linka.cloud/grpc/interceptors/validation"
"go.linka.cloud/grpc/logger" "go.linka.cloud/grpc/logger"
"go.linka.cloud/grpc/registry/mdns"
"go.linka.cloud/grpc/service" "go.linka.cloud/grpc/service"
) )
@ -61,6 +60,7 @@ func httpLogger(next http.Handler) http.Handler {
func main() { func main() {
name := "greeter" name := "greeter"
version := "v0.0.1"
secure := true secure := true
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{}) done := make(chan struct{})
@ -75,9 +75,9 @@ func main() {
svc, err = service.New( svc, err = service.New(
service.WithContext(ctx), service.WithContext(ctx),
service.WithName(name), service.WithName(name),
service.WithVersion("v0.0.1"), service.WithVersion(version),
service.WithAddress(address), service.WithAddress(address),
service.WithRegistry(mdns.NewRegistry()), // service.WithRegistry(mdns.NewRegistry()),
service.WithReflection(true), service.WithReflection(true),
service.WithSecure(secure), service.WithSecure(secure),
service.WithAfterStart(func() error { service.WithAfterStart(func() error {
@ -108,9 +108,10 @@ func main() {
}() }()
<-ready <-ready
s, err := client.New( s, err := client.New(
client.WithName("greeter"), // client.WithName(name),
client.WithVersion("v0.0.1"), // client.WithVersion(version),
client.WithRegistry(mdns.NewRegistry()), client.WithAddress("localhost:9991"),
// client.WithRegistry(mdns.NewRegistry()),
client.WithSecure(secure), client.WithSecure(secure),
) )
if err != nil { if err != nil {

View File

@ -1,6 +1,8 @@
package noop package noop
import ( import (
"errors"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
"go.linka.cloud/grpc/registry" "go.linka.cloud/grpc/registry"
@ -42,17 +44,9 @@ func (n noop) ListServices(option ...registry.ListOption) ([]*registry.Service,
} }
func (n noop) Watch(option ...registry.WatchOption) (registry.Watcher, error) { func (n noop) Watch(option ...registry.WatchOption) (registry.Watcher, error) {
return noopWatcher{}, nil return nil, errors.New("watch not supported")
} }
func (n noop) String() string { func (n noop) String() string {
return "passthroug" return "passthroug"
} }
type noopWatcher struct{}
func (n noopWatcher) Next() (*registry.Result, error) {
return &registry.Result{}, nil
}
func (n noopWatcher) Stop() {}

View File

@ -34,7 +34,7 @@ type resolvr struct {
} }
func (r *resolvr) run() { func (r *resolvr) run() {
if r.reg.String() == "noop" { if r.reg.String() == "passthroug" {
return return
} }
var name, version string var name, version string
@ -68,9 +68,9 @@ func (r *resolvr) run() {
} }
// TODO(adphi): implement // TODO(adphi): implement
switch res.Action { switch res.Action {
case "create": case registry.Create.String():
case "delete": case registry.Delete.String():
} }
r.cc.UpdateState(resolver.State{Addresses: r.addrs}) r.cc.UpdateState(resolver.State{Addresses: r.addrs})