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: add addition info for consul #86

Merged
merged 3 commits into from
Sep 22, 2023
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: 17 additions & 5 deletions consul/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func main() {
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
Expand All @@ -61,17 +60,19 @@ func main() {
}
```

#### Customize Service Check
#### Customize

registry has a default config for service check as below
Consul extension provides custom configuration of service inspection and `Tags` `Meta` field.

Registry has a default config for service check as below

```
check.Timeout = "5s"
check.Interval = "5s"
check.DeregisterCriticalServiceAfter = "1m"
```

you can also use `WithCheck` to modify your config
You can use `WithCheck` to modify the configuration of the service check. At the same time, you can also use `WithAdditionInfo` to modify the `Meta` `Tags` field of the service.

```golang
package main
Expand All @@ -98,7 +99,18 @@ func main() {
check.Timeout = "10s"
check.Interval = "10s"
check.DeregisterCriticalServiceAfter = "1m"
r := consul.NewConsulRegister(consulClient, consul.WithCheck(check))

// custom addition info
additionInfo := &consul.AdditionInfo{
Tags: []string{"tag1", "tag2"},
Meta: map[string]string{
"meta1": "val1",
"meta2": "val2",
},
}
r := consul.NewConsulRegister(consulClient,
consul.WithCheck(check), consul.WithAdditionInfo(additionInfo),
)
}

```
Expand Down
21 changes: 16 additions & 5 deletions consul/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func main() {
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
Expand All @@ -61,7 +60,9 @@ func main() {
}
```

#### 自定义服务检查
#### 自定义配置

Consul 拓展提供了服务检查与 `Tags` `Meta` 字段的自定义配置。

注册中心默认配置服务检查,如下:

Expand All @@ -71,7 +72,7 @@ check.Interval = "5s"
check.DeregisterCriticalServiceAfter = "1m"
```

你也可以使用`WithCheck`来修改配置
你也可以使用`WithCheck`来修改服务检查的配置。同时,你也可以使用`WithAdditionInfo`来修改服务的`Meta` `Tags` 字段。

```golang
package main
Expand All @@ -98,9 +99,19 @@ func main() {
check.Timeout = "10s"
check.Interval = "10s"
check.DeregisterCriticalServiceAfter = "1m"
r := consul.NewConsulRegister(consulClient, consul.WithCheck(check))

// custom addition info
additionInfo := &consul.AdditionInfo{
Tags: []string{"tag1", "tag2"},
Meta: map[string]string{
"meta1": "val1",
"meta2": "val2",
},
}
r := consul.NewConsulRegister(consulClient,
consul.WithCheck(check), consul.WithAdditionInfo(additionInfo),
)
}

```

### 客户端
Expand Down
25 changes: 17 additions & 8 deletions consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestConsulPrepared(t *testing.T) {

// TestNewConsulRegister tests the NewConsulRegister function.
func TestNewConsulRegister(t *testing.T) {
t.Parallel()
config := consulapi.DefaultConfig()
config.Address = consulAddr
cli, err := consulapi.NewClient(config)
Expand All @@ -107,6 +108,7 @@ func TestNewConsulRegister(t *testing.T) {

// TestNewConsulRegisterWithCheckOption tests the NewConsulRegister function with check option.
func TestNewConsulRegisterWithCheckOption(t *testing.T) {
t.Parallel()
config := consulapi.DefaultConfig()
config.Address = consulAddr
cli, err := consulapi.NewClient(config)
Expand All @@ -126,6 +128,7 @@ func TestNewConsulRegisterWithCheckOption(t *testing.T) {

// TestNewConsulResolver tests the NewConsulResolver function .
func TestNewConsulResolver(t *testing.T) {
t.Parallel()
config := consulapi.DefaultConfig()
config.Address = consulAddr
cli, err := consulapi.NewClient(config)
Expand All @@ -140,6 +143,7 @@ func TestNewConsulResolver(t *testing.T) {

// TestConsulRegister tests the Register function with Hertz.
func TestConsulRegister(t *testing.T) {
t.Parallel()
config := consulapi.DefaultConfig()
config.Address = consulAddr
consulClient, err := consulapi.NewClient(config)
Expand All @@ -148,26 +152,28 @@ func TestConsulRegister(t *testing.T) {
return
}

info := &AdditionInfo{
Tags: []string{"tag1", "tag2", "tag3"},
Meta: map[string]string{
"meta1": "value1",
"meta2": "value2",
},
}

var (
testSvcName = "hertz.test.demo1"
testSvcPort = fmt.Sprintf("%d", 8581)
testSvcAddr = net.JoinHostPort(localIpAddr, testSvcPort)
testSvcWeight = 777
metaList = map[string]string{
"k1": "vv1",
"k2": "vv2",
"k3": "vv3",
}
)

r := NewConsulRegister(consulClient)
r := NewConsulRegister(consulClient, WithAdditionInfo(info))
h := server.Default(
server.WithHostPorts(testSvcAddr),
server.WithRegistry(r, &registry.Info{
ServiceName: testSvcName,
Addr: utils.NewNetAddr("tcp", testSvcAddr),
Weight: testSvcWeight,
Tags: metaList,
}),
)

Expand All @@ -187,12 +193,14 @@ func TestConsulRegister(t *testing.T) {
assert.Equal(t, testSvcName, gotSvc.Service)
assert.Equal(t, testSvcAddr, net.JoinHostPort(gotSvc.Address, fmt.Sprintf("%d", gotSvc.Port)))
assert.Equal(t, testSvcWeight, gotSvc.Weights.Passing)
assert.Equal(t, metaList, gotSvc.Meta)
assert.Equal(t, info.Tags, gotSvc.Tags)
assert.Equal(t, info.Meta, gotSvc.Meta)
}
}

// TestConsulDiscovery tests the ConsulDiscovery function with Hertz.
func TestConsulDiscovery(t *testing.T) {
t.Parallel()
consulConfig := consulapi.DefaultConfig()
consulConfig.Address = consulAddr
consulClient, err := consulapi.NewClient(consulConfig)
Expand Down Expand Up @@ -248,6 +256,7 @@ func TestConsulDiscovery(t *testing.T) {

// TestConsulDeregister tests the Deregister function with Hertz
func TestConsulDeregister(t *testing.T) {
t.Parallel()
consulConfig := consulapi.DefaultConfig()
consulConfig.Address = consulAddr
consulClient, err := consulapi.NewClient(consulConfig)
Expand Down
18 changes: 11 additions & 7 deletions consul/example/custom-config/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ func main() {
Timeout: "5s",
DeregisterCriticalServiceAfter: "15s",
}
r := consul.NewConsulRegister(consulClient, consul.WithCheck(check))
// custom addition info
additionInfo := &consul.AdditionInfo{
Tags: []string{"tag1", "tag2"},
Meta: map[string]string{
"meta1": "val1",
"meta2": "val2",
},
}
r := consul.NewConsulRegister(consulClient,
consul.WithCheck(check), consul.WithAdditionInfo(additionInfo),
)

wg.Add(2)
go func() {
Expand All @@ -67,9 +77,6 @@ func main() {
ServiceName: "custom-config-demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: map[string]string{
"key1": "val1",
},
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
Expand All @@ -95,9 +102,6 @@ func main() {
ServiceName: "custom-config-demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: map[string]string{
"key2": "val2",
},
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
Expand Down
16 changes: 14 additions & 2 deletions consul/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ type consulRegistry struct {
opts options
}

type AdditionInfo struct {
Tags []string
Meta map[string]string
}

var _ registry.Registry = (*consulRegistry)(nil)

type options struct {
check *api.AgentServiceCheck
check *api.AgentServiceCheck
AdditionInfo AdditionInfo
}

// Option is the option of Consul.
Expand All @@ -54,6 +60,11 @@ func WithCheck(check *api.AgentServiceCheck) Option {
return func(o *options) { o.check = check }
}

// WithAdditionInfo is consul registry option to set AdditionInfo.
func WithAdditionInfo(info *AdditionInfo) Option {
return func(o *options) { o.AdditionInfo = *info }
}

// NewConsulRegister create a new registry using consul.
func NewConsulRegister(consulClient *api.Client, opts ...Option) registry.Registry {
op := options{
Expand Down Expand Up @@ -88,7 +99,8 @@ func (c *consulRegistry) Register(info *registry.Info) error {
Name: info.ServiceName,
Address: host,
Port: port,
Meta: info.Tags,
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
Tags: c.opts.AdditionInfo.Tags,
Meta: c.opts.AdditionInfo.Meta,
Weights: &api.AgentWeights{
Passing: info.Weight,
Warning: info.Weight,
Expand Down