简体   繁体   English

Spring RestTemplate响应速度慢

[英]Spring RestTemplate slow response time

While trying to POST object using RestTemplate on Spring 3.2 I got average response time 8 seconds 尝试在Spring 3.2上使用RestTemplate POST对象时,平均响应时间为8秒

using curl 使用卷曲

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

I'm getting approximately 4 sec average time. 我的平均时间约为4秒。 I can't understand why. 我不明白为什么。

My configuration: 我的配置:

<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>

Then I simple autowire it: 然后我简单地自动接线:

    @Autowired
    RestTemplate restTemplate;

public Response getXml(Request request){

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

PS: as an alternative I tried to parse Request/Response object using JaxB and an send it using org.apache.http.client.HttpClient the average time is around 7 seconds, which is far from being good. PS:作为一种替代方案,我尝试使用JaxB解析Request / Response对象,然后使用org.apache.http.client.HttpClient发送该对象,平均时间约为7秒,这远远不够。

it might be a ipv4/6 issue. 这可能是ipv4 / 6问题。 try 尝试

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

if it works for you, run your java cmd using the param 如果适合您,请使用param运行Java cmd

-Djava.net.preferIPv4Stack=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM