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
master v1.2.4
Kevin Wan 3 years ago committed by GitHub
parent 543d590710
commit 9d528dddd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,7 +4,6 @@ import (
"log" "log"
"time" "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"
"github.com/tal-tech/go-zero/zrpc/internal/auth" "github.com/tal-tech/go-zero/zrpc/internal/auth"
"github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors" "github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors"
@ -64,24 +63,11 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
opts = append(opts, options...) opts = append(opts, options...)
var target string target, err := c.BuildTarget()
var err error if err != nil {
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 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)
}
client, err := internal.NewClient(target, opts...) client, err := internal.NewClient(target, opts...)
if err != nil { if err != nil {
return nil, err return nil, err

@ -4,6 +4,7 @@ import (
"github.com/tal-tech/go-zero/core/discov" "github.com/tal-tech/go-zero/core/discov"
"github.com/tal-tech/go-zero/core/service" "github.com/tal-tech/go-zero/core/service"
"github.com/tal-tech/go-zero/core/stores/redis" "github.com/tal-tech/go-zero/core/stores/redis"
"github.com/tal-tech/go-zero/zrpc/resolver"
) )
type ( type (
@ -67,6 +68,25 @@ func (sc RpcServerConf) Validate() error {
return sc.Redis.Validate() 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. // HasCredential checks if there is a credential in config.
func (cc RpcClientConf) HasCredential() bool { func (cc RpcClientConf) HasCredential() bool {
return len(cc.App) > 0 && len(cc.Token) > 0 return len(cc.App) > 0 && len(cc.Token) > 0

@ -9,7 +9,7 @@ import (
"github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c" "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/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"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
) )
@ -20,7 +20,7 @@ const (
) )
func init() { func init() {
resolver.RegisterResolver() resolver.Register()
} }
type ( type (

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

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

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

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

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

@ -1,4 +1,4 @@
package resolver package internal
import ( import (
"context" "context"
@ -8,7 +8,7 @@ import (
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/proc" "github.com/tal-tech/go-zero/core/proc"
"github.com/tal-tech/go-zero/core/threading" "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" "google.golang.org/grpc/resolver"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers" "k8s.io/client-go/informers"

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

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

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

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

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

@ -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()
}

@ -1,20 +1,20 @@
package internal package resolver
import ( import (
"fmt" "fmt"
"strings" "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. // BuildDirectTarget returns a string that represents the given endpoints with direct schema.
func BuildDirectTarget(endpoints []string) string { func BuildDirectTarget(endpoints []string) string {
return fmt.Sprintf("%s:///%s", resolver.DirectScheme, return fmt.Sprintf("%s:///%s", internal.DirectScheme,
strings.Join(endpoints, resolver.EndpointSep)) strings.Join(endpoints, internal.EndpointSep))
} }
// BuildDiscovTarget returns a string that represents the given endpoints with discov schema. // BuildDiscovTarget returns a string that represents the given endpoints with discov schema.
func BuildDiscovTarget(endpoints []string, key string) string { func BuildDiscovTarget(endpoints []string, key string) string {
return fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme, return fmt.Sprintf("%s://%s/%s", internal.DiscovScheme,
strings.Join(endpoints, resolver.EndpointSep), key) strings.Join(endpoints, internal.EndpointSep), key)
} }

@ -1,4 +1,4 @@
package internal package resolver
import ( import (
"testing" "testing"
Loading…
Cancel
Save