gpt4 book ai didi

rest - AWS-ECS-容器之间的通信-未知主机错误

转载 作者:行者123 更新时间:2023-12-02 19:34:46 24 4
gpt4 key购买 nike

我有两个Docker容器。

  • TestWeb(公开:80)
  • TestAPI(公开:80)

  • Testweb容器调用TestApi容器。主机可以从端口8080与TestWeb容器通信。主机可以使用8081与TestApi通信。

    我可以在Web开发箱(Windows 10)中让TestWeb调用TestApi,但是当我将代码部署到AWS(ECS)时,出现“未知主机”异常。两种容器都可以正常工作,我可以分别调用它们。但是当我调用一个内部使用HttpClient对Container2中的方法进行Rest调用的方法时,会出现错误:

    An error occurred while sending the request. ---> System.Net.Http.CurlException: Couldn't resolve host name.



    码:
    using (var client = new HttpClient())
    {
    try
    {
    string url = "http://testapi/api/Tenant/?i=" + id;
    var response = client.GetAsync(url).Result;
    if (response.IsSuccessStatusCode)
    {
    var responseContent = response.Content;
    string responseString = responseContent.ReadAsStringAsync().Result;
    return responseString;
    }
    return response.StatusCode.ToString();
    }
    catch (HttpRequestException httpRequestException)
    {
    return httpRequestException.Message;
    }
    }

    以下是我尝试过的事情:

    两个容器(TestWeb,TestAPI)在AWS ECS中位于相同的任务定义中。在检查容器时,会获得每个容器的IP地址。我可以使用其IP地址从container1 ping容器2。但是我无法使用container2名称ping。它给了我“未知主机”错误。

    最佳答案

    看来ECS并不是在幕后使用合法的docker-compose,但是它们的实现确实支持Compose V2的“links”功能。

    这是我刚刚在ECS上运行的撰写文件的一部分,该文件需要相同的功能并且收到与您相同的“无法解析主机”错误。我添加的“链接”修复了我在Elastic Container Service上的主机名解析问题!

    version: '3'
    services:
    appserver:
    links:
    - database:database
    - socks-proxy:socks-proxy

    这使我的应用服务器可以与 databasesocks-proxy主机名进行通信。格式为“SERVICE:ALIAS”,可以将它们与默认做法保持相同。

    在您的示例中,它将是:
    version: '3'
    services:
    testapi:
    links:
    - testweb:testweb
    testweb:
    links:
    - testapi:testapi

    关于rest - AWS-ECS-容器之间的通信-未知主机错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46919916/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com