我拥有一个Elastic Beanstalk 环境,其中包含一个运行Spring Boot 服务器的单个 EC2 实例。我认为没有必要讨论部署选项,但如果需要,我会将它们添加为编辑。
最近,我部署了一个连接到我的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,并且它的出站规则似乎既允许又拒绝网络流量:
我无法删除明确否认一切的规则。
我是否应该使用自定义 ACL 和自定义出站规则创建自定义 VPC?仅用于访问外部资源(如 MongoDB 数据库)?我可能错过了什么吗?我是 AWS 新手(实践中),刚刚意识到获得 AWS 认证并不能保证你拥有任何实践技能。
您不应触碰 VPC 的网络 ACL 规则。屏幕截图中的 ACL 允许所有流量,因此网络 ACL 没有问题。
MongoDB Atlas 有一个IP 访问列表设置。您需要将 EC2 实例的公共 IP 添加到该列表中吗?请注意,这必须是实例的公共IP,而不是私有 IP。