Spring Boot 在localhost域奇怪的404问题(Mac book pro)
2017-08-31 16:30
555 查看
在mac系统中,明明url是对的,浏览器也可以打开,一个简单的代码调用就是404,你有没有遇到过?
调用接口的方式:
大部分情况下,返回如下错误,偶尔成功。
把localhost 改为一个私网IP,页面空白,不报错。
到 bash中查看:
什么情况?
再次检查localhost:
查看端口:
什么鬼?
杀掉node,恢复清明了。
Mac osx 一手造成了坑。ubuntu 测试无坑,启动http-server的情况下,tomcat根本起不来:
现在用springboot起8080,竟然成功,但这个坑就这么挖好了。
有两个进程都使用的8080,spring boot 是localhost:8080 ,他会精神错乱。因为localhost也是127.0.0.1。
奇了怪的是,既然错乱,启动的时候居然不报端口占用。
那么我们现在要明确,localhost指向127.0.0.1,但二者还是不一样,localhost可以看做一个域名。
为了避免入坑,如果可能尽量不使用localhost,直接使用IP。
Tomcat 启动同样的问题。
浏览器一切正常,restTemplate错乱。
情景再现
普通的一个controller,返回一个常量。@GetMapping("/project_metadata/spring-boot") public String getMetadata(){ return "{\"data\":1234}";//这个不重要 }
调用接口的方式:
content = new JSONObject(restTemplate.getForObject(url, String.class));
大部分情况下,返回如下错误,偶尔成功。
2017-08-31 14:35:38.867 INFO 3450 --- [nio-8080-exec-1] .i.w.s.DefaultInitializrMetadataProvider : Fetching boot metadata from http://localhost:8080/project_metadata/spring-boot 2017-08-31 14:35:38.872 WARN 3450 --- [nio-8080-exec-1] .i.w.s.DefaultInitializrMetadataProvider : Failed to fetch spring boot metadata org.springframework.web.client.HttpClientErrorException: 404 Not Found at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
排查
浏览器访问是正常的。把localhost 改为一个私网IP,页面空白,不报错。
到 bash中查看:
curl -I http://10.2.10.203:8080/project_metadata/spring-boot HTTP/1.1 404 Not Found server: ecstatic-1.4.1 Date: Thu, 31 Aug 2017 07:06:39 GMT Connection: keep-alive
什么情况?
再次检查localhost:
curl -I http://localhost:8080/project_metadata/spring-boot HTTP/1.1 200 Content-Type: application/json;charset=UTF-8 Content-Length: 2683 Date: Thu, 31 Aug 2017 07:07:28 GMT
查看端口:
lsof -i:8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 1045 pollyduan 13u IPv4 0x992085ef857b1d07 0t0 TCP *:http-alt (LISTEN) java 3995 pollyduan 65u IPv6 0x992085ef905d994f 0t0 TCP *:http-alt (LISTEN)
什么鬼?
杀掉node,恢复清明了。
坑在哪里?
有两个进程都在监听8080,但ip错乱。Mac osx 一手造成了坑。ubuntu 测试无坑,启动http-server的情况下,tomcat根本起不来:
Caused by: java.net.BindException: Address already in use at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.Net.bind(Net.java:425) at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340) at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:742) at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:458) at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:120) at org.apache.catalina.connector.Connector.initInternal(Connector.java:960) ... 13 more
小结:
完整的坑是这样的,我用node起了一个127.0.0.1:8080 调js,完了没关。现在用springboot起8080,竟然成功,但这个坑就这么挖好了。
有两个进程都使用的8080,spring boot 是localhost:8080 ,他会精神错乱。因为localhost也是127.0.0.1。
奇了怪的是,既然错乱,启动的时候居然不报端口占用。
那么我们现在要明确,localhost指向127.0.0.1,但二者还是不一样,localhost可以看做一个域名。
为了避免入坑,如果可能尽量不使用localhost,直接使用IP。
Tomcat 启动同样的问题。
浏览器一切正常,restTemplate错乱。
相关文章推荐
- 解决Spring Boot 在localhost域奇怪的404问题(Mac book pro)
- 解决Mac book pro外接现实器有黑边问题
- 解决mac book pro经常风火轮问题
- Mac Book Pro 网络连接问题
- tomcat配置问题:访问http://localhost:8080/ 遇到 Access Error: 404
- mac book pro 安装keras (无gpu)
- MAC book pro如何启动mongodb服务
- Mac系统开发常见问题-IntelliJIDEA-TOMCAT-Address localhost:80 is already in use
- nginx+yii奇怪的404问题
- mac book pro 最全快捷键
- Mac系统开发常见问题-IntelliJIDEA-TOMCAT-Unable to ping server at localhost:1099
- mac os lion 上php连接mysql数据库不能使用使用localhost问题
- [MacOSX_系统]_[Mac book pro开机进不去windows或电源适配器显示一个黑色叉]
- mac book pro 休眠后 不能输出声音的解决方案
- mac 遇到的奇怪问题?
- Mac OS X 跟新到10.11后cocoapods安装出现的问题(- bad response Not Found 404 (http://ruby.taobao.org/latest_spe)
- 问题:Tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误
- [MacOSX_系统]_[Mac book pro开机进不去windows或电源适配器显示一个黑色叉]
- 解决Mac下Sequel Pro 1.1 连接 Homebrew安装Mysql5.7.8的问题 Sequel Pro 1.1 encountered an unexpected error
- Mac Book Pro不能识别移动硬盘