Spring Boot 快速遷移至 Quarkus


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

>>> 源碼 https://gitee.com/log4j/pig,歡迎署名轉載 <<<


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM