共计 3550 个字符,预计需要花费 9 分钟才能阅读完成。
Quarkus 是一个目前十分火的 Java 利用开发框架,定位是轻量级的微服务框架。,Quarkus 提供了优良的容器化整合能力,相较于传统开发框架(Spring Boot)有着更快的启动速度、更小的内存耗费、更短的服务响应。
Quarkus 性能比照图
本文将演示将 SpringBoot 迁徙至 Quarkus
Spring Boot 示例程序
应用 JPA 实现 数据库的增删改查操作,根底代码如下
- maven 依赖
`<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码 `
- jpa crud
`public interface DemoUserDao extends CrudRepository<DemoUser, Long> {
}
复制代码 `
迁徙至 Quarkus
- quarkus-bom 治理了全副 quarkus 插件 maven 依赖的版本信息,引入后所有依赖不须要再定义版本。
`<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bom</artifactId>
<version>1.10.5.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
复制代码 `
- 迁徙 spring-web、spring-jpa 至 quarkus 技术栈。
`<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-spring-web</artifactId>
</dependency>
复制代码 `
- 配置文件调整 (还是在 application.yml)
`quarkus.datasource.db-kind=mysql
quarkus.datasource.jdbc.driver=com.mysql.cj.jdbc.Driver
quarkus.datasource.username=root
quarkus.datasource.password=root
quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/pig_demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
复制代码 `
- Main 办法调整为 实现
QuarkusApplication
,且须要通过Quarkus.waitForExit()
放弃服务运行。
`@QuarkusMain
public class SimpleApplication implements QuarkusApplication {
public static void main(String[] args) {
Quarkus.run(SimpleApplication.class,args);
}
@Override
public int run(String… args) {
Quarkus.waitForExit();
return 0;
}
}
复制代码 `
启动运行
main 办法启动, 输入 Quarkus banner
`__ ____ __ _____ ___ __ ____ ______
–/ __ / / / / _ | / _ / //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /
–________/_/ |_/_/|_/_/|_|____/___/
2021-01-12 22:31:46,341 INFO [io.qua.arc.pro.BeanProcessor] (build-21) Found unrecommended usage of private members (use package-private instead) in application beans:
– @Inject field com.example.simple.controller.DemoController#userDao
2021-01-12 22:31:48,702 INFO [io.quarkus] (Quarkus Main Thread) Quarkus 1.10.5.Final on JVM started in 4.613s. Listening on: http://localhost:8080
2021-01-12 22:31:48,703 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2021-01-12 22:31:48,703 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, hibernate-orm, hibernate-orm-panache, mutiny, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation, spring-data-jpa, spring-di, spring-web]
复制代码 `
十分重要的是输入了以后曾经装置的性能
`Installed features: [agroal, cdi, hibernate-orm, hibernate-orm-panache, mutiny, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation, spring-data-jpa, spring-di, spring-web]
复制代码 `
【扩大】actuator 监控迁徙
- 增加以下依赖
`<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
复制代码 `
- 指定拜访监控断点门路
`quarkus.smallrye-health.root-path=/actuator/health
复制代码 `
- 拜访监控查看断点测试
`curl http://localhost:8080/actuator/health
{
“status”: “UP”,
“checks”: [
{
“name”: “Database connections health check”,
“status”: “UP”
}
]
}⏎
复制代码 `
【扩大】Flyway 迁徙
- 增加 quarkus flyway 插件
`<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
</dependency>
复制代码 `
- 指定插件启动策略即可
quarkus.flyway.migrate-at-start=true
参考:《2020 最新 Java 根底精讲视频教程和学习路线!》
链接:https://juejin.cn/post/692003…