Skip to content

Commit

Permalink
🐛 修复Re-Register
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanbo committed Jul 28, 2020
1 parent 00c8ef8 commit d190b02
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
36 changes: 25 additions & 11 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package eureka_client

import (
"errors"
"fmt"
"net/http"
"net/url"

"github.com/xuanbo/requests"
)

var (
// ErrNotFound 实例不存在,需要重新注册
ErrNotFound = errors.New("not found")
)

// 与eureka服务端rest交互
// https://github.com/Netflix/eureka/wiki/Eureka-REST-operations

Expand All @@ -21,24 +28,24 @@ func Register(zone, app string, instance *Instance) error {
Instance: instance,
}

url := zone + "apps/" + app
u := zone + "apps/" + app

// status: http.StatusNoContent
result := requests.Post(url).Json(info).Send().Status2xx()
result := requests.Post(u).Json(info).Send().Status2xx()
if result.Err != nil {
return fmt.Errorf("Register application instance failed, error: %s", result.Err)
return fmt.Errorf("register application instance failed, error: %s", result.Err)
}
return nil
}

// UnRegister 删除实例
// DELETE /eureka/v2/apps/appID/instanceID
func UnRegister(zone, app, instanceID string) error {
url := zone + "apps/" + app + "/" + instanceID
u := zone + "apps/" + app + "/" + instanceID
// status: http.StatusNoContent
result := requests.Delete(url).Send().StatusOk()
result := requests.Delete(u).Send().StatusOk()
if result.Err != nil {
return fmt.Errorf("UnRegister application instance failed, error: %s", result.Err)
return fmt.Errorf("unRegister application instance failed, error: %s", result.Err)
}
return nil
}
Expand All @@ -53,10 +60,10 @@ func Refresh(zone string) (*Applications, error) {
res := &Result{
Applications: apps,
}
url := zone + "apps"
err := requests.Get(url).Header("Accept", " application/json").Send().StatusOk().Json(res)
u := zone + "apps"
err := requests.Get(u).Header("Accept", " application/json").Send().StatusOk().Json(res)
if err != nil {
return nil, fmt.Errorf("Refresh failed, error: %s", err)
return nil, fmt.Errorf("refresh failed, error: %s", err)
}
return apps, nil
}
Expand All @@ -68,9 +75,16 @@ func Heartbeat(zone, app, instanceID string) error {
params := url.Values{
"status": {"UP"},
}
result := requests.Put(u).Params(params).Send().StatusOk()
result := requests.Put(u).Params(params).Send()
if result.Err != nil {
return fmt.Errorf("Heartbeat failed, error: %s", result.Err)
return fmt.Errorf("heartbeat failed, error: %s", result.Err)
}
// 心跳 404 说明eureka server重启过,需要重新注册
if result.Resp.StatusCode == http.StatusNotFound {
return ErrNotFound
}
if result.Resp.StatusCode != http.StatusOK {
return fmt.Errorf("heartbeat failed, invalid status code: %d", result.Resp.StatusCode)
}
return nil
}
17 changes: 12 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *Client) Start() {
log.Println(err.Error())
return
}
log.Println("Register application instance successful")
log.Println("register application instance successful")
// 刷新服务列表
go c.refresh()
// 心跳
Expand All @@ -47,7 +47,7 @@ func (c *Client) refresh() {
if err := c.doRefresh(); err != nil {
log.Println(err)
} else {
log.Println("Refresh application instance successful")
log.Println("refresh application instance successful")
}
} else {
break
Expand All @@ -62,9 +62,16 @@ func (c *Client) heartbeat() {
for {
if c.Running {
if err := c.doHeartbeat(); err != nil {
if err == ErrNotFound {
log.Println("heartbeat Not Found, need register")
if err = c.doRegister(); err != nil {
log.Printf("do register error: %s\n", err)
}
continue
}
log.Println(err)
} else {
log.Println("Heartbeat application instance successful")
log.Println("heartbeat application instance successful")
}
} else {
break
Expand Down Expand Up @@ -118,12 +125,12 @@ func (c *Client) handleSignal() {
case syscall.SIGKILL:
fallthrough
case syscall.SIGTERM:
log.Println("Receive exit signal, client instance going to de-egister")
log.Println("receive exit signal, client instance going to de-register")
err := c.doUnRegister()
if err != nil {
log.Println(err.Error())
} else {
log.Println("UnRegister application instance successful")
log.Println("unRegister application instance successful")
}
os.Exit(0)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
// create eureka client
client := eureka.NewClient(&eureka.Config{
DefaultZone: "http://127.0.0.1:8080/eureka/",
DefaultZone: "http://localhost:8761/eureka/",
App: "go-example",
Port: 10000,
RenewalIntervalInSecs: 10,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/xuanbo/eureka-client

go 1.14

require github.com/xuanbo/requests v0.0.1
2 changes: 1 addition & 1 deletion readme-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## 未完成

* 当eureka服务端重启后,心跳404时支持重新注册
* ~~当eureka服务端重启后,心跳404时支持重新注册~~

* 以delta方式刷新服务列表(增量拉取)

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Todo

* Re-Register
* ~~Re-Register~~

* Refresh by delta

Expand Down

0 comments on commit d190b02

Please sign in to comment.