因此,您在 spring boot 应用程序中运行 tomcat,从 maven spring boot 插件开始,它在 localhost 上侦听,但无法使用您的 LAN IP 地址访问。
请注意 - SO 上的人们很乐意提供帮助,但获取必要信息不应该是寻宝。
第一
查看tomcat监听的是哪个IP地址和端口:spring boot log通常会告诉你主机名,例如mymac.local,端口会显示出来,端口:8080 (http),:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.2.3.RELEASE)
2015-04-29 09:14:33.269 INFO 7031 --- [main] hello.Application Starting Application v0.1.0 on mymac.local with PID 7031 (/Users/foo/Development/java/springframework/gs-spring-boot/complete/target/gs-spring-boot-0.1.0.jar started by foo in /Users/foo/Development/java/springframework/gs-spring-boot/complete)
2015-04-29 09:14:35.072 INFO 7031 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-04-29 09:14:35.506 INFO 7031 --- [main] o.apache.catalina.core.StandardService : Starting service Tomcat
2015-04-29 09:14:35.507 INFO 7031 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.20
并且作为一项规则,tomcat 开始侦听所有 IP 地址 (0.0.0.0),所以很奇怪这对您不起作用。
另一种方法是使用netstat。对于 Max/Linux:
$ netstat -an | grep 8080
tcp46 0 0 *.8080 *.* LISTEN
在窗户上
C:\> netstat -an | find "8080"
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING
TCP [::]:8080 [::]:0 LISTENING
如果您的应用正在侦听 0.0.0.0 或像 192.168.0.100 之类的真实 IP,那么您可能会以某种方式在网络上被防火墙或阻止。
如果您的应用仅在 localhost/127.0.0.1 上侦听,那么您需要告诉 tomcat 使用真实 IP,例如这个问题:
How to configure embedded Tomcat integrated with Spring to listen requests to IP address, besides localhost?
其次
使用 curl/telnet/netcat 等简单客户端进行基本连接。浏览器,尤其是 IE,可能会受到诸如 Windows 组策略之类的限制,这将阻止许多事情在很少或没有信息的情况下工作。
所以在另一台机器上,看看你是否可以telnet到tomcat端口(假设tomcat在192.168.0.100上运行):
$ telnet 192.168.0.100 8080
Trying 192.168.0.100...
Connected to localhost.
Escape character is '^]'.
如果您看到 已连接 消息,则说明您已连接,因此如果在浏览器中无法正常工作,则说明存在其他问题。
如果立即被拒绝,则两台计算机之间没有路由/连接。这通常意味着你的 tomcat 没有监听正确的 IP 地址
$ telnet 192.168.0.100 8080
Trying 192.168.0.100...
telnet: connect to address 192.168.0.100: Connection refused
telnet: Unable to connect to remote host
如果它只是挂在连接消息上,那么它可能是防火墙。
$ telnet 192.168.0.100 8080
Trying 192.168.0.100...