Skip to content

Commit

Permalink
feat: register with consul client
Browse files Browse the repository at this point in the history
  • Loading branch information
rfyiamcool committed Nov 27, 2024
1 parent 8dbf48a commit 743128b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
### Server

#### Basic Usage
```

```go
import (
...
"github.com/cloudwego/kitex/pkg/rpcinfo"
Expand Down Expand Up @@ -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",
Expand All @@ -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"
Expand All @@ -91,7 +94,7 @@ func main() {

#### Basic Usage

```
```go
import (
...
"github.com/cloudwego/kitex/client"
Expand Down Expand Up @@ -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"
Expand All @@ -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

Expand Down
14 changes: 14 additions & 0 deletions consul_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 743128b

Please sign in to comment.