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

feat: register with consul client #10

Merged
merged 1 commit into from
Dec 1, 2024
Merged
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
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
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.