java - 如何使 Spring Boot 应用程序可以通过服务器的外部 IP 地址访问?
问题描述
我是 Spring Boot 的新手。我有一个用 Spring Boot 编写的 REST API 应用程序。当我执行 Spring Boot JAR 时,一切正常,我可以使用localhost
地址而不是实际地址访问 REST API:
http://localhost:8083/articles
但是当我尝试通过我的外部 IP 地址访问 REST API 时,我做不到:
http://100.90.80.70:8083/articles
netstat -antu
Linux 终端中的命令给了我以下输出:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp6 0 0 :::8083 :::* LISTEN
据我了解,我的应用程序只能在 中访问localhost
,因为它没有foreign address
.
我的application.properties
文件只有这一行:
server.port=8083
另外,当我尝试添加server.address
一行时application.properties
:
server.address=100.90.80.70
server.port=8083
我有以下内容Exception
:Caused by: java.net.BindException: Cannot assign requested address
。
所以我的问题是:如何让服务器的外部 IP 地址可以访问 Spring Boot 应用程序?谢谢你。
解决方案
正如@Mark 所说,问题出在防火墙上。我8083
在防火墙设置中打开了端口,现在我可以通过外部 IP 地址访问我的 REST API 应用程序:
http://100.90.80.70:8083/articles
检查防火墙状态的 Linux 命令:
sudo ufw status verbose
8083
通过 TCP 协议为远程访问打开端口:
sudo ufw allow 8083/tcp
更多设置在这里:https ://www.cyberciti.biz/faq/how-to-open-firewall-port-on-ubuntu-linux-12-04-14-04-lts/
推荐阅读
- c++ - `const` 作为移动构造函数的模板参数的效果
- java - 创建一个 `Copyable` 类型的接口而不是使用 `Cloneable` 有意义吗?
- android - 在 Firebase 数据库中删除节点时遇到问题
- python - 不显示文件夹中的任何 csv 文件
- mysql - Apache Superset 通过 Spark 连接到 Mariadb
- c++ - 使用 Nvidia 编码 SDK 制作的编码视频不正确
- ios - Color 与 UIColor 支持的默认亮/暗模式颜色
- php - 在 curl &simple_html_dom 中抓取文本
- firebase - Flutter Firestore 相同文档的两个 StreamBuilder 算作 2 次读取?
- r - 3.6 R 中不可用的数据集