【问题标题】:Spring RestTemplate slow response timeSpring RestTemplate 响应时间慢
【发布时间】:2013-06-24 13:48:47
【问题描述】:

在 Spring 3.2 上尝试使用 RestTemplate 发布对象时,我的平均响应时间为 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);
}

P.S:作为替代方案,我尝试使用 JaxB 解析请求/响应对象并使用 org.apache.http.client.HttpClient 发送它,平均时间约为 7 秒,这远非好。

【问题讨论】:

  • request.xml 的实际大小是多少?
  • @ZagorulkinDmitry,大约 1000 个字符长。

标签: java spring web-services rest


【解决方案1】:

可能是 ipv4/6 问题。试试

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

如果它适合您,请使用参数运行您的 java cmd

-Djava.net.preferIPv4Stack=true

【讨论】:

  • 你能否详细说明一下,你的意思是ipv6请求可能比ipv4快两倍?
  • 不,我的网络(我不知道到底是谁,可能是网关)首先尝试使用 ipv6 解析 ip,所以 curl -4(或 preferIPv4Stack 参数)强制 ipv4 IP 解析,并且它解决了我的问题
猜你喜欢
  • 2013-04-30
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
相关资源
最近更新 更多