Skip to content

Commit 3f84672

Browse files
authored
docs: add etcd retry (#827)
1 parent bde68f5 commit 3f84672

File tree

2 files changed

+224
-2
lines changed
  • content
    • en/docs/hertz/tutorials/service-governance/service_discovery
    • zh/docs/hertz/tutorials/service-governance/service_discovery

2 files changed

+224
-2
lines changed

content/en/docs/hertz/tutorials/service-governance/service_discovery/etcd.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "etcd"
3-
date: 2023-04-22
3+
date: 2023-10-18
44
weight: 4
55
keywords: ["Service Registration and Discovery", "etcd"]
66
description: "Service Registration and Discovery etcd Extensions provided by Hertz."
@@ -84,6 +84,117 @@ func main() {
8484
}
8585
```
8686

87+
#### Retry
88+
89+
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.
90+
91+
**Default Config**
92+
93+
| Config Name | Default Value | Description |
94+
| ------------------- | ---------------- | ------------------------------------------------------------ |
95+
| WithMaxAttemptTimes | 5 | Used to set the maximum number of attempts, if 0, it means infinite attempts |
96+
| WithObserveDelay | 30 * time.Second | Used to set the delay time for checking service status under normal connection conditions |
97+
| WithRetryDelay | 10 * time.Second | Used to set the retry delay time after disconnecting |
98+
99+
##### WithMaxAttemptTimes
100+
101+
`WithMaxAttemptTimes` sets the maximum number of call attempt times, including the initial call.
102+
103+
Function signature:
104+
105+
```go
106+
func WithMaxAttemptTimes(maxAttemptTimes uint) Option
107+
```
108+
109+
Example:
110+
111+
```go
112+
func main() {
113+
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
114+
etcd.WithMaxAttemptTimes(10),
115+
)
116+
if err != nil {
117+
panic(err)
118+
}
119+
// ...
120+
h := server.Default(
121+
server.WithHostPorts(addr),
122+
server.WithRegistry(r, &registry.Info{
123+
ServiceName: "hertz.test.demo",
124+
Addr: utils.NewNetAddr("tcp", addr),
125+
Weight: 10,
126+
Tags: nil,
127+
}))
128+
// ...
129+
}
130+
```
131+
132+
##### WithObserveDelay
133+
134+
`WithObserveDelay` sets the delay time for checking the service status under normal conditions.
135+
136+
Function signature:
137+
138+
```go
139+
func WithObserveDelay(observeDelay time.Duration) Option
140+
```
141+
142+
Example:
143+
144+
```go
145+
func main() {
146+
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
147+
etcd.WithObserveDelay(20*time.Second),
148+
)
149+
if err != nil {
150+
panic(err)
151+
}
152+
// ...
153+
h := server.Default(
154+
server.WithHostPorts(addr),
155+
server.WithRegistry(r, &registry.Info{
156+
ServiceName: "hertz.test.demo",
157+
Addr: utils.NewNetAddr("tcp", addr),
158+
Weight: 10,
159+
Tags: nil,
160+
}))
161+
// ...
162+
}
163+
```
164+
165+
##### WithRetryDelay
166+
167+
`WithRetryDelay` sets the delay time of retry.
168+
169+
Function signature:
170+
171+
```go
172+
func WithRetryDelay(t time.Duration) Option
173+
```
174+
175+
Example:
176+
177+
```go
178+
func main() {
179+
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
180+
etcd.WithRetryDelay(5*time.Second),
181+
)
182+
if err != nil {
183+
panic(err)
184+
}
185+
// ...
186+
h := server.Default(
187+
server.WithHostPorts(addr),
188+
server.WithRegistry(r, &registry.Info{
189+
ServiceName: "hertz.test.demo",
190+
Addr: utils.NewNetAddr("tcp", addr),
191+
Weight: 10,
192+
Tags: nil,
193+
}))
194+
// ...
195+
}
196+
```
197+
87198
### NewEtcdRegistry
88199

89200
`NewEtcdRegistry` uses etcd to create a new service registry, requires passing in the endpoint value. Customizable service registry configuration.

content/zh/docs/hertz/tutorials/service-governance/service_discovery/etcd.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "etcd"
3-
date: 2023-04-22
3+
date: 2023-10-18
44
weight: 4
55
keywords: ["服务注册与发现", "etcd"]
66
description: "Hertz 提供的服务注册与发现 etcd 拓展。"
@@ -84,6 +84,117 @@ func main() {
8484
}
8585
```
8686

87+
#### Retry
88+
89+
在服务注册到 etcd 之后,它会定期检查服务的状态。如果发现任何异常状态,它将尝试重新注册服务。observeDelay 是正常情况下检查服务状态的延迟时间,而 retryDelay 是断开连接后尝试注册服务的延迟时间。
90+
91+
**默认配置**
92+
93+
| 配置名 | 默认值 | 描述 |
94+
| ------------------- | ---------------- | --------------------------------------------- |
95+
| WithMaxAttemptTimes | 5 | 用于设置最大尝试次数,如果为0,则表示无限尝试 |
96+
| WithObserveDelay | 30 * time.Second | 用于设置正常连接条件下检查服务状态的延迟时间 |
97+
| WithRetryDelay | 10 * time.Second | 用于设置断开连接后重试的延迟时间 |
98+
99+
##### WithMaxAttemptTimes
100+
101+
`WithMaxAttemptTimes` 用于设置最大尝试次数,包括初始调用。
102+
103+
函数签名:
104+
105+
```go
106+
func WithMaxAttemptTimes(maxAttemptTimes uint) Option
107+
```
108+
109+
示例代码:
110+
111+
```go
112+
func main() {
113+
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
114+
etcd.WithMaxAttemptTimes(10),
115+
)
116+
if err != nil {
117+
panic(err)
118+
}
119+
// ...
120+
h := server.Default(
121+
server.WithHostPorts(addr),
122+
server.WithRegistry(r, &registry.Info{
123+
ServiceName: "hertz.test.demo",
124+
Addr: utils.NewNetAddr("tcp", addr),
125+
Weight: 10,
126+
Tags: nil,
127+
}))
128+
// ...
129+
}
130+
```
131+
132+
##### WithObserveDelay
133+
134+
`WithObserveDelay` 用于设置正常连接条件下检查服务状态的延迟时间。
135+
136+
函数签名:
137+
138+
```go
139+
func WithObserveDelay(observeDelay time.Duration) Option
140+
```
141+
142+
示例代码:
143+
144+
```go
145+
func main() {
146+
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
147+
etcd.WithObserveDelay(20*time.Second),
148+
)
149+
if err != nil {
150+
panic(err)
151+
}
152+
// ...
153+
h := server.Default(
154+
server.WithHostPorts(addr),
155+
server.WithRegistry(r, &registry.Info{
156+
ServiceName: "hertz.test.demo",
157+
Addr: utils.NewNetAddr("tcp", addr),
158+
Weight: 10,
159+
Tags: nil,
160+
}))
161+
// ...
162+
}
163+
```
164+
165+
##### WithRetryDelay
166+
167+
`WithRetryDelay`用于设置断开连接后重试的延迟时间。
168+
169+
函数签名:
170+
171+
```go
172+
func WithRetryDelay(t time.Duration) Option
173+
```
174+
175+
示例代码:
176+
177+
```go
178+
func main() {
179+
r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"},
180+
etcd.WithRetryDelay(5*time.Second),
181+
)
182+
if err != nil {
183+
panic(err)
184+
}
185+
// ...
186+
h := server.Default(
187+
server.WithHostPorts(addr),
188+
server.WithRegistry(r, &registry.Info{
189+
ServiceName: "hertz.test.demo",
190+
Addr: utils.NewNetAddr("tcp", addr),
191+
Weight: 10,
192+
Tags: nil,
193+
}))
194+
// ...
195+
}
196+
```
197+
87198
### NewEtcdRegistry
88199

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

0 commit comments

Comments
 (0)