diff --git a/README.md b/README.md index fe34131..bad57c5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ ### Server #### Basic Usage -``` + +```go import ( ... "github.com/cloudwego/kitex/pkg/rpcinfo" @@ -43,14 +44,15 @@ check.DeregisterCriticalServiceAfter = "1m" you can also use `WithCheck` to modify your service check setting -``` +```go import ( ... consul "github.com/kitex-contrib/registry-consul" consulapi "github.com/hashicorp/consul/api" ) + func main() { - ... + ... r, err := consul.NewConsulRegister("127.0.0.1:8500", consul.WithCheck(&consulapi.AgentServiceCheck{ Interval: "7s", Timeout: "5s", @@ -70,14 +72,15 @@ config.Scheme = "http" you can also use `NewConsulRegisterWithConfig` to modify your config -``` +```go import ( ... consul "github.com/kitex-contrib/registry-consul" consulapi "github.com/hashicorp/consul/api" ) + func main() { - ... + ... consulConfig := consulapi.Config{ Address: "127.0.0.1:8500", Scheme: "https" @@ -91,7 +94,7 @@ func main() { #### Basic Usage -``` +```go import ( ... "github.com/cloudwego/kitex/client" @@ -124,14 +127,15 @@ config.Scheme = "http" you can also use `NewConsulResolverWithConfig` to modify your config -``` +```go import ( ... consul "github.com/kitex-contrib/registry-consul" consulapi "github.com/hashicorp/consul/api" ) + func main() { - ... + ... consulConfig := consulapi.Config{ Address: "127.0.0.1:8500", Scheme: "https" @@ -143,7 +147,7 @@ func main() { ## Example -See Server and Client in example/basic or example/custom-config. +See Server and Client in [example/basic](https://github.com/kitex-contrib/registry-consul/tree/main/example/basic) or [example/custom-config](https://github.com/kitex-contrib/registry-consul/tree/main/example/custom-config). ## Compatibility diff --git a/consul_registry.go b/consul_registry.go index 4353181..b1bc5dc 100644 --- a/consul_registry.go +++ b/consul_registry.go @@ -44,6 +44,7 @@ var errIllegalTagChar = errors.New("illegal tag character") type Option func(o *options) // WithCheck is consul registry option to set AgentServiceCheck. +// If disable consul check, set the check option to nil. func WithCheck(check *api.AgentServiceCheck) Option { return func(o *options) { o.check = check } } @@ -86,6 +87,19 @@ func NewConsulRegisterWithConfig(config *api.Config, opts ...Option) (*consulReg return &consulRegistry{consulClient: client, opts: op}, nil } +// NewConsulRegisterWithClient create a new registry using consul, with client. +func NewConsulRegisterWithClient(client *api.Client, opts ...Option) (*consulRegistry, error) { + op := options{ + check: defaultCheck(), + } + + for _, option := range opts { + option(&op) + } + + return &consulRegistry{consulClient: client, opts: op}, nil +} + // Register register a service to consul. // Note: the tag map of the service can not contain the `:` character. func (c *consulRegistry) Register(info *registry.Info) error { diff --git a/consul_test.go b/consul_test.go index 9a39559..261ac72 100644 --- a/consul_test.go +++ b/consul_test.go @@ -89,6 +89,16 @@ func TestNewConsulRegisterWithConfig(t *testing.T) { assert.NoError(t, err) } +// TestNewConsulRegisterWithConfig tests the NewConsulRegisterWithConfig function. +func TestNewConsulRegisterWithClient(t *testing.T) { + _, err := NewConsulRegisterWithClient(consulClient, WithCheck(&consulapi.AgentServiceCheck{ + Interval: "7s", + Timeout: "5s", + DeregisterCriticalServiceAfter: "15s", + })) + assert.NoError(t, err) +} + // TestNewConsulResolver tests the NewConsulResolver function. func TestNewConsulResolver(t *testing.T) { _, err := NewConsulResolver(consulAddr)