feat: registry.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
etcdKratos "github.com/go-kratos/kratos/contrib/registry/etcd/v2"
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
|
||||
etcdClient "go.etcd.io/etcd/client/v3"
|
||||
@@ -10,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
// NewRegistry 创建一个注册发现客户端 - Etcd
|
||||
func NewRegistry(c *conf.Registry) *etcdKratos.Registry {
|
||||
func NewRegistry(c *conf.Registry) *Registry {
|
||||
if c == nil || c.Etcd == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -25,7 +24,7 @@ func NewRegistry(c *conf.Registry) *etcdKratos.Registry {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
reg := etcdKratos.New(cli)
|
||||
reg := New(cli)
|
||||
|
||||
return reg
|
||||
}
|
||||
42
registry/etcd/go.mod
Normal file
42
registry/etcd/go.mod
Normal file
@@ -0,0 +1,42 @@
|
||||
module github.com/tx7do/kratos-bootstrap/registry/etcd
|
||||
|
||||
go 1.24.0
|
||||
|
||||
toolchain go1.24.3
|
||||
|
||||
replace (
|
||||
github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.4.1
|
||||
|
||||
github.com/tx7do/kratos-bootstrap/api => ../../api
|
||||
github.com/tx7do/kratos-bootstrap/registry => ../registry
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-kratos/kratos/v2 v2.8.4
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/tx7do/kratos-bootstrap/api v0.0.20
|
||||
go.etcd.io/etcd/client/v3 v3.6.0
|
||||
google.golang.org/grpc v1.72.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/coreos/go-semver v0.3.1 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.6.0 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.6.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.uber.org/zap v1.27.0 // indirect
|
||||
golang.org/x/net v0.40.0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
golang.org/x/text v0.25.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250528174236-200df99c418a // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a // indirect
|
||||
google.golang.org/protobuf v1.36.6 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
107
registry/etcd/go.sum
Normal file
107
registry/etcd/go.sum
Normal file
@@ -0,0 +1,107 @@
|
||||
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
|
||||
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-kratos/kratos/v2 v2.8.4 h1:eIJLE9Qq9WSoKx+Buy2uPyrahtF/lPh+Xf4MTpxhmjs=
|
||||
github.com/go-kratos/kratos/v2 v2.8.4/go.mod h1:mq62W2101a5uYyRxe+7IdWubu7gZCGYqSNKwGFiiRcw=
|
||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
go.etcd.io/etcd/api/v3 v3.6.0 h1:vdbkcUBGLf1vfopoGE/uS3Nv0KPyIpUV/HM6w9yx2kM=
|
||||
go.etcd.io/etcd/api/v3 v3.6.0/go.mod h1:Wt5yZqEmxgTNJGHob7mTVBJDZNXiHPtXTcPab37iFOw=
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.6.0 h1:nchnPqpuxvv3UuGGHaz0DQKYi5EIW5wOYsgUNRc365k=
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.6.0/go.mod h1:Jv5SFWMnGvIBn8o3OaBq/PnT0jjsX8iNokAUessNjoA=
|
||||
go.etcd.io/etcd/client/v3 v3.6.0 h1:/yjKzD+HW5v/3DVj9tpwFxzNbu8hjcKID183ug9duWk=
|
||||
go.etcd.io/etcd/client/v3 v3.6.0/go.mod h1:Jzk/Knqe06pkOZPHXsQ0+vNDvMQrgIqJ0W8DwPdMJMg=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
|
||||
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
|
||||
go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI=
|
||||
go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ=
|
||||
go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE=
|
||||
go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A=
|
||||
go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w=
|
||||
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
|
||||
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
|
||||
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
|
||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
|
||||
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250528174236-200df99c418a h1:SGktgSolFCo75dnHJF2yMvnns6jCmHFJ0vE4Vn2JKvQ=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250528174236-200df99c418a/go.mod h1:a77HrdMjoeKbnd2jmgcWdaS++ZLZAEq3orIOAEIKiVw=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a h1:v2PbRU4K3llS09c7zodFpNePeamkAwG3mPrAery9VeE=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A=
|
||||
google.golang.org/grpc v1.72.2 h1:TdbGzwb82ty4OusHWepvFWGLgIbNo1/SUynEN0ssqv8=
|
||||
google.golang.org/grpc v1.72.2/go.mod h1:wH5Aktxcg25y1I3w7H69nHfXdOG3UiadoBtjh3izSDM=
|
||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
35
registry/etcd/options.go
Normal file
35
registry/etcd/options.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Option is etcd registry option.
|
||||
type Option func(o *options)
|
||||
|
||||
type options struct {
|
||||
ctx context.Context
|
||||
namespace string
|
||||
ttl time.Duration
|
||||
maxRetry int
|
||||
}
|
||||
|
||||
// Context with registry context.
|
||||
func Context(ctx context.Context) Option {
|
||||
return func(o *options) { o.ctx = ctx }
|
||||
}
|
||||
|
||||
// Namespace with registry namespace.
|
||||
func Namespace(ns string) Option {
|
||||
return func(o *options) { o.namespace = ns }
|
||||
}
|
||||
|
||||
// RegisterTTL with register ttl.
|
||||
func RegisterTTL(ttl time.Duration) Option {
|
||||
return func(o *options) { o.ttl = ttl }
|
||||
}
|
||||
|
||||
func MaxRetry(num int) Option {
|
||||
return func(o *options) { o.maxRetry = num }
|
||||
}
|
||||
184
registry/etcd/registry.go
Normal file
184
registry/etcd/registry.go
Normal file
@@ -0,0 +1,184 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
var (
|
||||
_ registry.Registrar = (*Registry)(nil)
|
||||
_ registry.Discovery = (*Registry)(nil)
|
||||
)
|
||||
|
||||
// Registry is etcd registry.
|
||||
type Registry struct {
|
||||
opts *options
|
||||
client *clientv3.Client
|
||||
kv clientv3.KV
|
||||
lease clientv3.Lease
|
||||
}
|
||||
|
||||
// New creates etcd registry
|
||||
func New(client *clientv3.Client, opts ...Option) (r *Registry) {
|
||||
op := &options{
|
||||
ctx: context.Background(),
|
||||
namespace: "/microservices",
|
||||
ttl: time.Second * 15,
|
||||
maxRetry: 5,
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(op)
|
||||
}
|
||||
return &Registry{
|
||||
opts: op,
|
||||
client: client,
|
||||
kv: clientv3.NewKV(client),
|
||||
}
|
||||
}
|
||||
|
||||
// Register the registration.
|
||||
func (r *Registry) Register(ctx context.Context, service *registry.ServiceInstance) error {
|
||||
key := fmt.Sprintf("%s/%s/%s", r.opts.namespace, service.Name, service.ID)
|
||||
value, err := marshal(service)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r.lease != nil {
|
||||
r.lease.Close()
|
||||
}
|
||||
r.lease = clientv3.NewLease(r.client)
|
||||
leaseID, err := r.registerWithKV(ctx, key, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go r.heartBeat(r.opts.ctx, leaseID, key, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deregister the registration.
|
||||
func (r *Registry) Deregister(ctx context.Context, service *registry.ServiceInstance) error {
|
||||
defer func() {
|
||||
if r.lease != nil {
|
||||
r.lease.Close()
|
||||
}
|
||||
}()
|
||||
key := fmt.Sprintf("%s/%s/%s", r.opts.namespace, service.Name, service.ID)
|
||||
_, err := r.client.Delete(ctx, key)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetService return the service instances in memory according to the service name.
|
||||
func (r *Registry) GetService(ctx context.Context, name string) ([]*registry.ServiceInstance, error) {
|
||||
key := fmt.Sprintf("%s/%s", r.opts.namespace, name)
|
||||
resp, err := r.kv.Get(ctx, key, clientv3.WithPrefix())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]*registry.ServiceInstance, 0, len(resp.Kvs))
|
||||
for _, kv := range resp.Kvs {
|
||||
si, err := unmarshal(kv.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if si.Name != name {
|
||||
continue
|
||||
}
|
||||
items = append(items, si)
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// Watch creates a watcher according to the service name.
|
||||
func (r *Registry) Watch(ctx context.Context, name string) (registry.Watcher, error) {
|
||||
key := fmt.Sprintf("%s/%s", r.opts.namespace, name)
|
||||
return newWatcher(ctx, key, name, r.client)
|
||||
}
|
||||
|
||||
// registerWithKV create a new lease, return current leaseID
|
||||
func (r *Registry) registerWithKV(ctx context.Context, key string, value string) (clientv3.LeaseID, error) {
|
||||
grant, err := r.lease.Grant(ctx, int64(r.opts.ttl.Seconds()))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
_, err = r.client.Put(ctx, key, value, clientv3.WithLease(grant.ID))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return grant.ID, nil
|
||||
}
|
||||
|
||||
func (r *Registry) heartBeat(ctx context.Context, leaseID clientv3.LeaseID, key string, value string) {
|
||||
curLeaseID := leaseID
|
||||
kac, err := r.client.KeepAlive(ctx, leaseID)
|
||||
if err != nil {
|
||||
curLeaseID = 0
|
||||
}
|
||||
rand.Seed(time.Now().Unix())
|
||||
|
||||
for {
|
||||
if curLeaseID == 0 {
|
||||
// try to registerWithKV
|
||||
var retreat []int
|
||||
for retryCnt := 0; retryCnt < r.opts.maxRetry; retryCnt++ {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
// prevent infinite blocking
|
||||
idChan := make(chan clientv3.LeaseID, 1)
|
||||
errChan := make(chan error, 1)
|
||||
cancelCtx, cancel := context.WithCancel(ctx)
|
||||
go func() {
|
||||
defer cancel()
|
||||
id, registerErr := r.registerWithKV(cancelCtx, key, value)
|
||||
if registerErr != nil {
|
||||
errChan <- registerErr
|
||||
} else {
|
||||
idChan <- id
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(3 * time.Second):
|
||||
cancel()
|
||||
continue
|
||||
case <-errChan:
|
||||
continue
|
||||
case curLeaseID = <-idChan:
|
||||
}
|
||||
|
||||
kac, err = r.client.KeepAlive(ctx, curLeaseID)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
retreat = append(retreat, 1<<retryCnt)
|
||||
time.Sleep(time.Duration(retreat[rand.Intn(len(retreat))]) * time.Second)
|
||||
}
|
||||
if _, ok := <-kac; !ok {
|
||||
// retry failed
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
case _, ok := <-kac:
|
||||
if !ok {
|
||||
if ctx.Err() != nil {
|
||||
// channel closed due to context cancel
|
||||
return
|
||||
}
|
||||
// need to retry registration
|
||||
curLeaseID = 0
|
||||
continue
|
||||
}
|
||||
case <-r.opts.ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
154
registry/etcd/registry_test.go
Normal file
154
registry/etcd/registry_test.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
func TestRegistry(t *testing.T) {
|
||||
client, err := clientv3.New(clientv3.Config{
|
||||
Endpoints: []string{"127.0.0.1:2379"},
|
||||
DialTimeout: time.Second, DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
s := ®istry.ServiceInstance{
|
||||
ID: "0",
|
||||
Name: "helloworld",
|
||||
}
|
||||
|
||||
r := New(client)
|
||||
w, err := r.Watch(ctx, s.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
_ = w.Stop()
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
res, err1 := w.Next()
|
||||
if err1 != nil {
|
||||
return
|
||||
}
|
||||
t.Logf("watch: %d", len(res))
|
||||
for _, r := range res {
|
||||
t.Logf("next: %+v", r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Second)
|
||||
|
||||
if err1 := r.Register(ctx, s); err1 != nil {
|
||||
t.Fatal(err1)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
|
||||
res, err := r.GetService(ctx, s.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res) != 1 && res[0].Name != s.Name {
|
||||
t.Errorf("not expected: %+v", res)
|
||||
}
|
||||
|
||||
if err1 := r.Deregister(ctx, s); err1 != nil {
|
||||
t.Fatal(err1)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
|
||||
res, err = r.GetService(ctx, s.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res) != 0 {
|
||||
t.Errorf("not expected empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeartBeat(t *testing.T) {
|
||||
client, err := clientv3.New(clientv3.Config{
|
||||
Endpoints: []string{"127.0.0.1:2379"},
|
||||
DialTimeout: time.Second, DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
s := ®istry.ServiceInstance{
|
||||
ID: "0",
|
||||
Name: "helloworld",
|
||||
}
|
||||
|
||||
go func() {
|
||||
r := New(client)
|
||||
w, err1 := r.Watch(ctx, s.Name)
|
||||
if err1 != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = w.Stop()
|
||||
}()
|
||||
for {
|
||||
res, err2 := w.Next()
|
||||
if err2 != nil {
|
||||
return
|
||||
}
|
||||
t.Logf("watch: %d", len(res))
|
||||
for _, r := range res {
|
||||
t.Logf("next: %+v", r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// new a server
|
||||
r := New(client,
|
||||
RegisterTTL(2*time.Second),
|
||||
MaxRetry(5),
|
||||
)
|
||||
|
||||
key := fmt.Sprintf("%s/%s/%s", r.opts.namespace, s.Name, s.ID)
|
||||
value, _ := marshal(s)
|
||||
r.lease = clientv3.NewLease(r.client)
|
||||
leaseID, err := r.registerWithKV(ctx, key, value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// wait for lease expired
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
res, err := r.GetService(ctx, s.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res) != 0 {
|
||||
t.Errorf("not expected empty")
|
||||
}
|
||||
|
||||
go r.heartBeat(ctx, leaseID, key, value)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
res, err = r.GetService(ctx, s.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res) == 0 {
|
||||
t.Errorf("reconnect failed")
|
||||
}
|
||||
}
|
||||
20
registry/etcd/service.go
Normal file
20
registry/etcd/service.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
func marshal(si *registry.ServiceInstance) (string, error) {
|
||||
data, err := json.Marshal(si)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func unmarshal(data []byte) (si *registry.ServiceInstance, err error) {
|
||||
err = json.Unmarshal(data, &si)
|
||||
return
|
||||
}
|
||||
95
registry/etcd/watcher.go
Normal file
95
registry/etcd/watcher.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
var _ registry.Watcher = (*watcher)(nil)
|
||||
|
||||
type watcher struct {
|
||||
key string
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
client *clientv3.Client
|
||||
watchChan clientv3.WatchChan
|
||||
watcher clientv3.Watcher
|
||||
kv clientv3.KV
|
||||
first bool
|
||||
serviceName string
|
||||
}
|
||||
|
||||
func newWatcher(ctx context.Context, key, name string, client *clientv3.Client) (*watcher, error) {
|
||||
w := &watcher{
|
||||
key: key,
|
||||
client: client,
|
||||
watcher: clientv3.NewWatcher(client),
|
||||
kv: clientv3.NewKV(client),
|
||||
first: true,
|
||||
serviceName: name,
|
||||
}
|
||||
w.ctx, w.cancel = context.WithCancel(ctx)
|
||||
w.watchChan = w.watcher.Watch(w.ctx, key, clientv3.WithPrefix(), clientv3.WithRev(0), clientv3.WithKeysOnly())
|
||||
err := w.watcher.RequestProgress(w.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (w *watcher) Next() ([]*registry.ServiceInstance, error) {
|
||||
if w.first {
|
||||
item, err := w.getInstance()
|
||||
w.first = false
|
||||
return item, err
|
||||
}
|
||||
|
||||
select {
|
||||
case <-w.ctx.Done():
|
||||
return nil, w.ctx.Err()
|
||||
case watchResp, ok := <-w.watchChan:
|
||||
if !ok || watchResp.Err() != nil {
|
||||
time.Sleep(time.Second)
|
||||
err := w.reWatch()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return w.getInstance()
|
||||
}
|
||||
}
|
||||
|
||||
func (w *watcher) Stop() error {
|
||||
w.cancel()
|
||||
return w.watcher.Close()
|
||||
}
|
||||
|
||||
func (w *watcher) getInstance() ([]*registry.ServiceInstance, error) {
|
||||
resp, err := w.kv.Get(w.ctx, w.key, clientv3.WithPrefix())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]*registry.ServiceInstance, 0, len(resp.Kvs))
|
||||
for _, kv := range resp.Kvs {
|
||||
si, err := unmarshal(kv.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if si.Name != w.serviceName {
|
||||
continue
|
||||
}
|
||||
items = append(items, si)
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (w *watcher) reWatch() error {
|
||||
w.watcher.Close()
|
||||
w.watcher = clientv3.NewWatcher(w.client)
|
||||
w.watchChan = w.watcher.Watch(w.ctx, w.key, clientv3.WithPrefix(), clientv3.WithRev(0), clientv3.WithKeysOnly())
|
||||
return w.watcher.RequestProgress(w.ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user