Skip to content

Commit

Permalink
fix: eureka interface and replication optimize (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewshan authored Jan 4, 2023
1 parent 621c182 commit c20f539
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
8 changes: 5 additions & 3 deletions apiserver/eurekaserver/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ func (a *ApplicationsBuilder) BuildApplications(oldAppsCache *ApplicationsRespCa
hashBuilder[status] = hashBuilder[status] + count
}
}
newApps.Application = append(newApps.Application, targetApp)
newApps.ApplicationMap[targetApp.Name] = targetApp
if len(targetApp.Instance) > 0 {
newApps.Application = append(newApps.Application, targetApp)
newApps.ApplicationMap[targetApp.Name] = targetApp
}
}
if oldApps != nil && len(oldApps.Application) != len(newApps.Application) {
changed = true
Expand Down Expand Up @@ -244,7 +246,7 @@ func (a *ApplicationsBuilder) buildDeltaApps(oldAppsCache *ApplicationsRespCache
}
// 修改,需要比较实例的变更
diffApp := diffApplication(oldApplication, application)
if diffApp != nil {
if diffApp != nil && len(diffApp.Instance) > 0 {
newDeltaApps.Application = append(newDeltaApps.Application, diffApp)
instCount += len(diffApp.Instance)
}
Expand Down
11 changes: 8 additions & 3 deletions apiserver/eurekaserver/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func (h *EurekaServer) BatchReplication(req *restful.Request, rsp *restful.Respo
writeHeader(http.StatusForbidden, rsp)
return
}
batchResponse, resultCode := h.doBatchReplicate(replicateRequest, token)
if err := writeEurekaResponseWithCode(restful.MIME_JSON, batchResponse, req, rsp, resultCode); nil != err {
log.Errorf("[EurekaServer]fail to write replicate response, client: %s, err: %v", remoteAddr, err)
}
}

func (h *EurekaServer) doBatchReplicate(replicateRequest *ReplicationList, token string) (*ReplicationListResponse, uint32) {
batchResponse := &ReplicationListResponse{ResponseList: []*ReplicationInstanceResponse{}}
var resultCode = api.ExecuteSuccess
for _, instanceInfo := range replicateRequest.ReplicationList {
Expand All @@ -87,9 +94,7 @@ func (h *EurekaServer) BatchReplication(req *restful.Request, rsp *restful.Respo
}
batchResponse.ResponseList = append(batchResponse.ResponseList, resp)
}
if err := writeEurekaResponseWithCode(restful.MIME_JSON, batchResponse, req, rsp, resultCode); nil != err {
log.Errorf("[EurekaServer]fail to write replicate response, client: %s, err: %v", remoteAddr, err)
}
return batchResponse, resultCode
}

func (h *EurekaServer) dispatch(replicationInstance *ReplicationInstance, token string) (*ReplicationInstanceResponse, uint32) {
Expand Down
3 changes: 2 additions & 1 deletion apiserver/eurekaserver/replicate_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (r *ReplicateWorker) doBatchReplicate(tasks []*ReplicationInstance) {
for _, task := range tasks {
replicateInfo = append(replicateInfo, fmt.Sprintf("%s:%s", task.Action, task.Id))
}
log.Infof("start to send replicate text %s, peers %v", string(jsonData), r.peers)
for _, peer := range r.peers {
go r.doReplicateToPeer(peer, tasks, jsonData, replicateInfo)
}
Expand Down Expand Up @@ -158,14 +159,14 @@ func sendHttpRequest(peer string, jsonData []byte, replicateInfo []string) (*Rep
log.Errorf("[EUREKA-SERVER] fail to send replicate request: %v", err)
return nil, err
}
defer response.Body.Close()
respStr, _ := io.ReadAll(response.Body)
respObj := &ReplicationListResponse{}
err = json.Unmarshal(respStr, respObj)
if nil != err {
log.Errorf("[EUREKA-SERVER] fail unmarshal text %s to ReplicationListResponse: %v", string(respStr), err)
return nil, err
}
_ = response.Body.Close()
log.Infof("[EUREKA-SERVER] success to replicate to %s, instances %v", peer, replicateInfo)
return respObj, nil
}
2 changes: 1 addition & 1 deletion service/healthcheck/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *Server) doReport(ctx context.Context, instance *api.Instance) *api.Resp

event := &model.InstanceEvent{
Id: ins.ID(),
Instance: instance,
Instance: ins.Proto,
EType: model.EventInstanceSendHeartbeat,
}
event.InjectMetadata(ctx)
Expand Down

0 comments on commit c20f539

Please sign in to comment.