diff --git a/Common/commonfunc.go b/Common/commonfunc.go index 5dabeaf..35186e4 100644 --- a/Common/commonfunc.go +++ b/Common/commonfunc.go @@ -1024,3 +1024,27 @@ func FormatTimes(now time.Time, format string) string { return now.Format(format) } + +func CheckIPReachable(ip string, port int, timeout time.Duration) bool { + conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), timeout) + if err != nil { + return false + } + conn.Close() + return true +} + +func GetAvailableEndpoint() string { + primaryIP := LoadConfig().SeverData.ServerIP + backupIP := LoadConfig().SeverData.BackUpServerIP // 替换为实际备选IP + port := 9800 + timeout := 3 * time.Second + + if CheckIPReachable(primaryIP, port, timeout) { + return primaryIP + } + if CheckIPReachable(backupIP, port, timeout) { + return backupIP + } + return "" +} diff --git a/main.go b/main.go index 2fe1b88..3d43824 100644 --- a/main.go +++ b/main.go @@ -1817,7 +1817,15 @@ func GetAgenda(AgID string) { //通过接口获取会议,人员信息 var agendaPersons []Model.T_AgendaPerson = make([]Model.T_AgendaPerson, 0) - url := "http://" + Common.LoadConfig().SeverData.ServerIP + ":9800/vhwcapi/v1/web/api/service/congresscontrol/getpersonforcheckdoor" + + targetIP := Common.GetAvailableEndpoint() + if targetIP == "" { + log.Error("所有IP均不可达") + log.Println("所有IP均不可达") + return + } + + url := "http://" + targetIP + ":9800/vhwcapi/v1/web/api/service/congresscontrol/getpersonforcheckdoor" jsonData := `{}` body, statusCode, err := Common.SendPostRequest(url, jsonData, "application/json") if err != nil {