Skip to content

Commit

Permalink
🐛 Fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanbo committed Dec 13, 2021
1 parent d190b02 commit 843a5f1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
37 changes: 24 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,26 @@ func (c *Client) Start() {
c.mutex.Lock()
c.Running = true
c.mutex.Unlock()
// 注册
if err := c.doRegister(); err != nil {
log.Println(err.Error())
return
}
log.Println("register application instance successful")
// 刷新服务列表
go c.refresh()
// 心跳
go c.heartbeat()
// 监听退出信号,自动删除注册信息
go c.handleSignal()
go func() {
// 注册
// 修复首次无法连接时,直接return问题
for {
if err := c.doRegister(); err != nil {
log.Println(err.Error())
sleep := time.Duration(c.Config.RetryIntervalInSecs)
time.Sleep(sleep * time.Second)
continue
}
log.Println("register application instance successful :)")
break
}
// 刷新服务列表
go c.refresh()
// 心跳
go c.heartbeat()
// 监听退出信号,自动删除注册信息
go c.handleSignal()
}()
}

// refresh 刷新服务列表
Expand All @@ -63,7 +71,7 @@ func (c *Client) heartbeat() {
if c.Running {
if err := c.doHeartbeat(); err != nil {
if err == ErrNotFound {
log.Println("heartbeat Not Found, need register")
log.Println("heartbeat not found, need register")
if err = c.doRegister(); err != nil {
log.Printf("do register error: %s\n", err)
}
Expand Down Expand Up @@ -148,6 +156,9 @@ func defaultConfig(config *Config) {
if config.DefaultZone == "" {
config.DefaultZone = "http://localhost:8761/eureka/"
}
if config.RetryIntervalInSecs == 0 {
config.RetryIntervalInSecs = 5
}
if config.RenewalIntervalInSecs == 0 {
config.RenewalIntervalInSecs = 30
}
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
type Config struct {
// eureka服务端地址
DefaultZone string
// 首次连接不上时,重试间隔5s
RetryIntervalInSecs int
// 心跳间隔,默认30s
RenewalIntervalInSecs int
// 获取服务列表间隔,默认15s
Expand Down
1 change: 1 addition & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
DefaultZone: "http://localhost:8761/eureka/",
App: "go-example",
Port: 10000,
RetryIntervalInSecs: 5,
RenewalIntervalInSecs: 10,
DurationInSecs: 30,
Metadata: map[string]interface{}{
Expand Down

0 comments on commit 843a5f1

Please sign in to comment.