微信支付或者支付宝支付验签太慢

调用微信支付或者支付宝验签的时候会很慢,发现程序没有问题,主要是请求到返回这段时间太长。
具体如下: prepayInfo->prepay response 大概用了6秒的时间
 
优化前
[08 14:10:51,081 INFO ] [http--8081-3$598667353] util.WxmppayUtil - Wxmppay source str:
[08 14:10:51,171 INFO ] [http--8081-3$598667353] util.WxmppayUtil - Wxmppay xml str:
[08 14:10:51,171 INFO ] [http--8081-3$598667353] util.WxmppayUtil - Wxpay request prepayInfo:
[08 14:10:56,779 INFO ] [http--8081-3$598667353] util.WxmppayUtil - WX prepay response:
[08 14:10:56,800 INFO ] [http--8081-3$598667353] util.WxmppayUtil - Wxpay prepay id str: wx2017020814105685b74eb4c00453577375
[08 14:10:56,800 INFO ] [http--8081-3$598667353] util.WxmppayUtil - Wxmppay source str: appId=wx9006127d7b209213&nonceStr=dcdgdihjeccjdehbagaihfficgbagaei&package=prepay_id=wx2017020814105685b74eb4c00453577375&signType=MD5&timeStamp=1486534256&key=j5oSq7yOVet9DiWWZOcSTbqzm1KhKOZZ





安装nslook dig tcpdump 工具查看域名解析部分


yum install bind-utils

yum install tcpdump


[root@localhost opt]# dig www.baidu.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.4 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16260
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.            IN    A

;; ANSWER SECTION:
www.baidu.com.        975    IN    CNAME    www.a.shifen.com.
www.a.shifen.com.    22    IN    A    220.181.112.244
www.a.shifen.com.    22    IN    A    220.181.111.188

;; Query time: 0 msec
;; SERVER: 218.30.26.68#53(218.30.26.68)
;; WHEN: Wed Feb  8 14:21:15 2017
;; MSG SIZE  rcvd: 90



[root@localhost opt]# dig api.mch.weixin.qq.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.4 <<>> api.mch.weixin.qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 49081
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;api.mch.weixin.qq.com.        IN    A

;; ANSWER SECTION:
api.mch.weixin.qq.com.    123    IN    CNAME    forward.qq.com.
forward.qq.com.        76    IN    A    123.151.79.109
forward.qq.com.        76    IN    A    123.151.71.149

;; Query time: 1 msec
;; SERVER: 218.30.26.68#53(218.30.26.68)
;; WHEN: Wed Feb  8 14:21:42 2017
;; MSG SIZE  rcvd: 93






[root@localhost opt]# nslookup api.mch.weixin.qq.com
Server:        218.30.26.68
Address:    218.30.26.68#53

Non-authoritative answer:
api.mch.weixin.qq.com    canonical name = forward.qq.com.
Name:    forward.qq.com
Address: 123.151.79.109
Name:    forward.qq.com
Address: 123.151.71.149




[root@localhost opt]# nslookup www.baidu.com
Server:        218.30.26.68
Address:    218.30.26.68#53

Non-authoritative answer:
www.baidu.com    canonical name = www.a.shifen.com.
Name:    www.a.shifen.com
Address: 220.181.112.244
Name:    www.a.shifen.com
Address: 220.181.111.188






通过wget分析域名解析的时间, 发现每次都会去域名服务器解析。


具体功过查看dns查询包
tcpdump -i eth1  host 8.8.8.8 -X -e -nn



后来安装本地域名缓存, centos下安装命令如下:

yum install nscd

vim /etc/nscd.conf
service nscd status

service nscd restart


安装后通过wget再次访问时速度明显加快, 但是通过程序来访问还是很慢。

 

 


代码中是通过来发送请求的, PoolingHttpClientConnectionManager 初始化没有传入DnsResolver,默认使用的是 SystemDefaultDnsResolver,具体代码如下:

 

 

 

    public static final SystemDefaultDnsResolver INSTANCE = new SystemDefaultDnsResolver();

    @Override
    public InetAddress[] resolve(final String host) throws UnknownHostException {
        return InetAddress.getAllByName(host);
    }
 
    看到上面代码最终是调用 InerAddress.getAllByName(host); 方法



优化后

[08 16:12:02,539 INFO ] [http--8081-11$762658730] util.WxmppayUtil - Wxmppay source str:
[08 16:12:02,540 INFO ] [http--8081-11$762658730] util.WxmppayUtil - Wxmppay xml str:
[08 16:12:02,541 INFO ] [http--8081-11$762658730] util.WxmppayUtil - Wxpay request prepayInfo:
[08 16:12:02,542 INFO ] [http--8081-11$762658730] util.CwCachedDnsResolver - get address from cached dns
[08 16:12:02,806 INFO ] [http--8081-11$762658730] util.WxmppayUtil - WX prepay response:
[08 16:12:02,811 INFO ] [http--8081-11$762658730] util.WxmppayUtil - Wxpay prepay id str: wx20170208161202b47a6324710779458744
[08 16:12:02,811 INFO ] [http--8081-11$762658730] util.WxmppayUtil - Wxmppay source str:

 

你可能感兴趣的:(java)