无法从 Elastic Beanstalk 的 EC2 实例访问外部 MongoDB Atlas 数据库

问题描述 投票:0回答:1

我拥有一个 Elastic Beanstalk 环境,其中包含一个 单个 EC2 实例,运行一个 Spring Boot 服务器。我认为没有必要谈论部署选项,但如果需要的话,我会将它们添加为编辑。

最近,我部署了一个连接到我的 MongoDB 数据库的代码(使用通过 MongoDB Atlas 托管的外部源),该代码在本地完美运行。当我尝试访问某个页面时,我的 Spring Boot 应用程序当前会列出该数据库中的项目。

在远程,它可以完美工作,直到我到达尝试查询数据库的某个页面。尝试查看 Beanstalk 的

web.stdout.log
文件,我得到了一些例外:

Dec 18 10:07:45 ip-172-31-46-201 web[182140]: 2024-12-18T10:07:45.757Z ERROR 182140 --- [mockapi] [nio-5000-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataAccessResourceFailureException: Timed out while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=REPLICA_SET, servers=[{address=docsbymario-shard-00-00.xyx0n.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLException: Received fatal alert: internal_error}}, {address=docsbymario-shard-00-01.xyx0n.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLException: Received fatal alert: internal_error}}, {address=docsbymario-shard-00-02.xyx0n.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLException: Received fatal alert: internal_error}}]] with root cause
Dec 18 10:07:45 ip-172-31-46-201 web[182140]: com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=REPLICA_SET, servers=[{address=docsbymario-shard-00-00.xyx0n.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLException: Received fatal alert: internal_error}}, {address=docsbymario-shard-00-01.xyx0n.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLException: Received fatal alert: internal_error}}, {address=docsbymario-shard-00-02.xyx0n.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLException: Received fatal alert: internal_error}}]

通过猜测,我认为这样的异常来自访问数据库 URL 的问题(即在 application.properties 文件中定义的

properly
)。

EC2 安全组允许每个地址到每个端口的出站流量。

我使用的是默认VPC,没有创建自定义VPC,它的出站规则似乎既允许又拒绝网络流量: enter image description here

我无法删除明确拒绝一切的规则。

我是否应该使用自定义 ACL 和自定义出站规则创建自定义 VPC?仅访问 MongoDB 数据库等外部资源?我可能错过了什么吗?我是 AWS 新手(在实践中),刚刚意识到获得 AWS 认证并不能保证您拥有任何实用技能。

amazon-web-services amazon-elastic-beanstalk
1个回答
1
投票

您不应触及 VPC 的网络 ACL 规则。屏幕截图中的 ACL 允许所有流量,因此网络 ACL 不是问题。

MongoDB Atlas 有一个 IP 访问列表 设置。您需要将 EC2 实例的公共 IP 添加到该列表吗?请注意,这必须是实例的 public IP,而不是私有 IP。

© www.soinside.com 2019 - 2024. All rights reserved.