diff --git a/client/client.go b/client/client.go index de558e3..5e4a5fc 100644 --- a/client/client.go +++ b/client/client.go @@ -36,8 +36,14 @@ func New(opts ...Option) (Client, error) { if !c.opts.secure { c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithInsecure()) } - c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name) - if c.opts.version != "" { + if c.opts.addr == "" { + c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name) + } 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) } return c, nil diff --git a/client/options.go b/client/options.go index 6850dbc..5854207 100644 --- a/client/options.go +++ b/client/options.go @@ -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 { return func(o *options) { o.tlsConfig = conf @@ -58,6 +64,7 @@ type options struct { registry registry.Registry name string version string + addr string tlsConfig *tls.Config secure bool dialOptions []grpc.DialOption diff --git a/example/example.go b/example/example.go index af1c614..4a048be 100644 --- a/example/example.go +++ b/example/example.go @@ -18,7 +18,6 @@ import ( metrics2 "go.linka.cloud/grpc/interceptors/metrics" validation2 "go.linka.cloud/grpc/interceptors/validation" "go.linka.cloud/grpc/logger" - "go.linka.cloud/grpc/registry/mdns" "go.linka.cloud/grpc/service" ) @@ -61,6 +60,7 @@ func httpLogger(next http.Handler) http.Handler { func main() { name := "greeter" + version := "v0.0.1" secure := true ctx, cancel := context.WithCancel(context.Background()) done := make(chan struct{}) @@ -75,9 +75,9 @@ func main() { svc, err = service.New( service.WithContext(ctx), service.WithName(name), - service.WithVersion("v0.0.1"), + service.WithVersion(version), service.WithAddress(address), - service.WithRegistry(mdns.NewRegistry()), + // service.WithRegistry(mdns.NewRegistry()), service.WithReflection(true), service.WithSecure(secure), service.WithAfterStart(func() error { @@ -108,9 +108,10 @@ func main() { }() <-ready s, err := client.New( - client.WithName("greeter"), - client.WithVersion("v0.0.1"), - client.WithRegistry(mdns.NewRegistry()), + // client.WithName(name), + // client.WithVersion(version), + client.WithAddress("localhost:9991"), + // client.WithRegistry(mdns.NewRegistry()), client.WithSecure(secure), ) if err != nil { diff --git a/registry/noop/registry.go b/registry/noop/registry.go index c4a2c49..c18b277 100644 --- a/registry/noop/registry.go +++ b/registry/noop/registry.go @@ -1,6 +1,8 @@ package noop import ( + "errors" + "google.golang.org/grpc/resolver" "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) { - return noopWatcher{}, nil + return nil, errors.New("watch not supported") } func (n noop) String() string { return "passthroug" } - -type noopWatcher struct{} - -func (n noopWatcher) Next() (*registry.Result, error) { - return ®istry.Result{}, nil -} - -func (n noopWatcher) Stop() {} diff --git a/resolver/resolver.go b/resolver/resolver.go index ea5374c..c7991c2 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -34,7 +34,7 @@ type resolvr struct { } func (r *resolvr) run() { - if r.reg.String() == "noop" { + if r.reg.String() == "passthroug" { return } var name, version string @@ -68,9 +68,9 @@ func (r *resolvr) run() { } // TODO(adphi): implement switch res.Action { - case "create": + case registry.Create.String(): - case "delete": + case registry.Delete.String(): } r.cc.UpdateState(resolver.State{Addresses: r.addrs})