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
This commit is contained in:
@@ -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...)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
type etcdBuilder struct {
|
||||
discovBuilder
|
||||
@@ -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,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import "math/rand"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package resolver
|
||||
package internal
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
11
zrpc/resolver/register.go
Normal file
11
zrpc/resolver/register.go
Normal file
@@ -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 (
|
||||
"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,4 +1,4 @@
|
||||
package internal
|
||||
package resolver
|
||||
|
||||
import (
|
||||
"testing"
|
||||
Reference in New Issue
Block a user