温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何将SpringBoot项目迁移到Quarkus中

发布时间:2021-01-22 14:41:12 来源:亿速云 阅读:234 作者:Leah 栏目:开发技术

如何将SpringBoot项目迁移到Quarkus中?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

使用 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

关于如何将SpringBoot项目迁移到Quarkus中问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI