【问题标题】:Microservices can't communicate using Docker for Mac微服务无法使用 Docker for Mac 进行通信
【发布时间】:2019-11-15 04:35:05
【问题描述】:

我正在尝试获取两个容器,它们各自运行不同的 Go 服务。这两个服务都是用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 authcurl -X Post http://auth:8080/validate,但在 Go 中我会得到 dial address tcp i/o timeout。这是代码。

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.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

Dockerfiles 都长这样:

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"]

编辑:

我在 Go 中运行 net.LookupHost(env.AuthHost),它返回的 IP 地址与 pingcurl 甚至 docker inspect 不同。那么这是围棋吗?

编辑:

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

我尝试在主机上公开一个端口并使用该端口访问它,但运气不佳(主机名相同)。

编辑:

所以我假设它是一个只有 Mac 的东西。我克隆了存储库并在 Windows 10 上运行,并且能够连接到我的身份验证服务。这会是 Docker for Mac 错误吗?我可能会向他们报告,但我不会认为这已经结束,因为它仍然是 Mac 用户的问题。

【问题讨论】:

  • 我不认为client.Do重试,您可以尝试在拨号之前添加time.Sleep(10*time.Second),以获得更好的grpc服务器在线机会。你也可以尝试用depends_on: -db替换links: -db
  • 我看不出在通话前增加 10 秒会有什么帮助?这是一个持续的错误。每次我尝试并遇到同样的错误时,每次都没有容器重新启动。我不需要链接以便获取 IP 地址吗?

标签: go docker microservices


【解决方案1】:

所以 Docker for Mac 今天刚刚发布了一个新的 beta 版本。这似乎解决了我的连接问题。现在,当我发现它可以在我的 Windows 电脑上运行时,我确实对源代码进行了更改。

这里是用于修复的 Docker 版本:

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

我确实移动了服务,但我没有发现任何会改变容器之间通信的不同之处。这只是在这里,以防其他人有同样的问题。

【讨论】:

    猜你喜欢
    • 2022-07-16
    • 1970-01-01
    • 2020-09-14
    • 2018-09-07
    • 2021-09-22
    • 1970-01-01
    • 2022-01-13
    • 2022-10-02
    • 2019-11-15
    相关资源
    最近更新 更多