Golang Microservices无法使用Docker for Mac进行通信

我试图获得两个容器,每个容器运行不同的golang服务.这两个服务都是使用net / http包构建的.我有一个API前端作为一个和身份验证服务后端.

这是我的撰写文件:

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

我知道链接正在工作,因为从api容器我可以ping auth和curl -X发布http:// auth:8080 / validate但是在golang中我得到一个拨号地址tcp i / o timeout.这是golang代码.

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(请求)是抛出拨号错误的原因.现在我的auth服务甚至没有被触及,因为我有一个记录器可以打印来筛选每个正在进入的请求.

> env.AuthHost映射到AUTH_HOST环境变量.
> env.AuthPort映射到Auth_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),它返回一个不同的IP地址,然后ping,curl,甚至是docker inspect.这是一个golang的事情吗?

编辑:

很抱歉,所有编辑都会尝试调试.

如果我删除了authString的端口部分,请求会通过,但在解析响应时会出错.响应是NGINX的301重定向,我认为这很奇怪,因为它甚至不在我的堆栈中.重定向的位置标题是localhost,我认为这也很奇怪.

我已经尝试在主机上公开一个端口并使用该端口访问它,但没有更好的运气(相同的主机名).

编辑:

因此,我认为这只是Mac.我克隆了回购并在Windows 10上运行,我能够连接到我的auth服务.这会是Docker for Mac错误吗?我可能会向他们报告,但我不会认为这是关闭的,因为它仍然是Mac用户的问题.

最佳答案
所以Docker for Mac今天推出了新的测试版.这似乎解决了我的连接问题.现在,当我发现它在我的Windows PC上运行时,我确实对源代码进行了更改.

这是Docker for fix的版本:

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

我确实移动了服务,但我没有看到任何会改变容器之间通信的不同.这只是在这里,其他人也有同样的问题.

相关文章

最近一直在开发Apworks框架的案例代码,同时也在一起修复Apw...
最近每天都在空闲时间努力编写Apworks框架的案例代码WeText。...
在《Kubernetes中分布式存储Rook-Ceph部署快速演练》文章中,...
最近在项目中有涉及到Kubernetes的分布式存储部分的内容,也...
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.ne...
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.ne...