Skip to content

Commit

Permalink
feat: support third party orm to interact with go-zero (#1286)
Browse files Browse the repository at this point in the history
* fixes #987

* chore: fix test failure

* chore: add comments

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

* chore: refactor
  • Loading branch information
kevwan authored Dec 1, 2021
1 parent 543d590 commit 9d528dd
Show file tree
Hide file tree
Showing 24 changed files with 55 additions and 38 deletions.
20 changes: 3 additions & 17 deletions zrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...)
Expand Down
20 changes: 20 additions & 0 deletions zrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions zrpc/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -20,7 +20,7 @@ const (
)

func init() {
resolver.RegisterResolver()
resolver.Register()
}

type (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"strings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"strings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

type etcdBuilder struct {
discovBuilder
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"context"
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import "math/rand"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package internal

import (
"strconv"
Expand Down
11 changes: 11 additions & 0 deletions zrpc/resolver/register.go
Original file line number Diff line number Diff line change
@@ -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()
}
12 changes: 6 additions & 6 deletions zrpc/internal/target.go → zrpc/resolver/target.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package resolver

import (
"testing"
Expand Down

0 comments on commit 9d528dd

Please sign in to comment.