Skip to content

Commit

Permalink
feat: register with consul client
Browse files Browse the repository at this point in the history
Signed-off-by: rfyiamcool <[email protected]>
  • Loading branch information
GuangmingLuo authored and rfyiamcool committed Nov 27, 2024
2 parents 2905e45 + 3657177 commit d66e480
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 237 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
58 changes: 55 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
module github.com/kitex-contrib/registry-consul

go 1.16
go 1.18

require (
github.com/apache/thrift v0.13.0
github.com/cloudwego/kitex v0.9.1
github.com/cloudwego/kitex v0.11.3
github.com/hashicorp/consul/api v1.20.0
github.com/hashicorp/go-hclog v1.3.1 // indirect
github.com/stretchr/testify v1.9.0
)

require (
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/bytedance/gopkg v0.1.1 // indirect
github.com/bytedance/sonic v1.12.2 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/configmanager v0.2.2 // indirect
github.com/cloudwego/dynamicgo v0.4.0 // indirect
github.com/cloudwego/fastpb v0.0.5 // indirect
github.com/cloudwego/frugal v0.2.0 // indirect
github.com/cloudwego/gopkg v0.1.2 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cloudwego/localsession v0.0.2 // indirect
github.com/cloudwego/netpoll v0.6.4 // indirect
github.com/cloudwego/runtimex v0.1.0 // indirect
github.com/cloudwego/thriftgo v0.3.17 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/gjson v1.17.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
Loading

0 comments on commit d66e480

Please sign in to comment.