SpringBoot之解決雲服務器VPS在所處雲端集羣的內網不能解析域名的問題:java.net.UnknownHostException:abc.cn: Temporary failure in n

1、原由與緣由分析過程html

  前端小夥伴兒告訴我,說服務器崩了。前端

  請求數據接口,接口有響應,但報的json提示指向:數據庫異常錯誤。java

  遂登錄雲主機查看日誌,核心記錄顯示以下:mysql

2018-11-09 22:15:53.824 ERROR 26088 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Caused by: java.net.UnknownHostException: abc.cn: Temporary failure in name resolution【名稱解析的臨時失敗】
	at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_151]
	at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_151]
	at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_151]
	at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[na:1.8.0_151]
	at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_151]
	at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_151]
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:188) ~[mysql-connector-java-5.1.47.jar!/:5.1.47]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301) ~[mysql-connector-java-5.1.47.jar!/:5.1.47]
	... 67 common frames omitted

2018-11-09 22:15:53.825  WARN 26088 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata : Communications link failure

Caused by: java.net.UnknownHostException: abc.cn
	at java.net.InetAddress.getAllByName0(InetAddress.java:1280) ~[na:1.8.0_151]
	at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_151]
	at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_151]
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:188) ~[mysql-connector-java-5.1.47.jar!/:5.1.47]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301) ~[mysql-connector-java-5.1.47.jar!/:5.1.47]
	... 66 common frames omitted

2018-11-09 22:15:56.741  WARN 26088 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
2018-11-09 22:15:56.742 ERROR 26088 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2018-11-09 22:15:56.750  WARN 26088 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
2018-11-09 22:15:56.764  INFO 26088 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-11-09 22:15:56.877  INFO 26088 --- [           main] ConditionEvaluationReportLoggingListener : 

  第一次遇到這問題,有點懵,直接把全部的SpringBoot和tomcat相關進程kill了;重啓SpringBoot項目(jar包);查看日誌,項目啓動失敗。Excuse Me?spring

  搜索該問題UnknownHostException: abc.cn: Temporary failure in name resolution,事情有了起色:懷疑確實是域名解析出了問題,DNS失敗sql

  作出假(huai)設(yi):個人雲主機所屬雲服務器提供商的服務器集羣所處內網不能直接訪問/解析外網的域名,致使DNS失敗數據庫

  驗證假設:apache

    本地電腦Ping域名:ping abc.com ,成功響應IP地址 x.y.z.q【外網解析域名正常】json

 

    在雲主機Ping域名abc.com,未成功響應,以下圖。【雲主機內網解析域名失敗】windows

    驗證成功。

  那麼,如何解決: 雲主機在雲服務器提供商的內部不能解析域名(abc.com),從而DNS解析失敗,致使不能鏈接數據庫所致 的問題?

 

2、解決方案

  原理:相似於平時咱們在本地電腦fq或者破解某些軟件的驗證關時,更改windows的host文件,讓主機的網絡請求DNS優先解析本地的[IP 域名]解析配置。

  擴展子問題1:DNS概念

DNS(Domain Name System,域名系統),因特網上做爲域名和IP地址相互映射的一個分佈式數據庫,可以使用戶更方便的訪問互聯網,而不用去記住可以被機器直接讀取的IP數串。經過主機名,最終獲得該主機名對應的IP地址的過程叫作域名解析(或主機名解析)。

  擴展子問題2:DNS整套解析系統的原理?(忘了?就請去複習)

  解決方法:

    方法1:新增配置雲主機

        step1.HOST 本地DNS解析【僅此方法,已被親測有效】  

vi /etc/hosts

        step2.添加<IP,域名>解析。例如: 

223.231.234.33 www.baidu.com

    方法2:  網卡配置文件DNS服務地址【特色:重啓服務器不會被抹掉配置

vi /etc/sysconfig/network-scripts/ifcfg-eth0

        添加DNS服務器地址。例如: 

DSN1='114.114.114.114'

       【備註】免費公共的DNS有:(詳見:DNS服務器地址

Google DNS 8.8.8.8,8.8.4.4
Public DNS+ 119.29.29.29
阿里 DNS 223.6.6.6,223.5.5.5
百度 DNS 180.76.76.76
360 DNS 電信:101.226.4.6 聯通:123.125.81.6 移動:101.226.4.6 鐵通:101.226.4.6
OpenDNS 208.67.220.220
114DNS 114.114.114.114,114.114.115.115 (有廣告插入嫌疑)

    方法3:系統默認DNS配置【特色:重啓服務器會被抹掉配置,非永久性】

vi /etc/resolv.conf

      添加DNS服務器解析規則。例如:

nameserver 114.114.114.114

    【小結】系統解析的優先級:方法1>方法2>方法3

     方法4:項目不使用域名,直接使用IP地址【特色:治標不治本,不必定適用全部工程狀況】

 

3、結果分析

一、Spring Boot的初始化確定是先於Tomcat的,因此應該是Spring Boot的初始化過程當中出現了阻塞致使應用啓動過慢的問題。

二、Caused by: java.net.UnknownHostException: abc.cn:未知域名異常

三、Linux nano操做

    保存 :Ctrl+O  

    退出:Ctrl+X

四、疑問:爲何以前項目部署就能夠,且沒有出問題呢?根本緣由說明還沒找到

 

4、參考文獻

  [1] LINUX DNS解析的3種修改方法~ 【推薦】

  [2] DNS服務器地址 【推薦】

  [3] Temporary failure in name resolution - DNS引起的問題和思考 【推薦:解題思路清晰】

  [4] /etc/resolv.conf文件詳解

  [5] nano編輯器使用教程

相關文章
相關標籤/搜索