Spring Boot 性能相关的配置
由于有机会在Spring Boot中设置与性能相关的配置,所以我将其记录下来。
由于我还包含了自己的假设,请谅解这一点。
我打算随时改进它。
目前,主要的設定是關於同時執行連接數。
各种版本 (gè
“OpenJDK版本11.0.6”
“org.springframework.boot:spring-boot-starter-web:jar:2.1.7.RELEASE”
“org.springframework.boot:spring-boot-starter-jetty:jar:2.1.7.RELEASE”
“org.springframework.boot:spring-boot-starter-jdbc:jar:2.1.7.RELEASE”
“com.h2database:h2:jar:1.4.199”
Jetty(Java Servlet容器/网页服务器)
Jetty 是 Java Servlet 容器/Web 服务器,也是 Spring Boot 的嵌入式容器。在 Jetty 中,我们主要进行线程数的设置。
设定项目
将其配置在application.properties文件中,则会变成以下方式。
server.jetty.acceptors=2
server.jetty.selectors=3
确认设置是否起作用的方法
这是在Mac上确认的方法。(可能也适用于其他环境,但未经验证。)
启动Spring Boot应用程序。
②启动jconsole
在终端上输入”jconsole”并执行。jconsole是JDK内置的图形化工具,通过使用JMX来可视化MBean的状态。

请确认线程的状态。
当检查线程的状态时,可以看到如下情况,验证了设置已生效。

另外,在这里我们设置了如前所述的参数:server.jetty.acceptors=2、server.jetty.selectors=3。
顺便提一下,有8个以”qtp”开头的线程是因为线程池的最小大小(线程数)的默认值是8。
请提供原文以便进行翻译。
https://www.eclipse.org/jetty/documentation/current/architecture.html -> https://www.eclipse.org/jetty/documentation/current/architecture.html(了解Eclipse Jetty的架构)
https://support.sonatype.com/hc/en-us/articles/360000744687-Understanding-Eclipse-Jetty-9-4-8-Thread-Allocation -> https://support.sonatype.com/hc/en-us/articles/360000744687-Understanding-Eclipse-Jetty-9-4-8-Thread-Allocation(了解Eclipse Jetty 9.4.8的线程分配)
https://www.techscore.com/tech/Java/JavaSE/NIO/5/ -> https://www.techscore.com/tech/Java/JavaSE/NIO/5/(了解Java NIO(非阻塞IO)的基础)
https://www.techscore.com/tech/Java/JavaSE/NIO/5-2/ -> https://www.techscore.com/tech/Java/JavaSE/NIO/5-2/(深入了解Java NIO(非阻塞IO)的进阶)
https://spring.pleiades.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#server-properties -> https://spring.pleiades.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#server-properties(查看Spring Boot的服务器属性设置)
HikariCP(连接池)
HikariCP是一个高速的连接池,在Spring Boot中经常被使用。spring-boot-starter-jdbc依赖于HikariCP,当使用spring-boot-starter-jdbc时,默认会使用HikariCP。
在HikariCP中,可以对连接池中维护的连接数量等进行配置。
设置选项
如果在application.properties文件中进行设置,将会如下所示。
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=10
确认设置是否生效的方法
下面是在Mac上进行确认的方法。(尽管还应该可以在其他环境中进行确认,但未经实际验证。)
将HikariCP对象配置为以MBean形式进行注册。
我会添加以下设定。
spring.datasource.hikari.register-mbeans=true
通过这样做,HikariCP的对象将被注册为MBean,并且可以使用JMX从外部进行监控。
启动Spring Boot应用程序
③打开jconsole
在终端输入jconsole并执行。jconsole是与JDK一起提供的GUI工具,使用JMX可视化MBean的状态。

④检查MBean的状态


请参考
以下是相关的链接:
– https://github.com/brettwooldridge/HikariCP
– https://spring.pleiades.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-properties
– https://matsumana.info/blog/2016/02/06/spring-boot-hikaricp-metrics/
请注意:以上链接是与HikariCP相关的资源。