热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springboot每隔一段时间后第一次请求耗时特别长(内含详情)

2017-08-25更新发现,在访问任何一个接口后,接着的接口在短时间内访问速度都很正常。且该情况,无论是否通过nginx代理都是这样。始终是隔一段时


2017-08-25 更新
发现,在访问任何一个接口后,接着的接口在短时间内访问速度都很正常。
且该情况,无论是否通过nginx 代理都是这样。
始终是隔一段时间后的第一个接口访问速度很慢。


背景
1、 应用基于 Spring Boot
2、 调试部分已独立出来为单一的 Get 请求接口。
3、 该接口仅仅返回一个普通json,包含服务器时间、应用名称、请求状态等信息,无任何I/O、数据库等复杂操作。
4、 项目启动方式为 打包后通过 java -jar xxx.war 启动,上层由 nginx 托管。
5、 Spring Boot 启动端口为8000, nginx 端口为9999

问题
无论用浏览器、Postman、curl、Python(requests) 去访问接口,均得到第一次响应非常耗时,短时间内第二次访问会快很多。

分析
1、暂时通过日志判断出控制台输出请求头部分的前后耗时非常长,但是不明白下一步该去怎么分析找问题。
2、单独接口部分的请求响应很快。

请各位帮忙分析下,以下为代码。

接口代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    @RequestMapping(value = "/status")

    @ResponseBody

    public

    Map status(HttpServletRequest request){

        Map m = new HashMap<>;



        m.put("name", "*****");

        m.put("remote_addr", request.getHeader("X-Real-Addr"));



        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar calendar = Calendar.getInstance();

        m.put("time", sdf.format(calendar.getTime()));





        return m;

    }

日志设置代码

1
2
3
4
5
#logging(TRACE DEBUG INFO WARN)

logging.level.org.apache=debug

logging.level.org.springframework=INFO

logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE

logging.level.org.springframework.jdbc.core=DEBUG

日志代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2017-08-22 16:28:54,430 [http-nio-8000-Acceptor-0] DEBUG o.a.tomcat.util.threads.LimitLatch - Counting up[http-nio-8000-Acceptor-0] latch=1

2017-08-22 16:28:54,430 [http-nio-8000-exec-2] DEBUG o.a.coyote.http11.Http11NioProtocol - Processing socket [org.apache.tomcat.util.net.NioChannel@16655a47:java.nio.channels.SocketChannel[connected local=/192.168.0.30:8000 remote=/192.168.0.30:55507]] with status [OPEN_READ]

2017-08-22 16:28:54,431 [http-nio-8000-exec-2] DEBUG o.a.coyote.http11.Http11InputBuffer - Received [GET /rest/db/status HTTP/1.0

X-Real-Addr: 192.168.1.106

X-Real-Port: 54620

HostAddr: http://192.168.1.30

HostPort: 9999

project: /rest

ServerAddr: http://192.168.1.30:9999

Host: 192.168.0.30:8000

Connection: close

Pragma: no-cache

Cache-Control: no-cache

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

Accept-Encoding: gzip, deflate

Accept-Language: zh-CN,zh;q=0.8



]

2017-08-22 16:28:55,843 [http-nio-8000-exec-2] DEBUG o.a.c.a.AuthenticatorBase - Security checking request GET /rest/db/status

2017-08-22 16:28:55,843 [http-nio-8000-exec-2] DEBUG org.apache.catalina.realm.RealmBase -   No applicable constraints defined

2017-08-22 16:28:55,843 [http-nio-8000-exec-2] DEBUG o.a.c.a.AuthenticatorBase -  Not subject to any constraint

2017-08-22 16:28:55,843 [http-nio-8000-exec-2] DEBUG o.apache.tomcat.util.http.Parameters - Set encoding to UTF-8

2017-08-22 16:28:55,844 [http-nio-8000-exec-2] INFO  org.kys.log.spring.LogInterceptor - start [/rest/db/status]

2017-08-22 16:28:55,847 [http-nio-8000-exec-2] INFO  org.kys.log.spring.LogInterceptor - complete [/rest/db/status] :: Time Taken=3

2017-08-22 16:28:55,847 [http-nio-8000-exec-2] DEBUG o.a.coyote.http11.Http11Processor - Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@75160339:org.apache.tomcat.util.net.NioChannel@16655a47:java.nio.channels.SocketChannel[connected local=/192.168.0.30:8000 remote=/192.168.0.30:55507]], Status in: [OPEN_READ], State out: [CLOSED]

2017-08-22 16:28:55,848 [http-nio-8000-exec-2] DEBUG o.a.tomcat.util.threads.LimitLatch - Counting down[http-nio-8000-exec-2] latch=1

2017-08-22 16:29:03,493 [http-nio-8000-Acceptor-0] DEBUG o.a.tomcat.util.threads.LimitLatch - Counting up[http-nio-8000-Acceptor-0] latch=1

2017-08-22 16:29:03,503 [http-nio-8000-exec-3] DEBUG o.a.coyote.http11.Http11NioProtocol - Processing socket [org.apache.tomcat.util.net.NioChannel@16655a47:java.nio.channels.SocketChannel[connected local=/192.168.0.30:8000 remote=/192.168.0.30:55520]] with status [OPEN_READ]

2017-08-22 16:29:03,504 [http-nio-8000-exec-3] DEBUG o.a.coyote.http11.Http11InputBuffer - Received [GET /rest/db/status HTTP/1.0

X-Real-Addr: 192.168.1.106

X-Real-Port: 54620

HostAddr: http://192.168.1.30

HostPort: 9999

project: /rest

ServerAddr: http://192.168.1.30:9999

Host: 192.168.0.30:8000

Connection: close

Pragma: no-cache

Cache-Control: no-cache

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

Accept-Encoding: gzip, deflate

Accept-Language: zh-CN,zh;q=0.8



]

2017-08-22 16:29:03,507 [http-nio-8000-exec-3] DEBUG o.a.c.a.AuthenticatorBase - Security checking request GET /rest/db/status

2017-08-22 16:29:03,508 [http-nio-8000-exec-3] DEBUG org.apache.catalina.realm.RealmBase -   No applicable constraints defined

2017-08-22 16:29:03,508 [http-nio-8000-exec-3] DEBUG o.a.c.a.AuthenticatorBase -  Not subject to any constraint

2017-08-22 16:29:03,508 [http-nio-8000-exec-3] DEBUG o.apache.tomcat.util.http.Parameters - Set encoding to UTF-8



推荐阅读
author-avatar
mobiledu2502857923
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有