【发布时间】: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 auth 和 curl -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 地址与 ping、curl 甚至 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