Я использую Spring RestTemplate для извлечения объектов из спокойного сервера вместе с Interceptor для базовой аутентификации. Однако я испытываю огромные различия в производительности между localhost и средой разработки.
При запуске клиентского и restful сервера локально, RestTemplate getForObject занимает 3 секунды. При запуске клиента на локальном и спокойном сервере в DEV это занимает 59 секунд. Я поместил некоторые записи в Interceptor - в DEV, оставшийся сервер отвечает довольно быстро (за 1 секунду). Причиной задержки является getForObject, поэтому я полагаю, что в DEV RestTemplate требуется много времени для преобразования тела ответа в объект.
Я делаю один и тот же вызов в localhost и DEV, поэтому ожидаю, что размер тела ответа будет одинаковым. Кто-нибудь знает, как улучшить производительность в DEV? Большое спасибо!
Перехватчик:
public class BasicAuthInterceptor implements ClientHttpRequestInterceptor {
public static final Log log = LogFactory.getLog(BasicAuthInterceptor.class);
private String username;
private String password;
public BasicAuthInterceptor( String username, String password ) {
this.username = username;
this.password = password;
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
//Build the auth-header
final String auth = username + ":" + password;
final byte[] encodedAuth = Base64.encodeBase64( auth.getBytes( Charset.forName( "US-ASCII" ) ) );
final String authHeader = "Basic " + new String( encodedAuth );
//Add the auth-header
request.getHeaders().add( "Authorization", authHeader );
log.info("Sending request to the data server");
ClientHttpResponse response = execution.execute(request, body);
log.info("Got response from the data server. Status: " + response.getStatusCode() + " " + response.getStatusText());
log.info("Response header: " + response.getHeaders());
return response;
} }
Вызов RestTemplate:
RestTemplate restTemplate = new RestTemplate();
final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add( new BasicAuthInterceptor( "user", "xxx" ) );
restTemplate.setInterceptors( interceptors );
AggregatedCptyExposure[] expoIq = restTemplate.getForObject(fullUrl, AggregatedCptyExposure[].class);
Из журнала я заметил, что заголовки ответа различаются между DEV и local.
Местное время:
INFO 09/12 13:41:00.160 {} [pool-6-thread-8] BasicAuthInterceptor - Response header: {Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[Wed, 12 Sep 2018 17:41:00 GMT]}
DEV:
INFO 09/12 13:44:05.800 {} [pool-6-thread-13] BasicAuthInterceptor - Response header: {Date=[Wed, 12 Sep 2018 17:44:05 GMT], Set-Cookie=[X-Session-Token=mode&BASICAUTH&user&name&token&AAEAAAFlzuIHW2wljAAXQ5zrkYZFE75H7p9MfbH6REVWICAgICAgIAEnuj5LmEhn8vdL8atZ9j6DGGmXSimKUUSEc%2BpPv6uJ2A%3D%3D; path=/; HttpOnly, ROUTEID=.2; path=/, ROUTEID=.2; path=/, EncryptCookie=!MUIcv63qIjUiv0DaswGZ/7EuVxdhGlpB0wRK4L7mIcd8CeqPrqCaQXDpIomEbw5h3/vRHDvPYFxjQQ==; expires=Wed, 12-Sep-2018 21:44:05 GMT; path=/; Httponly; Secure], Strict-Transport-Security=[max-age=31536000], Content-Type=[application/json;charset=UTF-8], Vary=[Accept-Encoding], X-Frame-Options=[SAMEORIGIN], Content-Security-Policy-Report-Only=[default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' 'unsafe-eval'; report-uri ....], X-Content-Type-Options=[nosniff], Access-Control-Allow-Origin=[.....], Access-Control-Allow-Credentials=[true], X-XSS-Protection=[1; mode=block], X-Route-Ref=[Route:2;dcts01aa/1=dcts03;2=dcts01; P59357], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Keep-Alive=[timeout=5, max=100], Connection=[Keep-Alive], Transfer-Encoding=[chunked]}