diff --git a/api.go b/api.go index c9a6e9d..0bc31dd 100644 --- a/api.go +++ b/api.go @@ -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 @@ -21,12 +28,12 @@ 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 } @@ -34,11 +41,11 @@ func Register(zone, app string, instance *Instance) error { // 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 } @@ -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 } @@ -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 } diff --git a/client.go b/client.go index c6254d0..80f64e3 100644 --- a/client.go +++ b/client.go @@ -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() // 心跳 @@ -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 @@ -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 @@ -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) } diff --git a/examples/main.go b/examples/main.go index 138f8a7..5c03a28 100644 --- a/examples/main.go +++ b/examples/main.go @@ -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, diff --git a/go.mod b/go.mod index fb5ff64..f6e351a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/xuanbo/eureka-client +go 1.14 + require github.com/xuanbo/requests v0.0.1 diff --git a/readme-zh.md b/readme-zh.md index 61aa21c..44d26a5 100644 --- a/readme-zh.md +++ b/readme-zh.md @@ -11,7 +11,7 @@ ## 未完成 -* 当eureka服务端重启后,心跳404时支持重新注册 +* ~~当eureka服务端重启后,心跳404时支持重新注册~~ * 以delta方式刷新服务列表(增量拉取) diff --git a/readme.md b/readme.md index 8a84886..4836269 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ ## Todo -* Re-Register +* ~~Re-Register~~ * Refresh by delta