簡體   English   中英

Spring RestTemplate響應速度慢

[英]Spring RestTemplate slow response time

嘗試在Spring 3.2上使用RestTemplate POST對象時,平均響應時間為8秒

使用卷曲

time curl -X POST -H "Content-Type: application/xml" -T request.xml https://x.y.com:20000/rest

我的平均時間約為4秒。 我不明白為什么。

我的配置:

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>x.y.z.Request</value>
                <value>x.y.z.Response</value>
                <value>x.y.z.AnotherRequest</value>
                <value>x.y.z.AnotherResponse</value>
            </list>
        </property>
    </bean>


<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"
        scope="prototype">
        <constructor-arg>
            <bean
                class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
                <property name="readTimeout" value="${application.urlReadTimeout}" />
                <property name="connectTimeout" value="${application.urlConnectionTimeout}" />
            </bean>
        </constructor-arg>

        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                    <property name="marshaller" ref="jaxb2Marshaller" />
                    <property name="unmarshaller" ref="jaxb2Marshaller" />
                </bean>
                <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter" />
            </list>
        </property>
    </bean>

然后我簡單地自動接線:

    @Autowired
    RestTemplate restTemplate;

public Response getXml(Request request){

    Response response = restTemplate.postForObject(httpUrl,request, Response.class);
}

PS:作為一種替代方案,我嘗試使用JaxB解析Request / Response對象,然后使用org.apache.http.client.HttpClient發送該對象,平均時間約為7秒,這遠遠不夠。

這可能是ipv4 / 6問題。 嘗試

curl -4 -X POST -H "Content-Type: application/xml" -T request.xml https://x.y.com:20000/rest

如果適合您,請使用param運行Java cmd

-Djava.net.preferIPv4Stack=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM