2016-07-28 2 views
6

각각 다른 golang 서비스를 실행하는 두 개의 컨테이너를 얻으려고합니다. 두 서비스 모두 net/http 패키지로 제작되었습니다. API 프론트 엔드와 인증 서비스 백엔드가 있습니다. 내가 링크 내가 ping authcurl -X Post http://auth:8080/validate하지만 golang에서 나는 dial address tcp i/o timeout 얻을 수있는 API를 컨테이너에서하기 때문에 작업하는 알고Golang Microservices가 Mac 용 Docker를 사용하여 통신 할 수 없음

version: "2" 
services: 
    staticfiles: 
    build: ./files 
    volumes: 
     - /public 
     - /views 
    api: 
    build: ./api 
    environment: 
     - PORT=8080 
     - BASE_URL=https://example.org 
     - AUTH_HOST=auth 
     - AUTH_PORT=8080 
     - VIEW_DIR=/views 
     - PUBLIC_DIR=/public 
    ports: 
     - "80:8080" 
    volumes_from: 
     - staticfiles:ro 
    links: 
     - auth 
    depends_on: 
     - staticfiles 
    db: 
    build: ./postgres 
    environment: 
     - POSTGRES_USER=inheritor 
     - POSTGRES_DB=inheritor 
    auth: 
    build: ./auth 
    expose: 
     - "8080" 
    environment: 
     - PORT=8080 
     - DB_USER=inheritor 
     - DB_NAME=inheritor 
     - DB_HOST=db 
     - DB_Port=5432 
    links: 
     - db 

:

여기 내 작성 파일입니다. 다음은 골란 코드입니다.

var (
    authString = "http://" + env.AuthHost + ":" + env.AuthPort 
) 

//ValidateToken validates a token using the session in DB 
func ValidateToken(req *model.ValidateRequest) (*model.JWTClaims, error) { 
    client := new(http.Client) 
    api := authString + "/validate" 
    cont, err := model.Jsonify(req) 
    if err != nil { 
     return nil, exception.NewInternalError("Could not turn the request into a json object.") 
    } 

    request, err := http.NewRequest("POST", api, bytes.NewBuffer(cont)) 
    if err != nil { 
     return nil, exception.NewInternalError("Could not create request: " + err.Error()) 
    } 
    request.Header.Set("Content-type", "application/json") 
    response, err := client.Do(request) 
    if err != nil { 
     return nil, exception.NewInternalError("Could not make the request: " + err.Error()) 
    } 
    defer response.Body.Close() 

    res := new(model.AuthResponse) 
    res.Claims = new(model.JWTClaims) 
    decoder := json.NewDecoder(response.Body) 
    err = decoder.Decode(&res) 
    spew.Dump(response.Body) 
    if err != nil { 
     return nil, exception.NewInternalError("Could not parse response back from auth service. " + err.Error()) 
    } 

    if response.StatusCode != http.StatusOK { 
     return nil, exception.NewInvalidJWTError(res.Error) 
    } 

    return res.Claims, nil 
} 

client.Do(request)은 다이얼 오류를 발생시키는 것입니다. 내가오고 모든 요청을 화면에 출력하는 로거를 가지고 있기 때문에 지금 내 정식 서비스에도 감동되지 않습니다.

  • env.AuthHost

    AUTH_HOST 환경 변수에 매핑됩니다.
  • env.AuthPortAuth_PORT 환경 변수에 매핑됩니다.

많은 도움을 주시면 감사하겠습니다.

도움이된다면 MacOSX를 사용하고 있습니다.

Client: 
Version:  1.12.0-rc4 
API version: 1.24 
Go version: go1.6.2 
Git commit: e4a0dbc 
Built:  Wed Jul 13 03:28:51 2016 
OS/Arch:  darwin/amd64 
Experimental: true 

Server: 
Version:  1.12.0-rc4 
API version: 1.24 
Go version: go1.6.2 
Git commit: e4a0dbc 
Built:  Wed Jul 13 03:28:51 2016 
OS/Arch:  linux/amd64 
Experimental: true 

두 golangs Dockerfile은 다음과 같습니다

FROM golang:1.6 
RUN mkdir -p /go/src/github.com/dixonwille/Inheritor/api 
WORKDIR /go/src/github.com/dixonwille/Inheritor/api 

COPY . /go/src/github.com/dixonwille/Inheritor/api 

RUN go build -v -o Inheritor cmd/Inheritor/main.go 

USER nobody 
ENTRYPOINT ["./Inheritor"] 

편집 :

그래서 내가 golang 내 net.LookupHost(env.AuthHost)를 실행하고 다음 ping을 다른 IP 주소를 반환, curl을, 심지어 docker inspect. 이거 골란 물건인가요?

편집 : 가지 날이 갈수록 디버깅하려고 모든 편집에 대한

죄송합니다.

authString의 포트 부분을 제거하면 요청이 처리되지만 응답을 구문 분석 할 때 오류가 발생합니다. 응답은 NGINX에 의한 301 리디렉션입니다. NGINX는 제 스택에도 있지 않기 때문에 이상하다고 생각합니다. 리디렉션의 위치 헤더는 localhost이며 이상하게 생각됩니다.

나는 호스트 컴퓨터에서 포트를 노출시키고 더 나은 운 (같은 호스트 이름)을 사용하여 포트에 액세스하려고 시도했다.

편집 :

그래서 그것은 내가 가정 맥 유일한 것이다. 나는 repo를 복제하고 Windows 10에서 실행되었으며 내 인증 서비스에 연결할 수있었습니다. Mac 용 Docker 오류일까요? 나는 아마 그것들에 대해보고 할 것이지만, 여전히 맥 사용자들에게는 문제가되기 때문에 이것을 고려하지 않을 것이다.

+0

나는 클라이언트라고 생각하지 않는다. 재 시도하기 전에, grpc 서버가 온라인 상태가 될 수있는 기회를 얻기 위해 다이얼하기 전에'time.Sleep (10 * time.Second)'를 추가 할 수있다. 'links : -db'를'depends_on : -db'로 대체 할 수도 있습니다 – Plato

+0

호출이 도움이되기 전에 10 초를 추가하는 방법을 모르겠습니까? 그것은 일정한 오류입니다. 매번 시도 할 때마다 동일한 오류가 발생할 때마다 컨테이너를 다시 시작하지 않습니다. 내가 IP 주소를 보유 할 수 있도록 링크가 필요하지 않습니까? – Clemsonopoly94

답변

1

Mac 용 Docker가 오늘 새로운 베타 버전으로 출시되었습니다. 이것은 연결 문제가 해결 된 것 같습니다.이제는 Windows PC에서 작동한다는 것을 알았을 때 소스 코드를 변경했습니다. 여기

수정 프로그램에 대한 도커의 버전 :

Client: 
Version:  1.12.0 
API version: 1.24 
Go version: go1.6.3 
Git commit: 8eab29e 
Built:  Thu Jul 28 21:04:48 2016 
OS/Arch:  darwin/amd64 
Experimental: true 

Server: 
Version:  1.12.0 
API version: 1.24 
Go version: go1.6.3 
Git commit: 8eab29e 
Built:  Thu Jul 28 21:04:48 2016 
OS/Arch:  linux/amd64 
Experimental: true 

그리고 여기에 작성 파일입니다

version: "2" 
services: 
    staticfiles: 
    build: ./files 
    volumes: 
     - /public 
     - /views 
     - /migrations 
    databasefiles: 
    build: ./databasefiles 
    volumes: 
     - /var/lib/postgresql/data 
    db: 
    build: ./postgres 
    depends_on: 
     - databasefiles 
    volumes_from: 
     - databasefiles 
    environment: 
     - POSTGRES_USER=inheritor 
     - POSTGRES_DB=inheritor 
    auth: 
    build: ./auth 
    expose: 
     - "8080" 
    depends_on: 
     - staticfiles 
    volumes_from: 
     - staticfiles:ro 
    environment: 
     - PORT=8080 
     - DB_USER=inheritor 
     - DB_NAME=inheritor 
     - DB_HOST=db 
     - DB_PORT=5432 
     - MIGRATION_DIR=/migrations 
    links: 
     - db 
    api: 
    build: ./api 
    environment: 
     - PORT=8080 
     - BASE_URL=https://example.org 
     - AUTH_HOST=auth 
     - AUTH_PORT=8080 
     - VIEW_DIR=/views 
     - PUBLIC_DIR=/public 
    ports: 
     - "80:8080" 
    volumes_from: 
     - staticfiles:ro 
    links: 
     - auth 
    depends_on: 
     - staticfiles 

나는 주위 서비스를 이동했지만 나는 변경 것이라고 아무것도 다른 표시되지 않습니다 컨테이너 간의 통신. 이것은 단지 여기에 다른 사람들이 같은 문제가 있음을 나타냅니다.

관련 문제