diff --git a/zrpc/client.go b/zrpc/client.go index 544c3234..5822eb24 100644 --- a/zrpc/client.go +++ b/zrpc/client.go @@ -4,7 +4,6 @@ import ( "log" "time" - "github.com/tal-tech/go-zero/core/discov" "github.com/tal-tech/go-zero/zrpc/internal" "github.com/tal-tech/go-zero/zrpc/internal/auth" "github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors" @@ -64,22 +63,9 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) { opts = append(opts, options...) - var target string - var err error - if len(c.Endpoints) > 0 { - target = internal.BuildDirectTarget(c.Endpoints) - } else if len(c.Target) > 0 { - target = c.Target - } else { - if err = c.Etcd.Validate(); err != nil { - return nil, err - } - - if c.Etcd.HasAccount() { - discov.RegisterAccount(c.Etcd.Hosts, c.Etcd.User, c.Etcd.Pass) - } - - target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key) + target, err := c.BuildTarget() + if err != nil { + return nil, err } client, err := internal.NewClient(target, opts...) diff --git a/zrpc/config.go b/zrpc/config.go index 2ddd6f9e..b47a7d2b 100644 --- a/zrpc/config.go +++ b/zrpc/config.go @@ -4,6 +4,7 @@ import ( "github.com/tal-tech/go-zero/core/discov" "github.com/tal-tech/go-zero/core/service" "github.com/tal-tech/go-zero/core/stores/redis" + "github.com/tal-tech/go-zero/zrpc/resolver" ) type ( @@ -67,6 +68,25 @@ func (sc RpcServerConf) Validate() error { return sc.Redis.Validate() } +// BuildTarget builds the rpc target from the given config. +func (cc RpcClientConf) BuildTarget() (string, error) { + if len(cc.Endpoints) > 0 { + return resolver.BuildDirectTarget(cc.Endpoints), nil + } else if len(cc.Target) > 0 { + return cc.Target, nil + } + + if err := cc.Etcd.Validate(); err != nil { + return "", err + } + + if cc.Etcd.HasAccount() { + discov.RegisterAccount(cc.Etcd.Hosts, cc.Etcd.User, cc.Etcd.Pass) + } + + return resolver.BuildDiscovTarget(cc.Etcd.Hosts, cc.Etcd.Key), nil +} + // HasCredential checks if there is a credential in config. func (cc RpcClientConf) HasCredential() bool { return len(cc.App) > 0 && len(cc.Token) > 0 diff --git a/zrpc/internal/client.go b/zrpc/internal/client.go index 65217003..5bad34a5 100644 --- a/zrpc/internal/client.go +++ b/zrpc/internal/client.go @@ -9,7 +9,7 @@ import ( "github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c" "github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors" - "github.com/tal-tech/go-zero/zrpc/internal/resolver" + "github.com/tal-tech/go-zero/zrpc/resolver" "google.golang.org/grpc" "google.golang.org/grpc/credentials" ) @@ -20,7 +20,7 @@ const ( ) func init() { - resolver.RegisterResolver() + resolver.Register() } type ( diff --git a/zrpc/internal/resolver/directbuilder.go b/zrpc/resolver/internal/directbuilder.go similarity index 97% rename from zrpc/internal/resolver/directbuilder.go rename to zrpc/resolver/internal/directbuilder.go index e0f4efe3..ed50c9a1 100644 --- a/zrpc/internal/resolver/directbuilder.go +++ b/zrpc/resolver/internal/directbuilder.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "strings" diff --git a/zrpc/internal/resolver/directbuilder_test.go b/zrpc/resolver/internal/directbuilder_test.go similarity index 98% rename from zrpc/internal/resolver/directbuilder_test.go rename to zrpc/resolver/internal/directbuilder_test.go index 175599e7..f19a1bbf 100644 --- a/zrpc/internal/resolver/directbuilder_test.go +++ b/zrpc/resolver/internal/directbuilder_test.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "fmt" diff --git a/zrpc/internal/resolver/discovbuilder.go b/zrpc/resolver/internal/discovbuilder.go similarity index 98% rename from zrpc/internal/resolver/discovbuilder.go rename to zrpc/resolver/internal/discovbuilder.go index 2f362e94..0131b55f 100644 --- a/zrpc/internal/resolver/discovbuilder.go +++ b/zrpc/resolver/internal/discovbuilder.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "strings" diff --git a/zrpc/internal/resolver/discovbuilder_test.go b/zrpc/resolver/internal/discovbuilder_test.go similarity index 91% rename from zrpc/internal/resolver/discovbuilder_test.go rename to zrpc/resolver/internal/discovbuilder_test.go index 042d67ec..437c99cf 100644 --- a/zrpc/internal/resolver/discovbuilder_test.go +++ b/zrpc/resolver/internal/discovbuilder_test.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "testing" diff --git a/zrpc/internal/resolver/etcdbuilder.go b/zrpc/resolver/internal/etcdbuilder.go similarity index 86% rename from zrpc/internal/resolver/etcdbuilder.go rename to zrpc/resolver/internal/etcdbuilder.go index 954e9d07..1d00dc39 100644 --- a/zrpc/internal/resolver/etcdbuilder.go +++ b/zrpc/resolver/internal/etcdbuilder.go @@ -1,4 +1,4 @@ -package resolver +package internal type etcdBuilder struct { discovBuilder diff --git a/zrpc/internal/resolver/kube/deploy/clusterrole.yaml b/zrpc/resolver/internal/kube/deploy/clusterrole.yaml similarity index 100% rename from zrpc/internal/resolver/kube/deploy/clusterrole.yaml rename to zrpc/resolver/internal/kube/deploy/clusterrole.yaml diff --git a/zrpc/internal/resolver/kube/deploy/clusterrolebinding.yaml b/zrpc/resolver/internal/kube/deploy/clusterrolebinding.yaml similarity index 100% rename from zrpc/internal/resolver/kube/deploy/clusterrolebinding.yaml rename to zrpc/resolver/internal/kube/deploy/clusterrolebinding.yaml diff --git a/zrpc/internal/resolver/kube/deploy/serviceaccount.yaml b/zrpc/resolver/internal/kube/deploy/serviceaccount.yaml similarity index 100% rename from zrpc/internal/resolver/kube/deploy/serviceaccount.yaml rename to zrpc/resolver/internal/kube/deploy/serviceaccount.yaml diff --git a/zrpc/internal/resolver/kube/eventhandler.go b/zrpc/resolver/internal/kube/eventhandler.go similarity index 100% rename from zrpc/internal/resolver/kube/eventhandler.go rename to zrpc/resolver/internal/kube/eventhandler.go diff --git a/zrpc/internal/resolver/kube/eventhandler_test.go b/zrpc/resolver/internal/kube/eventhandler_test.go similarity index 100% rename from zrpc/internal/resolver/kube/eventhandler_test.go rename to zrpc/resolver/internal/kube/eventhandler_test.go diff --git a/zrpc/internal/resolver/kube/targetparser.go b/zrpc/resolver/internal/kube/targetparser.go similarity index 100% rename from zrpc/internal/resolver/kube/targetparser.go rename to zrpc/resolver/internal/kube/targetparser.go diff --git a/zrpc/internal/resolver/kube/targetparser_test.go b/zrpc/resolver/internal/kube/targetparser_test.go similarity index 100% rename from zrpc/internal/resolver/kube/targetparser_test.go rename to zrpc/resolver/internal/kube/targetparser_test.go diff --git a/zrpc/internal/resolver/kubebuilder.go b/zrpc/resolver/internal/kubebuilder.go similarity index 95% rename from zrpc/internal/resolver/kubebuilder.go rename to zrpc/resolver/internal/kubebuilder.go index 73c0f97f..c59c7852 100644 --- a/zrpc/internal/resolver/kubebuilder.go +++ b/zrpc/resolver/internal/kubebuilder.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "context" @@ -8,7 +8,7 @@ import ( "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/proc" "github.com/tal-tech/go-zero/core/threading" - "github.com/tal-tech/go-zero/zrpc/internal/resolver/kube" + "github.com/tal-tech/go-zero/zrpc/resolver/internal/kube" "google.golang.org/grpc/resolver" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/informers" diff --git a/zrpc/internal/resolver/kubebuilder_test.go b/zrpc/resolver/internal/kubebuilder_test.go similarity index 91% rename from zrpc/internal/resolver/kubebuilder_test.go rename to zrpc/resolver/internal/kubebuilder_test.go index 3b7ce5a2..9d86a46f 100644 --- a/zrpc/internal/resolver/kubebuilder_test.go +++ b/zrpc/resolver/internal/kubebuilder_test.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "testing" diff --git a/zrpc/internal/resolver/resolver.go b/zrpc/resolver/internal/resolver.go similarity index 98% rename from zrpc/internal/resolver/resolver.go rename to zrpc/resolver/internal/resolver.go index 162e841d..37362a07 100644 --- a/zrpc/internal/resolver/resolver.go +++ b/zrpc/resolver/internal/resolver.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "fmt" diff --git a/zrpc/internal/resolver/resolver_test.go b/zrpc/resolver/internal/resolver_test.go similarity index 97% rename from zrpc/internal/resolver/resolver_test.go rename to zrpc/resolver/internal/resolver_test.go index 7ff9348d..dde4065a 100644 --- a/zrpc/internal/resolver/resolver_test.go +++ b/zrpc/resolver/internal/resolver_test.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "testing" diff --git a/zrpc/internal/resolver/subset.go b/zrpc/resolver/internal/subset.go similarity index 92% rename from zrpc/internal/resolver/subset.go rename to zrpc/resolver/internal/subset.go index 63a25fbe..ff5b5861 100644 --- a/zrpc/internal/resolver/subset.go +++ b/zrpc/resolver/internal/subset.go @@ -1,4 +1,4 @@ -package resolver +package internal import "math/rand" diff --git a/zrpc/internal/resolver/subset_test.go b/zrpc/resolver/internal/subset_test.go similarity index 98% rename from zrpc/internal/resolver/subset_test.go rename to zrpc/resolver/internal/subset_test.go index cb664cd6..2202ba72 100644 --- a/zrpc/internal/resolver/subset_test.go +++ b/zrpc/resolver/internal/subset_test.go @@ -1,4 +1,4 @@ -package resolver +package internal import ( "strconv" diff --git a/zrpc/resolver/register.go b/zrpc/resolver/register.go new file mode 100644 index 00000000..c3713044 --- /dev/null +++ b/zrpc/resolver/register.go @@ -0,0 +1,11 @@ +package resolver + +import ( + "github.com/tal-tech/go-zero/zrpc/resolver/internal" +) + +// Register registers schemes defined zrpc. +// Keep it in a separated package to let third party register manually. +func Register() { + internal.RegisterResolver() +} diff --git a/zrpc/internal/target.go b/zrpc/resolver/target.go similarity index 54% rename from zrpc/internal/target.go rename to zrpc/resolver/target.go index 25830667..783a27eb 100644 --- a/zrpc/internal/target.go +++ b/zrpc/resolver/target.go @@ -1,20 +1,20 @@ -package internal +package resolver import ( "fmt" "strings" - "github.com/tal-tech/go-zero/zrpc/internal/resolver" + "github.com/tal-tech/go-zero/zrpc/resolver/internal" ) // BuildDirectTarget returns a string that represents the given endpoints with direct schema. func BuildDirectTarget(endpoints []string) string { - return fmt.Sprintf("%s:///%s", resolver.DirectScheme, - strings.Join(endpoints, resolver.EndpointSep)) + return fmt.Sprintf("%s:///%s", internal.DirectScheme, + strings.Join(endpoints, internal.EndpointSep)) } // BuildDiscovTarget returns a string that represents the given endpoints with discov schema. func BuildDiscovTarget(endpoints []string, key string) string { - return fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme, - strings.Join(endpoints, resolver.EndpointSep), key) + return fmt.Sprintf("%s://%s/%s", internal.DiscovScheme, + strings.Join(endpoints, internal.EndpointSep), key) } diff --git a/zrpc/internal/target_test.go b/zrpc/resolver/target_test.go similarity index 96% rename from zrpc/internal/target_test.go rename to zrpc/resolver/target_test.go index a07b3a8e..4dd50191 100644 --- a/zrpc/internal/target_test.go +++ b/zrpc/resolver/target_test.go @@ -1,4 +1,4 @@ -package internal +package resolver import ( "testing"