When calling other third-party API interfaces such as Tencent in the program, there will be a long return time. The cause is that when accessing the URL of the API interface, it takes a long time to resolve the DNS. You need to manually add the domain name resolution of the third-party API interface website or modify the DNS configuration, and use the public DNS server preferentially. For example:
Write the go language program to call the Tencent Map API, and output the return time. When domain name resolution is not added or the DNS server configuration is not modified, the return time is about 10 seconds, as shown in the figure:
You can optimize the return time by modifying the DNS server priority setting as follows:
vi /etc/resolv.conf It can be seen that the public DNS server 188.8.131.52 has the lowest priority, as shown in the figure:
Edit the file and write the 184.108.40.206 entry of the public DNS server to the front, that is, adjust the priority of the 220.127.116.11 server to the highest, as shown in the figure:
wq to quit editing and save the file, execute the test program again, then the return time is reduced to about 300ms, as shown in the figure:
Another way is to modify the /etc/hosts file and add the domain name resolution of the calling API website in the file, as shown in the figure:
After saving the file, execute the test program again, then the return time is further reduced to about 80ms, as shown in the figure:
Using this method can save the time for domain name resolution in the public DNS server, but there are two disadvantages:
If you need to call more API websites, you need to manually add one by one
The added resolution is static resolution, just one of several IPs that access to the website. If the resolved IP address cannot be accessed for some reason, it will return to the public DNS server for resolution. If the resolved IP is not modified, the long calling time will still occur.
In summary, modify the DNS server priority or add local resolution according to your own situation.