5分钟拥抱云原生 | SpringBoot 迁移至 Quarkus
Quarkus 是一个目前非常火的 Java 应用开发框架,定位是轻量级的微服务框架。,Quarkus 提供了优秀的容器化整合能力,相较于传统开发框架(Spring Boot)有着更快的启动速度、更小的内存消耗、更短的服务响应。
本文将演示将 SpringBoot 应用迁移至 Quarkus
Spring Boot 示例程序
使用 JPA 完成 数据库的增删改查操作,基础代码如下
maven 依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
jpa crud
public interface DemoUserDao extends CrudRepository {
}
迁移至 Quarkus
quarkus-bom 管理了全部 quarkus 插件 maven 版本信息,引入后所有依赖不需要再定义版本。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkusgroupId>
<artifactId>quarkus-bomartifactId>
<version>1.10.5.Finalversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
迁移 spring-web 、spring-jpa 至 quarkus 技术栈。
<dependency>
<groupId>io.quarkusgroupId>
<artifactId>quarkus-spring-data-jpaartifactId>
dependency>
<dependency>
<groupId>io.quarkusgroupId>
<artifactId>quarkus-spring-webartifactId>
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 监控迁移
添加以下依赖
io.quarkus
quarkus-smallrye-health
指定访问监控断点路径
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 插件
io.quarkus
quarkus-flyway
指定插件启动策略即可
quarkus.flyway.migrate-at-start=true
往期推荐
评论