Skip to content

Commit

Permalink
1.Simplify syntax and struct;2.Add annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
AbericYang committed May 11, 2019
1 parent 59beb61 commit 42f456e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
1 change: 1 addition & 0 deletions discovery/consul/agent_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type AgentService struct {
Address string `json:"Address"`
}

// AgentCheck AgentServiceCheck 中所属服务健康检查 URL
type AgentCheck struct {
Output string `json:"Output"`
}
1 change: 1 addition & 0 deletions scheduled/task_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
selfDiscoveryComponent string
)

// CheckService 检查可用负载服务列表
func CheckService(serviceName, component string) {
selfServiceName = serviceName
selfDiscoveryComponent = component
Expand Down
87 changes: 48 additions & 39 deletions scheduled/task_service_consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func startCheckServicesByConsul(abortDiscovery chan int) {
func checkServicesByConsul(abortDiscovery chan int) {
// 检查发现服务状态
agentServiceChecks, slips := consul.ServiceCheck(selfServiceName)
if nil != slips {
abortDiscovery <- slips.Code
return
} else {
if nil == slips {
if nil == agentServiceChecks || len(agentServiceChecks) <= 0 {
consul.ReEnroll()
}
} else {
abortDiscovery <- slips.Code
return
}
// 获取本地可负载服务列表
allWay := shunt.GetShuntInstance().AllWay
Expand Down Expand Up @@ -107,42 +107,51 @@ func checkUpAndLocalByConsul(agentServiceChecks []*consul.AgentServiceCheck, ser
agentServiceCheck := agentServiceChecks[index]
// 如不可用,且本地列表中包含此服务,则移除本地列表中的服务
if agentServiceCheck.AggregatedStatus != "passing" {
servicesArr := services.Services
size := len(servicesArr)
for i := 0; i < size; i++ {
if servicesArr[i].Equal(agentServiceCheck.Service.Address, agentServiceCheck.Service.Port) {
services.Remove(i)
i--
size--
}
}
checkRemoveServiceByConsul(services, agentServiceCheck)
} else { // 如可用,且本地列表中不包含此服务,则新增服务到本地列表中
var health string // 服务健康检查地址
for offset := range agentServiceCheck.Checks {
if health = strings.Split(agentServiceCheck.Checks[offset].Output, " ")[2]; !strings.HasPrefix(health, "http") {
continue
} else {
health = health[0 : len(health)-1]
break
}
}
service := server.Service{
ID: agentServiceCheck.Service.ID,
Host: agentServiceCheck.Service.Address,
Port: agentServiceCheck.Service.Port,
Health: health,
}
have := false
for position := range services.Services {
if nil != services.Services && services.Services[position].Equal(service.Host, service.Port) {
have = true
break
}
}
if !have {
services.Add(service)
}
servicesCompare.Add(service)
checkAddServiceByConsul(services, servicesCompare, agentServiceCheck)
}
}
}

// checkRemoveServiceByConsul 移除本地列表中的服务
func checkRemoveServiceByConsul(services *server.Services, agentServiceCheck *consul.AgentServiceCheck) {
servicesArr := services.Services
size := len(servicesArr)
for i := 0; i < size; i++ {
if servicesArr[i].Equal(agentServiceCheck.Service.Address, agentServiceCheck.Service.Port) {
services.Remove(i)
i--
size--
}
}
}

// checkAddServiceByConsul 新增服务到本地列表中
func checkAddServiceByConsul(services, servicesCompare *server.Services, agentServiceCheck *consul.AgentServiceCheck) {
var health string // 服务健康检查地址
for offset := range agentServiceCheck.Checks {
if health = strings.Split(agentServiceCheck.Checks[offset].Output, " ")[2]; !strings.HasPrefix(health, "http") {
continue
}
health = health[0 : len(health)-1]
break
}
service := server.Service{
ID: agentServiceCheck.Service.ID,
Host: agentServiceCheck.Service.Address,
Port: agentServiceCheck.Service.Port,
Health: health,
}
have := false
for position := range services.Services {
if nil != services.Services && services.Services[position].Equal(service.Host, service.Port) {
have = true
break
}
}
if !have {
services.Add(service)
}
servicesCompare.Add(service)
}
12 changes: 6 additions & 6 deletions trans/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func SyncPoolGetRequest() *Request {
//
// param 请求对象
func (request *Request) RestJSONByURL(method string, url string, param interface{}) ([]byte, error) {
remote, uri := remoteUri(url)
remote, uri := remoteURI(url)
return request.RestJSON(method, remote, uri, param)
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func (request *Request) RestJSON(method string, remote string, uri string, param
//
// param 请求对象
func (request *Request) RestTextByURL(method string, url string, values url.Values) ([]byte, error) {
remote, uri := remoteUri(url)
remote, uri := remoteURI(url)
return request.RestText(method, remote, uri, values)
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func (request *Request) RestText(method string, remote string, uri string, value
//
// url:完整转发路径
func (request *Request) CallByURL(context *gin.Context, method string, url string) {
remote, uri := remoteUri(url)
remote, uri := remoteURI(url)
request.Call(context, method, remote, uri)
}

Expand Down Expand Up @@ -192,7 +192,7 @@ func (request *Request) Call(context *gin.Context, method string, remote string,
//
// callback *response.Result 请求转发降级后返回请求方结果对象
func (request *Request) CallbackByURL(context *gin.Context, method string, url string, callback func() *response.Result) {
remote, uri := remoteUri(url)
remote, uri := remoteURI(url)
request.Callback(context, method, remote, uri, callback)
}

Expand Down Expand Up @@ -290,7 +290,7 @@ func done(context *gin.Context, request *Request, body []byte, err error, callba
context.JSON(http.StatusOK, request.result)
}

func remoteUri(url string) (remote, uri string) {
func remoteURI(url string) (remote, uri string) {
urlTmp := url
if strings.Contains(urlTmp, "//") {
urlTmp = strings.Split(urlTmp, "//")[1]
Expand All @@ -299,6 +299,6 @@ func remoteUri(url string) (remote, uri string) {
urlTmp = urlTmp[size:]
remote = url[0:(len(url) - len(urlTmp) - 1)]
uri = urlTmp
log.Trans.Debug("remoteUri", zap.String("url", url), zap.String("remote", remote), zap.String("uri", uri))
log.Trans.Debug("remoteURI", zap.String("url", url), zap.String("remote", remote), zap.String("uri", uri))
return
}

0 comments on commit 42f456e

Please sign in to comment.