From 0ff5dfd08dd83acf9a8f4f0c312360b6347b9a35 Mon Sep 17 00:00:00 2001 From: L2ncE Date: Wed, 29 Mar 2023 14:51:51 +0800 Subject: [PATCH 1/3] feat: add option for `NewConsulRegisterWithConfig` --- consul_registry.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/consul_registry.go b/consul_registry.go index 8df20db..ffb5168 100644 --- a/consul_registry.go +++ b/consul_registry.go @@ -62,13 +62,21 @@ func NewConsulRegister(address string, opts ...Option) (registry.Registry, error } // NewConsulRegisterWithConfig create a new registry using consul, with a custom config. -func NewConsulRegisterWithConfig(config *api.Config) (*consulRegistry, error) { +func NewConsulRegisterWithConfig(config *api.Config, opts ...Option) (*consulRegistry, error) { client, err := api.NewClient(config) if err != nil { return nil, err } - return &consulRegistry{consulClient: client}, nil + op := options{ + check: defaultCheck(), + } + + for _, option := range opts { + option(&op) + } + + return &consulRegistry{consulClient: client, opts: op}, nil } // Register register a service to consul. From 2074905e899d09aa8164690387f6b9f60c20f350 Mon Sep 17 00:00:00 2001 From: L2ncE Date: Sat, 1 Apr 2023 15:33:07 +0800 Subject: [PATCH 2/3] feat: add test & example for `NewConsulRegisterWithConfig` --- consul_test.go | 6 +++++- example/custom-config/server/main.go | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/consul_test.go b/consul_test.go index 5f096be..e96f4bf 100644 --- a/consul_test.go +++ b/consul_test.go @@ -79,7 +79,11 @@ func TestNewConsulRegisterWithConfig(t *testing.T) { Address: consulAddr, WaitTime: 5 * time.Second, Namespace: "TEST-NS", - }) + }, WithCheck(&consulapi.AgentServiceCheck{ + Interval: "7s", + Timeout: "5s", + DeregisterCriticalServiceAfter: "15s", + })) assert.NoError(t, err) } diff --git a/example/custom-config/server/main.go b/example/custom-config/server/main.go index 5d552aa..63759b9 100644 --- a/example/custom-config/server/main.go +++ b/example/custom-config/server/main.go @@ -18,12 +18,11 @@ import ( "context" "log" - consulapi "github.com/hashicorp/consul/api" - "github.com/cloudwego/kitex-examples/hello/kitex_gen/api" "github.com/cloudwego/kitex-examples/hello/kitex_gen/api/hello" "github.com/cloudwego/kitex/pkg/registry" "github.com/cloudwego/kitex/server" + consulapi "github.com/hashicorp/consul/api" consul "github.com/kitex-contrib/registry-consul" ) @@ -41,7 +40,11 @@ func main() { Address: "127.0.0.1:8500", Token: "TEST-MY-TOKEN", } - r, err := consul.NewConsulRegisterWithConfig(&consulConfig) + r, err := consul.NewConsulRegisterWithConfig(&consulConfig, consul.WithCheck(&consulapi.AgentServiceCheck{ + Interval: "7s", + Timeout: "5s", + DeregisterCriticalServiceAfter: "15s", + })) if err != nil { log.Fatal(err) } From e3af90f9a7cfbdcf6c6321dfa1b453d295c65554 Mon Sep 17 00:00:00 2001 From: Xinhao Yuan <92938836+L2ncE@users.noreply.github.com> Date: Tue, 4 Apr 2023 20:24:21 +0800 Subject: [PATCH 3/3] style: improve import --- example/custom-config/server/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/example/custom-config/server/main.go b/example/custom-config/server/main.go index c1d9dbd..a244a43 100644 --- a/example/custom-config/server/main.go +++ b/example/custom-config/server/main.go @@ -20,12 +20,13 @@ import ( "context" "log" - "github.com/cloudwego/kitex-examples/hello/kitex_gen/api" - "github.com/cloudwego/kitex-examples/hello/kitex_gen/api/hello" + consulapi "github.com/hashicorp/consul/api" + "github.com/cloudwego/kitex/pkg/registry" "github.com/cloudwego/kitex/server" - consulapi "github.com/hashicorp/consul/api" consul "github.com/kitex-contrib/registry-consul" + "github.com/kitex-contrib/registry-consul/example/hello/kitex_gen/api" + "github.com/kitex-contrib/registry-consul/example/hello/kitex_gen/api/hello" ) type HelloImpl struct{}