Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

追加instanceid,解决k8s内环境变量注册时实例覆盖问题 #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@ package driver
import (
"context"
"fmt"
"net/url"
"os"
"strings"
"time"

"github.com/dtm-labs/dtmdriver"
"github.com/go-kratos/kratos/contrib/polaris/v2"
consul "github.com/go-kratos/kratos/contrib/registry/consul/v2"
etcd "github.com/go-kratos/kratos/contrib/registry/etcd/v2"
"github.com/go-kratos/kratos/v2/registry"
_ "github.com/go-kratos/kratos/v2/transport/grpc/resolver/direct"
"github.com/go-kratos/kratos/v2/transport/grpc/resolver/discovery"
"github.com/google/uuid"
consulAPI "github.com/hashicorp/consul/api"
polarisAPI "github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
etcdAPI "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc/resolver"
"net/url"
"strings"
)

const (
DriverName = "dtm-driver-kratos"
DriverName = "dtm-driver-kratos2"
DefaultScheme = "discovery"
EtcdScheme = "etcd"
ConsulScheme = "consul"
PolarisScheme = "polaris"
)

type kratosDriver struct{}
Expand All @@ -42,13 +50,18 @@ func (k *kratosDriver) RegisterService(target string, endpoint string) error {
if err != nil {
return err
}

// -- @# 从环境变量获取pod ip然后组合为endpoint
theEndPoint := "grpc://" + os.Getenv("POD_IP") + ":36790"

switch u.Scheme {
case DefaultScheme:
fallthrough
case EtcdScheme:
registerInstance := &registry.ServiceInstance{
ID: uuid.New().String(),
Name: strings.TrimPrefix(u.Path, "/"),
Endpoints: strings.Split(endpoint, ","),
Endpoints: []string{theEndPoint},
}
client, err := etcdAPI.New(etcdAPI.Config{
Endpoints: strings.Split(u.Host, ","),
Expand All @@ -63,14 +76,44 @@ func (k *kratosDriver) RegisterService(target string, endpoint string) error {

case ConsulScheme:
registerInstance := &registry.ServiceInstance{
ID: uuid.New().String(),
Name: strings.TrimPrefix(u.Path, "/"),
Endpoints: strings.Split(endpoint, ","),
Endpoints: []string{theEndPoint},
}
client, err := consulAPI.NewClient(&consulAPI.Config{Address: u.Host})
if err != nil {
return err
}
registry := consul.New(client)
//add resolver so that dtm can handle discovery://
resolver.Register(discovery.NewBuilder(registry, discovery.WithInsecure(true)))
return registry.Register(context.Background(), registerInstance)
case PolarisScheme:
registerInstance := &registry.ServiceInstance{
ID: uuid.New().String(),
Name: strings.TrimPrefix(u.Path, "/"),
Endpoints: []string{theEndPoint},
}

polarisCfg, err := config.LoadConfigurationByFile("./polaris.yaml")
if nil != err {
panic(err)
}

sdkctx, err := polarisAPI.InitContextByConfig(polarisCfg)
if nil != err {
panic(err)
}
client := polaris.New(sdkctx, polaris.WithNamespace("go"))
registry := client.Registry(
polaris.WithRegistryTimeout(time.Second),
polaris.WithRegistryHealthy(true),
polaris.WithRegistryIsolate(false),
polaris.WithRegistryRetryCount(3),
polaris.WithRegistryWeight(100),
polaris.WithRegistryTTL(3),
)

//add resolver so that dtm can handle discovery://
resolver.Register(discovery.NewBuilder(registry, discovery.WithInsecure(true)))
return registry.Register(context.Background(), registerInstance)
Expand Down
13 changes: 6 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module github.com/dtm-labs/dtmdriver-kratos
module github.com/fulltimelink/dtmdriver-kratos

go 1.15

require (
github.com/dtm-labs/dtmdriver v0.0.6
github.com/go-kratos/kratos/contrib/polaris/v2 v2.0.0-20240918015945-e1f5dc42b1e5
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20220414054820-d0b704b8f38d
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20220301040457-03ad2b663624
github.com/go-kratos/kratos/v2 v2.2.1
github.com/go-kratos/kratos/v2 v2.8.0
github.com/google/uuid v1.4.0
github.com/hashicorp/consul/api v1.12.0
github.com/polarismesh/polaris-go v1.3.0
go.etcd.io/etcd/client/v3 v3.5.2
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
google.golang.org/genproto v0.0.0-20220228195345-15d65a4533f7 // indirect
google.golang.org/grpc v1.48.0
google.golang.org/grpc v1.61.1
)
Loading