【问题标题】:RESTLET response time getting too slow when deployed in server部署在服务器中时,RESTLET 响应时间变得太慢
【发布时间】:2016-12-22 11:49:50
【问题描述】:

我创建了一个使用 POST/Json 的基于 Restlet 的 Web 服务。

问题是在本地机器上测试时,

效果很好。但是当它部署在服务器上时,它

比在我的本地开发中测试时多花费大约 10 秒

机器获得响应,即使没有其他进程使用

服务器的资源。

【问题讨论】:

    标签: java apache-httpclient-4.x restlet-2.0


    【解决方案1】:

    我通过使用 Apache Http Client 而不是解决了这个问题

    来自 RESTlet 的 ClientResource。

    我在远程 Linux 服务器上为同一个基于 RESTlet 的 Web 服务测试了 ClientResource 和 Apache HTTP。前者耗时约 10 秒,而后者 后者不到 1 秒。我不知道为什么。

    public class Test 
    {
        public static void main(String [] args)
        {
     
            HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead 
    
            try {
                HttpPost request = new HttpPost("http://hostname:port/testService");
                StringEntity params =new StringEntity("{\"key\" : \"value\"}");
                request.addHeader("content-type", "application/json");
                request.addHeader("Accept", "application/json");
                request.setEntity(params);
                HttpResponse response = httpClient.execute(request);
    
                String json_string = EntityUtils.toString(response.getEntity());
                
                System.out.println(json_string);
            }catch (Exception ex) {
                // handle exception here
            } finally {
                httpClient.getConnectionManager().shutdown(); //Deprecated
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 2011-05-18
      • 2018-11-16
      • 1970-01-01
      • 2014-03-25
      • 2019-08-03
      • 1970-01-01
      相关资源
      最近更新 更多