Ver código fonte

feat: support third party orm to interact with go-zero (#1286)

* fixes #987

* chore: fix test failure

* chore: add comments

* feat: support third party orm to interact with go-zero

* chore: refactor
Kevin Wan 3 anos atrás
pai
commit
9d528dddd6

+ 3 - 17
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...)

+ 20 - 0
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

+ 2 - 2
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 (

+ 1 - 1
zrpc/internal/resolver/directbuilder.go → zrpc/resolver/internal/directbuilder.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"strings"

+ 1 - 1
zrpc/internal/resolver/directbuilder_test.go → zrpc/resolver/internal/directbuilder_test.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"fmt"

+ 1 - 1
zrpc/internal/resolver/discovbuilder.go → zrpc/resolver/internal/discovbuilder.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"strings"

+ 1 - 1
zrpc/internal/resolver/discovbuilder_test.go → zrpc/resolver/internal/discovbuilder_test.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"testing"

+ 1 - 1
zrpc/internal/resolver/etcdbuilder.go → zrpc/resolver/internal/etcdbuilder.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 type etcdBuilder struct {
 	discovBuilder

+ 0 - 0
zrpc/internal/resolver/kube/deploy/clusterrole.yaml → zrpc/resolver/internal/kube/deploy/clusterrole.yaml


+ 0 - 0
zrpc/internal/resolver/kube/deploy/clusterrolebinding.yaml → zrpc/resolver/internal/kube/deploy/clusterrolebinding.yaml


+ 0 - 0
zrpc/internal/resolver/kube/deploy/serviceaccount.yaml → zrpc/resolver/internal/kube/deploy/serviceaccount.yaml


+ 0 - 0
zrpc/internal/resolver/kube/eventhandler.go → zrpc/resolver/internal/kube/eventhandler.go


+ 0 - 0
zrpc/internal/resolver/kube/eventhandler_test.go → zrpc/resolver/internal/kube/eventhandler_test.go


+ 0 - 0
zrpc/internal/resolver/kube/targetparser.go → zrpc/resolver/internal/kube/targetparser.go


+ 0 - 0
zrpc/internal/resolver/kube/targetparser_test.go → zrpc/resolver/internal/kube/targetparser_test.go


+ 2 - 2
zrpc/internal/resolver/kubebuilder.go → 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"

+ 1 - 1
zrpc/internal/resolver/kubebuilder_test.go → zrpc/resolver/internal/kubebuilder_test.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"testing"

+ 1 - 1
zrpc/internal/resolver/resolver.go → zrpc/resolver/internal/resolver.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"fmt"

+ 1 - 1
zrpc/internal/resolver/resolver_test.go → zrpc/resolver/internal/resolver_test.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"testing"

+ 1 - 1
zrpc/internal/resolver/subset.go → zrpc/resolver/internal/subset.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import "math/rand"
 

+ 1 - 1
zrpc/internal/resolver/subset_test.go → zrpc/resolver/internal/subset_test.go

@@ -1,4 +1,4 @@
-package resolver
+package internal
 
 import (
 	"strconv"

+ 11 - 0
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()
+}

+ 6 - 6
zrpc/internal/target.go → 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)
 }

+ 1 - 1
zrpc/internal/target_test.go → zrpc/resolver/target_test.go

@@ -1,4 +1,4 @@
-package internal
+package resolver
 
 import (
 	"testing"