简体   繁体   English

通过 AWS ECS 服务发现将前端应用程序与后端连接起来

[英]Connecting Frontend application with Backend via AWS ECS Service Discovery

I have a web application that consists of Frontend (built on ReactJS) and Backend (built on Python).我有一个 web 应用程序,它由前端(基于 ReactJS)和后端(基于 Python)组成。

Frontend: I have created a ECS Fargate service and attached aws application load balancer.前端:我创建了一个 ECS Fargate 服务并附加了 aws 应用程序负载均衡器。

Backend: I have created a ECS Fargate service and attached ECS Service discovery.后端:我创建了一个 ECS Fargate 服务并附加了 ECS 服务发现。

Now, when I try to make request to the backend via service discovery, the response from backend never came.现在,当我尝试通过服务发现向后端发出请求时,后端的响应从未到来。 Here my frontend code snippet:这是我的前端代码片段:

axios.get("http://be.be:5000/", {"abc":"bcd"})
                    .then(response => {
                        alert(response['data'])
                        console.log(response)
                        return response.data
                    })
                    .catch(error => {
                        console.log(error)

                    })

Here, 'be.be' is the URL provided by AWS ECS service discovery.这里的“be.be”是AWS ECS服务发现提供的URL。 Instead of "http://be.be:5000/" if I put public of the task like this: "http://public-ip:5000/" it works perfectly.如果我像这样公开任务,而不是“http://be.be:5000/”:“http://public-ip:5000/” 它可以完美运行。

[Note: I have created an ec2 server on the same VPC and tried to dig +short http://be.be , it works perfectly as well] [注意:我在同一个 VPC 上创建了一个 ec2 服务器并尝试挖掘 +short http://be.be ,它也可以正常工作]

What I am missing here?我在这里缺少什么?

You can't use service discovery (SD) names over the Internet from your front-end.不能从前端通过 Internet 使用服务发现 (SD) 名称。 They are only usable within a VPC, not from the outside.它们只能在 VPC 内使用,不能从外部使用。 This is because SD uses only private IP addresses which can't be used over the internet.这是因为 SD 仅使用无法通过 Internet 使用的私有 IP地址。 From docs :来自文档

The DNS records created for a service discovery service always register with the private IP address for the task, rather than the public IP address, even when public namespaces are used.为服务发现服务创建的 DNS 记录始终注册到任务的私有 IP 地址,而不是公共 IP 地址,即使使用公共命名空间也是如此。

For traffic over the internet, you have to use your load balancer.对于 Internet 上的流量,您必须使用负载均衡器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM