首页 > 解决方案 > 如何使 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 -antuLinux 终端中的命令给了我以下输出:

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

我有以下内容ExceptionCaused by: java.net.BindException: Cannot assign requested address

所以我的问题是:如何让服务器的外部 IP 地址可以访问 Spring Boot 应用程序?谢谢你。

标签: javaspring-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/


推荐阅读