Skip to content

Commit

Permalink
docs: add etcd retry (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
Claude-Zq authored Oct 18, 2023
1 parent bde68f5 commit 3f84672
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "etcd"
date: 2023-04-22
date: 2023-10-18
weight: 4
keywords: ["Service Registration and Discovery", "etcd"]
description: "Service Registration and Discovery etcd Extensions provided by Hertz."
Expand Down Expand Up @@ -84,6 +84,117 @@ func main() {
}
```

#### Retry

After the service is registered to `ETCD`, it will regularly check the status of the service. If any abnormal status is found, it will try to register the service again. `observeDelay` is the delay time for checking the service status under normal conditions, and `retryDelay` is the delay time for attempting to register the service after disconnecting.

**Default Config**

| Config Name | Default Value | Description |
| ------------------- | ---------------- | ------------------------------------------------------------ |
| WithMaxAttemptTimes | 5 | Used to set the maximum number of attempts, if 0, it means infinite attempts |
| WithObserveDelay | 30 * time.Second | Used to set the delay time for checking service status under normal connection conditions |
| WithRetryDelay | 10 * time.Second | Used to set the retry delay time after disconnecting |

##### WithMaxAttemptTimes

`WithMaxAttemptTimes` sets the maximum number of call attempt times, including the initial call.

Function signature:

```go
func WithMaxAttemptTimes(maxAttemptTimes uint) Option
```

Example:

```go
func main() {
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
etcd.WithMaxAttemptTimes(10),
)
if err != nil {
panic(err)
}
// ...
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}))
// ...
}
```

##### WithObserveDelay

`WithObserveDelay` sets the delay time for checking the service status under normal conditions.

Function signature:

```go
func WithObserveDelay(observeDelay time.Duration) Option
```

Example:

```go
func main() {
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
etcd.WithObserveDelay(20*time.Second),
)
if err != nil {
panic(err)
}
// ...
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}))
// ...
}
```

##### WithRetryDelay

`WithRetryDelay` sets the delay time of retry.

Function signature:

```go
func WithRetryDelay(t time.Duration) Option
```

Example:

```go
func main() {
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
etcd.WithRetryDelay(5*time.Second),
)
if err != nil {
panic(err)
}
// ...
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}))
// ...
}
```

### NewEtcdRegistry

`NewEtcdRegistry` uses etcd to create a new service registry, requires passing in the endpoint value. Customizable service registry configuration.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "etcd"
date: 2023-04-22
date: 2023-10-18
weight: 4
keywords: ["服务注册与发现", "etcd"]
description: "Hertz 提供的服务注册与发现 etcd 拓展。"
Expand Down Expand Up @@ -84,6 +84,117 @@ func main() {
}
```

#### Retry

在服务注册到 etcd 之后,它会定期检查服务的状态。如果发现任何异常状态,它将尝试重新注册服务。observeDelay 是正常情况下检查服务状态的延迟时间,而 retryDelay 是断开连接后尝试注册服务的延迟时间。

**默认配置**

| 配置名 | 默认值 | 描述 |
| ------------------- | ---------------- | --------------------------------------------- |
| WithMaxAttemptTimes | 5 | 用于设置最大尝试次数,如果为0,则表示无限尝试 |
| WithObserveDelay | 30 * time.Second | 用于设置正常连接条件下检查服务状态的延迟时间 |
| WithRetryDelay | 10 * time.Second | 用于设置断开连接后重试的延迟时间 |

##### WithMaxAttemptTimes

`WithMaxAttemptTimes` 用于设置最大尝试次数,包括初始调用。

函数签名:

```go
func WithMaxAttemptTimes(maxAttemptTimes uint) Option
```

示例代码:

```go
func main() {
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
etcd.WithMaxAttemptTimes(10),
)
if err != nil {
panic(err)
}
// ...
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}))
// ...
}
```

##### WithObserveDelay

`WithObserveDelay` 用于设置正常连接条件下检查服务状态的延迟时间。

函数签名:

```go
func WithObserveDelay(observeDelay time.Duration) Option
```

示例代码:

```go
func main() {
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
etcd.WithObserveDelay(20*time.Second),
)
if err != nil {
panic(err)
}
// ...
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}))
// ...
}
```

##### WithRetryDelay

`WithRetryDelay`用于设置断开连接后重试的延迟时间。

函数签名:

```go
func WithRetryDelay(t time.Duration) Option
```

示例代码:

```go
func main() {
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
etcd.WithRetryDelay(5*time.Second),
)
if err != nil {
panic(err)
}
// ...
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: nil,
}))
// ...
}
```

### NewEtcdRegistry

`NewEtcdRegistry` 使用 etcd 创建一个新的服务注册中心,需要传入端点值。可自定义服务注册中心配置。
Expand Down

1 comment on commit 3f84672

@vercel
Copy link

@vercel vercel bot commented on 3f84672 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.