TCP의 LISTEN을 보고 건강검진 응답
배경
AWS의 ALB (Application Load Balancer) 를 사용하려고 했는데 앱이 건강검진에 반응이 없어요.
(ALB는 정상적인 HTTP 상태 코드로서 200~499 범위지정 가능, 응용 프로그램은 500의 규격 orz에 정상적으로 응답)
그리고 어플리케이션에서 수정할 수 있는 분위기가 없어서 건강검진이라고 적었습니다.
요약
최종 완성코드
・net/http 건강검진용 웹 시작
· health check request를 받은 후 net.Listen 에서 서비스가 실행되는 20000번 포트에 Listen을 시도합니다
· Listen 성공(서비스 중지) → 응답 404
or
· Listen을 실행할 수 없는 경우(서비스 실행) → 응답 200
handler 만들기
건강검진 요청을 받았을 때 특정 포트(이번에는 20000 포트)를 시도한 Listen의 Handler입니다.
가능하면 404NotFound에서, Listen을 수행할 수 없으면 200OK에 응답합니다.
var (
svcProtocol = "tcp4"
svcPort = ":20000"
okMsg = "healthy"
ngMsg = "unhealthy..."
)
func checker(writer http.ResponseWriter, request *http.Request) {
// Try to listen on svcProtocol and svcPort.
listenchecker, err := net.Listen(svcProtocol, svcPort)
if err == nil {
// able to listen it... Which means, the service has stopped...ng
writer.WriteHeader(http.StatusNotFound)
fmt.Fprint(writer, ngMsg)
defer listenchecker.Close()
return
}
// not able to listen it. Which means, the service is working...ok!!!
fmt.Fprint(writer, okMsg)
return
}
handler 등록 및 건강검진 응답 웹 시작
건강검진 경로와 포트를 확인하고handler를 등록하고 웹을 시작합니다
health.go
var (
healthPath = "/health"
healthPort = ":80"
)
func main() {
http.HandleFunc(healthPath, checker)
http.ListenAndServe(healthPort, nil)
}
윈도우즈 컴파일
-ldflags="-H windowsgui" 프롬프트를 시작하지 마십시오.GOOS=windows GOARCH=386 go build -ldflags="-H windowsgui" health.go
AWS 측면
모니터 설정
먼저 서비스에 사용할 포트를 지정합니다.
건강검진 설정
다음은 건강검진을 지정합니다.
점은 건강검진을 지정하는 포트
トラフィックポート가 아니라 지정上書き, 건강검진 전용 포트를 지정합니다.
실험 시작
우선, 윈도우즈 서버에서 컴파일된 자체 건강 검사를 시작합니다
c:\>health.exe
건강검진(80) 및 서비스(20000) 함께 시작(LISTEN)c:\>netstat -aon | find "80"
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 3552
c:\>netstat -aon | find "20000"
TCP 0.0.0.0:20000 0.0.0.0:0 LISTENING 3636
컬로 건강검진 대상자를 맞으면 200을 돌려드립니다.~ ❯❯❯ curl -i http://ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com/health
HTTP/1.1 200 OK
Date: Sat, 30 Mar 2019 03:33:21 GMT
Content-Length: 7
Content-Type: text/plain; charset=utf-8
healthy
서비스를 중지하고 다시 curl로 확인하면 연동된 것을 확인할 수 있습니다.~ ❯❯❯ curl -i http://ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com/health
HTTP/1.1 404 Not Found
Date: Sat, 30 Mar 2019 03:34:17 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8
unhealthy...
Reference
이 문제에 관하여(TCP의 LISTEN을 보고 건강검진 응답), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/andromeda/items/ca9b893c3f966a963d90텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)