Łuk*_*ski 3 java rest groovy spring-web
我RestTemplate
在java中使用spring 编写REST客户端,在使用'HTTPBuilder'时使用groovy 编写REST客户端.两次调用都在我的电脑上花了10秒钟.在Postman和其他工具中,这种相同的帖子请求需要300毫秒.
这是groovy脚本中的简单调用.必需是间BEGIN REST invocation
和END REST invocation
:
@Grapes(
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
)
import groovy.json.JsonOutput
import groovy.json.internal.LazyMap
import groovy.transform.ToString
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.POST
HTTPBuilder http = new HTTPBuilder()
Forechart forechart = new Forechart(dayInterval: 3, adultCount: 1, childCount: 0,
flightList: [new Flight(departureStation: "WAW", arrivalStation: "CRL", date: new Date(2018, 06, 02))
,new Flight(departureStation: "CRL", arrivalStation: "WAW", date: new Date(2018, 10, 26))])
def jsonRequest = JsonOutput.toJson( forechart )
LazyMap jsonResponse
//BEGIN REST invocation
println "Request: " + JsonOutput.prettyPrint(jsonRequest)
long start = System.nanoTime()
http.request( 'https://be.wizzair.com/7.15/Api/asset/farechart', POST, JSON ) { req ->
body = jsonRequest
headers.Accept = 'application/json'
response.success = { resp, json ->
assert resp.statusLine.statusCode == 200
jsonResponse = json
}
response.'404' = {
println 'Not found'
}
}
long end = System.nanoTime() - start
//println "Response: $jsonResponse"
println "\nDuration: " + end / 1_000_000_000 + " seconds"
//END REST invocation
ForechartFlights forechartFlights = new ForechartFlights(jsonResponse)
//Data Objects
@ToString(includePackage = false)
class Forechart {
boolean wdc
List<Flight> flightList
int dayInterval
int adultCount
int childCount
boolean isRescueFare
}
@ToString(includePackage = false)
class Flight {
String departureStation
String arrivalStation
Date date
}
//response
@ToString(includePackage = false)
class ForechartFlights {
List<ForechartFlight> outboundFlights
List<ForechartFlight> returnFlights
}
@ToString(includePackage = false)
class ForechartFlight {
String departureStation
String arrivalStation
String priceType
String classOfService
boolean hasMacFlight
Date date
Price price;
}
@ToString(includePackage = false)
class Price {
BigDecimal amount
String currencyCode
}
Run Code Online (Sandbox Code Playgroud)
是什么原因?
看来这个请求需要花费很长时间(8秒以上)使用curl:
~> time curl -v -d "@post.data" -H "Content-Type: application/json" https://be.wizzair.com/7.14.0/Api/asset/farechart
* Trying 104.122.232.212...
* TCP_NODELAY set
* Connected to be.wizzair.com (104.122.232.212) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: wizzair.com
* Server certificate: DigiCert SHA2 Extended Validation Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> POST /7.14.0/Api/asset/farechart HTTP/1.1
> Host: be.wizzair.com
> User-Agent: curl/7.59.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 407
>
* upload completely sent off: 407 out of 407 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Cache-Control: no-cache,no-store
< Pragma: no-cache
< Expires: -1
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
< Strict-Transport-Security: max-age=31536000
< Server:
< Date: Sun, 27 May 2018 18:07:43 GMT
< Content-Length: 10023
< Connection: keep-alive
< Set-Cookie: ASP.NET_SessionId=hnrxfwuxvkxo34k3zhcljnce; path=/; secure; HttpOnly
< Set-Cookie: _abck=83611DFC82637AC8726723372C6A8EFC50EFF7BD2A040000EFF30A5B01AC6952~-1~Ip07wEKIXzwehsarZ0eh+sBBuiiRDje8DAQpi80fdXk=~-1~-1; expires=Mon, 27 May 2019 18:07:43 GMT; max-age=31536000; path=/; domain=.wizzair.com
< Set-Cookie: bm_sz=41F805925F8BB0BCFE47CBE64B3531A2~QAAQvffvUJsv45ZjAQAAId/IorrnukpLPnmZ8H7WRPTEdCk0rMrrIOdi+wLlRh3DFodvGU9Mu5gq0MDfhTtHxwo4hm0DC2IT5Ql6PrXWlY59p5wf57bBed+P4Lq5+bX7XtlNRFTFda9yjVVzYbgknJ30w8JOrE2prYAGu1mh/9qkFPzvKkJXjGcz7QA8eLUS; Domain=.wizzair.com; Path=/; Expires=Sun, 27 May 2018 22:07:35 GMT; Max-Age=14392; HttpOnly
<
<snip>response data here</snip>
real 0m8.386s
user 0m0.024s
sys 0m0.022s
Run Code Online (Sandbox Code Playgroud)
我首先将请求数据保存在文件中的位置post.data
.
我怀疑请求中有一些标题或其他细节在需要很长时间的调用(如此curl和groovy示例)和不需要的调用之间不相同.
如果您使用的是像Postman这样的工具,请检查请求标题,看看是否有一些我们没有发送的内容需要花费很长时间.
进一步测试后更新
我已经用邮递员验证了这一点(即邮差很快,java和curl速度慢)并且标题没有区别.我相信这与处理https和TLSv1.2有关.
虽然可能是远远的罪魁祸首可能是报告的慢速(非硬件加速)AES GCM加密,TLSv1.2(网站根据上述卷曲跟踪使用)允许:
Java Server,TLSv1.1快,TLSv1.2极慢(90MByte/sec对4MByte/sec)
经过一些取证后更新2
通过在运行测试时将以下标志发送到groovy(或java):
-Djavax.net.debug=all
Run Code Online (Sandbox Code Playgroud)
我们可以看到客户端脚本接收加密数据:
<snip a ton of network and tls detail>
05B0: 9E EA 98 06 6B 91 EC 2A 76 C8 F3 39 81 88 C5 30 ....k..*v..9...0
05C0: 02 34 45 E9 8C 12 EE 07 E3 31 C7 5C 8F DB FD C9 .4E......1.\....
05D0: 80 F4 0A F7 B4 57 CC 68 EF 96 70 7C 5E B6 .....W.h..p.^.
main, READ: TLSv1.2 Application Data, length = 1502
Padded plaintext after DECRYPTION: len = 1478
0000: 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D HTTP/1.1 200 OK.
0010: 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 61 .Content-Type: a
0020: 70 70 6C 69 63 61 74 69 6F 6E 2F 6A 73 6F 6E 3B pplication/json;
0030: 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A charset=utf-8..
00
<snip - HttpOnly is the end of the last response cookie, after that
I'm assuming the encrypted data starts>
0410: 3B 20 48 74 74 70 4F 6E 6C 79 0D 0A 0D 0A 1F 8B ; HttpOnly......
0420: 08 00 00 00 00 00 00 03 CD 98 C1 4A C3 40 14 45 ...........J.@.E
0430: FF 65 D6 09 CC 4C 6D D4 EC A4 E2 AA 45 B0 85 2E .e...Lm.....E...
<snip - more encrypted data>
0590: 1C 78 6E C4 5E C0 16 39 F0 DC 68 77 99 22 C7 16 .xn.^..9..hw."..
05A0: 39 EC DC B0 45 0E CC 46 BC 2F 60 8B 1C 78 6E C4 9...E..F./`..xn.
05B0: FB 02 B6 C8 81 E7 46 BC 2F 9C 5D E4 4E 6F FC FF ......F./.].No..
05C0: 20 5E 27 27 00 00 ^''..
<THIS IS WHERE WE HANG FOR A LONG TIME>
Duration: 10.904149839 seconds
Run Code Online (Sandbox Code Playgroud)
(我们看到大量输出,但最后我们收到了加密数据)
然后在那里挂了很长时间.
换句话说,不是接收数据或服务器需要很长时间.我们已有数据然后挂起.
我假设这是接收数据的解密需要时间,我会将其归因于上述慢速解密或java ssl的一些类似问题.
不坚如磐石,但似乎至少是一个似是而非的理论.
假设上述理论成立,我们仍然想知道为什么卷曲很慢.
归档时间: |
|
查看次数: |
362 次 |
最近记录: |