1. 程式人生 > >微信開發:NAT穿透

微信開發:NAT穿透

原帖收藏於IT老兵部落格

微信開發:NAT穿透。

前言

除錯微信,遇到問題,微信無法直接通知到開發機上,而是需要一臺伺服器的地址上,這給除錯增加了難度,上網研究了一下,如果在windows下,可以使用花生殼軟體,進行NAT穿透,但是花生殼不支援mac,又找了找,發現了ngrok,這個配置很簡單,解決了問題。

正文

一共需要四步:

Download ngrok
First, download the ngrok client, a single binary with zero run-time dependencies.
Mac OS XWindows Linux Mac (32-bit) Windows (32-bit)Linux (ARM) Linux (ARM64) Linux (32-bit)FreeBSD (64-Bit) FreeBSD (32-bit)

第一步,下載ngrok,它支援Mac、Linux、Windows等多個作業系統。

Unzip to install
On Linux or OSX you can unzip ngrok from a terminal with the following command. On Windows, just double click ngrok.zip.
unzip /path/to/ngrok.zip
Most people like to keep ngrok in their primary user folder or set an alias for easy command-line access.

第二步,解壓縮。

Connect your account
Running this command will add your authtoken to your ngrok.yml file. Connecting an account will list your open tunnels in the dashboard, give you longer tunnel timeouts, and more. Visit the dashboard to get your auth token.

./ngrok authtoken <YOUR_AUTH_TOKEN>

Don’t have an account?
Sign up for free to get your auth token.

第三步,註冊賬號,點選上面的Sign up連結,去註冊,或者使用谷歌賬戶授權,有了授權,這個工具會提供更好的支援。

Fire it up
Try it out by running it from the command line:
./ngrok help
To start a HTTP tunnel on port 80, run this next:
./ngrok http 80
Read the documentation to get more ideas on how to use ngrok.

第四部,開始使用,./ngrok help 可以獲取幫助,./ngrok http 80 開始執行,注意80是你想要反向代理的埠,這個要根據你的需求來設定,例如這裡是tomcat,埠是8080,那麼上面要改成8080。

在這裡插入圖片描述

執行起來之後,它會給你分配一個動態的域名,如上圖,在瀏覽器訪問這個域名,就可以訪問到你的服務了。

總結

工欲善其事,必先利其器。微信開發,如果不配置好這個回撥,那麼每次都需要部署到伺服器上才能驗證,這樣效率會低很多。