代码之家  ›  专栏  ›  技术社区  ›  Mario Mateaș

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

  •  0
  • Mario Mateaș  · 技术社区  · 12 月前

    我拥有一个 弹性豆茎环境 持有a 单个EC2实例 ,运行a 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 文件)。

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

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

    我无法删除明确否认一切的规则。

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

    1 回复  |  直到 12 月前
        1
  •  1
  •   Mark B    12 月前

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

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