我有一個 Go 應用程序和三個用于應用程序、數據庫和 mountebank 的 docker 容器來模擬/存根 HTTP 響應。我希望我的 mountebank 容器在 test_api(應用程序容器)發出請求時返回適當的響應。當我使用 Postman 調用端點時,mountebank 容器會返回正確的響應。但是,當我在 test_api 容器中的代碼像下面的代碼一樣發送 GET 請求時,它總是會收到撥打 tcp 127.0.0.1:3000:連接:連接被拒絕下面是 test_api 如何向 mountebank 容器發送請求func getSuper(id string) (*float64, error) { var url = "http://localhost:3000/ato/employee/?/balance" url = strings.Replace(url, "?", id, 1) log.Println("Sending request to this url: ", url) resp, err := http.Get(url) if err != nil { return nil, &errorhandling.RequestError{Context: "getSuper calling ato api", Code: errorhandling.Internal, Message: err.Error()} } body, err := resposeToByte(resp) if err != nil { log.Println("err in coverting response to byte[]:", err) return nil, &errorhandling.RequestError{Context: "getsuper resposeToByte", Code: errorhandling.Internal, Message: err.Error()} } superData, err := UnmarshalSuperDetails(body) if err != nil { return nil, &errorhandling.RequestError{Context: "UnmarshalSuperDetails())", Code: errorhandling.Internal, Message: err.Error()} } log.Println("super details ", superData) return &superData.SuperBalance, nil}這是我用于 mountebank 的 docker 文件FROM alpine:3.14ENV MOUNTEBANK_VERSION=2.4.0RUN apk add --update nodejs-lts && \ apk add --update npmRUN npm install -g mountebank@${MOUNTEBANK_VERSION} --productionEXPOSE 2525ENTRYPOINT ["mb"]CMD ["start"]如何更改我的代碼以使 test_api 收到來自 mountebank 的正確響應?
1 回答

holdtom
TA貢獻1805條經驗 獲得超10個贊
您應該在 test_api 中更改以下行;
var url = "http://localhost:3000/ato/employee/?/balance"
與以下一個;
var url = "http://mountebank:3000/ato/employee/?/balance"
由于這些是不同的容器,您應該在 Docker 環境中指定它們的名稱或 IP。您的 test_api 請求其 localhost 并且沒有 3000 的開放端口,您將收到連接被拒絕錯誤。您可以查看Docker Networking以獲取更多信息。
- 1 回答
- 0 關注
- 85 瀏覽
添加回答
舉報
0/150
提交
取消