概述
應(yīng)用客戶端需要先通過Azure網(wǎng)關(guān)(Gateway)才能連接到Azure上的PaaS數(shù)據(jù)庫(例如Azure SQL Database,MySQL Database on Azure等),網(wǎng)關(guān)連接的默認(rèn)設(shè)置是四分鐘,若四分鐘內(nèi)無數(shù)據(jù)包發(fā)送,該連接會過期。
為避免連接過期出現(xiàn)報錯,客戶可以選擇采用連接池的方式,通過為每個給定的連接配置保留一組活動連接來更好地管理連接。此外客戶也可以選擇設(shè)置發(fā)送心跳信號的方式確保所用連接一直處于活動狀態(tài),以避免被網(wǎng)關(guān)認(rèn)定為過期。
對于SQL Server Management Studio和MySQL Workbench等工具而言,其中已經(jīng)內(nèi)置了心跳信號設(shè)置功能;而如果用的是客戶自行開發(fā)的應(yīng)用,就需要手動設(shè)置心跳信號,具體方法可參考以下內(nèi)容。
設(shè)置心跳信號
以下分別為在Windows系統(tǒng)和Linux系統(tǒng)中設(shè)置心跳信號的方法,參數(shù)修改值可酌情自選。
Windows系統(tǒng)
可以打開"運行",輸入regedit,修改(如果沒有則添加)注冊表:
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParametersKeepAliveTime
打開Parameters后,在右側(cè)界面空白處鼠標(biāo)單擊右鍵創(chuàng)建DWORD(32位)值。
把名字修改為KeepAliveTime,值選十進(jìn)制,輸入60000。
之后重啟服務(wù)器以使注冊表更改生效。
Linux系統(tǒng)
對于Linux客戶端而言,需要修改以下四個keepalive參數(shù):
tcp_keepalive_probes-the number of probes that are sent and unacknowledged before the client considers the connection broken and notifies the application layer
tcp_keepalive_time-the interval between the last data packet sent and the first keepalive probe
tcp_keepalive_intvl-the interval between subsequent keepalive probes
tcp_retries2-the maximum number of times a packet is retransmitted before giving up
修改的方法是在Linux上運行以下四個echo命令:
echo"6">/proc/sys/net/ipv4/tcp_keepalive_time
echo"1">/proc/sys/net/ipv4/tcp_keepalive_intvl
echo"10">/proc/sys/net/ipv4/tcp_keepalive_probes
echo"3">/proc/sys/net/ipv4/tcp_retries2
tcp_keepalive_time和tcp_keepalive_intvl值的單位是秒。若要使得修改的值在系統(tǒng)重啟后仍然生效,需要將這兩個參數(shù)添加到/etc/sysctl.conf中。