Springboot遷移到Micronaut實現(xiàn)過程詳解
設置上下文
JVM是一項偉大的技術?,F(xiàn)代版本將運行的字節(jié)碼編譯為本機代碼,具體取決于現(xiàn)有的工作負載。出于這個原因,JVM 應用程序在運行時性能方面與本機可執(zhí)行文件不相上下,甚至勝過本機可執(zhí)行文件。
JVM 應用程序有一個預熱時間,在此期間它們性能不佳。在運行時加載類沒有幫助。Spring 和 Jakarta EE 等框架一直在使用類路徑掃描和反射,這使得啟動時間更長。對于長時間運行的進程,例如傳統(tǒng)的應用程序服務器,這不是問題。
在容器的上下文中,它是。因為人們把容器當作牛而不是寵物來處理,所以 Kubernetes等平臺會定期殺死 pod 并安排新的 pod。啟動時間越長,JVM 的相關性就越低。在需要快速自動縮放 pod 數(shù)量的無服務器環(huán)境中,情況會變得更糟。
為了趕上潮流,Oracle 提供了SubstrateVM。GraalVM 的子組件 SubstrateVM 允許將 JVM 字節(jié)碼轉換為本機可執(zhí)行文件。為此,SubstrateVM 編譯字節(jié)碼AOT。出于這個原因,您需要在運行時顯式地向它提供 JVM 上可用的信息。例如反射的情況。請注意,某些 JVM 功能未移植到 GraalVM。此外,AOT編譯是一個耗時的過程。
結果是,一方面,我們擁有 JVM 及其框架所利用的所有功能;另一方面,我們有本機可執(zhí)行文件,需要精細的手動配置和大量的構建時間。
新一代的框架已經(jīng)產(chǎn)生,旨在找到一個中間地帶 ,即Micronaut 和 Quarkus。它們都旨在生成字節(jié)碼 AOT。請注意,此 AOT 與上面提到的不同。兩個框架都沒有在運行時使用昂貴的反射,而是在構建時生成額外的類。這也使我們能夠避免在啟動時進行類路徑掃描。簡而言之,這個想法是關于在構建時提供盡可能多的代碼。
示例應用程序
我希望遷移的示例應用程序足夠簡單,這樣我就可以自己遷移它,但又不至于變得微不足道。它由以下部分組成:
- Spring MVC實現(xiàn)的控制器層
- 由 Spring Data JPA 實現(xiàn)的存儲庫層
- 一個 JPA 實體
- 通過 Spring Boot 在啟動時生成模式和數(shù)據(jù)插入
- Spring Boot 執(zhí)行器,啟用了
health和beans端點,無需身份驗證即可訪問
該應用程序是用 Kotlin 編寫的。我將使用 H2 作為數(shù)據(jù)庫來簡化整個設置。
常見變化
第一個變化是替換父 POM。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>2.1.3</version>
</parent>因為 Micronaut 在構建時生成字節(jié)碼,所以我們需要在編譯期間添加注釋處理器。因此,最后的第二步是在 POM 中配置它。
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
...
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId> <!-- 1 -->
<version>${micronaut.version}</version>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId> <!-- 2 -->
<version>${micronaut.data.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
...
</executions>
...
</plugin>- 處理依賴注入
- 處理持久化相關的類
您可以通過查看target/classes文件夾來檢查那些額外的類。例如,示例應用程序顯示以下內(nèi)容:
$Person$Introspection$$0.class PersonRepository$Intercepted$$proxy0.class
$Person$Introspection$$1.class PersonRepository$Intercepted$$proxy1.class
$Person$Introspection$$2.class PersonRepository$Intercepted$$proxy10.clas
$Person$Introspection$$3.class PersonRepository$Intercepted$$proxy2.class
$Person$Introspection.class PersonRepository$Intercepted$$proxy3.class
$Person$IntrospectionRef.class PersonRepository$Intercepted$$proxy4.class
$PersonControllerDefinition$$exec1.class PersonRepository$Intercepted$$proxy5.class
$PersonControllerDefinition$$exec2.class PersonRepository$Intercepted$$proxy6.class
$PersonControllerDefinition.class PersonRepository$Intercepted$$proxy7.class
$PersonControllerDefinitionClass.class PersonRepository$Intercepted$$proxy8.class
$PersonRepository$InterceptedDefinition.class PersonRepository$Intercepted$$proxy9.class
$PersonRepository$InterceptedDefinitionClass.class PersonRepository$Intercepted.class
Person.class PersonRepository.class
PersonController.class SpringToMicronautApplicationKt.class
Micronaut 創(chuàng)建包含Introspection并Intercepted通過kapt.
為了啟動應用程序,Spring Boot 引用了一個類。
@SpringBootApplication
class SpringToMicronautApplication
fun main(args: Array<String>) {
runApplication<SpringToMicronautApplication>(*args)
}Micronaut 允許我們只使用標準main函數(shù)。
fun main(args: Array<String>) {
build()
.args(*args)
.packages("ch.frankel.springtomicronaut")
.start()
}Spring Boot 插件可以main“自動”找到函數(shù)。在 Micronaut 中,當前版本要求您在 POM 中顯式設置它:
<properties> ... <exec.mainClass>ch.frankel.s2m.SpringToMicronautApplicationKt</exec.mainClass> </properties>
遷移 web 層
遷移到 web 層需要:
- 用相關的 Micronaut 依賴項替換 Spring Boot 啟動器
- 用 Micronaut 的注釋替換 Spring Boot 的注釋
為了使應用程序成為 Web 應用程序,Micronaut 要求添加嵌入式服務器依賴項。Tomcat、Jetty 和 Undertow 可用。由于Spring Boot默認是Tomcat,所以我們使用Tomcat:
<dependency> <groupId>io.micronaut.servlet</groupId> <artifactId>micronaut-http-server-tomcat</artifactId> <scope>runtime</scope> </dependency>
Spring 和 Micronaut 的注解幾乎是一一對應的。使用 Micronaut 只是使用一個包的注釋而不是另一個包的注釋的問題。Controller不同之處在于 Spring 提供了使用專門的注解將序列化為 JSON 的能力, @RestController. Micronaut 不需要也不需要在Controller注解上設置屬性。
| 春天 | 微航 |
|---|---|
| o.s.w.b.a.RestController | i.m.h.a.Controller(produces = [TEXT_JSON]) |
| o.s.w.b.a.GetMapping | i.m.h.a.Get |
| o.s.w.b.a.PathVariable | i.m.h.a.PathVariable |
o.s.w.b.a=org.springframework.web.bind.annotation
i.m.h.a=io.micronaut.http.annotation
遷移數(shù)據(jù)訪問層
要遷移到數(shù)據(jù)訪問層,必須:
- 使用 Micronaut 的依賴項而不是 Spring Boot 的
- 將 Micronaut 的 Spring Boot 替換
Repository為 Micronaut 的 - 使用 Micronaut 創(chuàng)建模式并加載初始數(shù)據(jù)
要創(chuàng)建數(shù)據(jù)源和連接池,Spring Boot 需要一個 Spring Data starter 和相關的驅(qū)動程序。Micronaut 需要三個不同的部分:
- 數(shù)據(jù)訪問依賴
- 驅(qū)動程序依賴
- 連接池依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
<version>${micronaut.data.version}</version>
</dependency>
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
</dependency>請注意,如果您忘記了連接池,您將在運行時遇到此錯誤:
No backing RepositoryOperations configured for repository. Check your configuration and try again
Spring Data JPA 在運行時生成存儲庫的實現(xiàn)。Micronaut Data 在構建時生成它們。對于開發(fā)人員來說,主要區(qū)別在于存儲庫接口必須使用 Micronaut 的@Repository.
@Repository interface PersonRepository : CrudRepository<Person, Long>
需要配置 Micronaut 以掃描存儲庫和實體:
jpa.default:
packages-to-scan:
- 'ch.frankel.springtomicronaut'要創(chuàng)建模式,您可以通過兩種不同的方式配置 Spring Boot:依賴 Hibernate 的模式創(chuàng)建或create.sql在類路徑的根目錄中提供一個文件。同樣,要插入初始數(shù)據(jù),您可以添加一個data.sql.
Micronaut 不提供開箱即用的機制來插入數(shù)據(jù)。但它提供了與 Flyway 的集成。放置 Flyway 的遷移的默認位置是db/migration,就像 Spring Boot 一樣。
<dependency> <groupId>io.micronaut.flyway</groupId> <artifactId>micronaut-flyway</artifactId> <version>2.1.1</version> </dependency>
jpa.default:
properties.hibernate:
hbm2ddl.auto: none # 1
show_sql: true # 2
flyway.datasources.default: enabled # 3- 禁用 Hibernate 的模式創(chuàng)建
- 記錄 SQL 語句
- 啟用 Flyway 遷移
H2 驅(qū)動程序依賴性保持不變。雖然 Spring Boot 使用默認參數(shù)創(chuàng)建連接,但 Micronaut 需要顯式配置它:
datasources.default: url: jdbc:h2:mem:test driverClassName: org.h2.Driver username: sa dialect: H2
遷移執(zhí)行器
Micronaut 還提供管理端點。它與 Spring Boot 的基本相同。
需要替換依賴項:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micronaut</groupId> <artifactId>micronaut-management</artifactId> </dependency>
與 Spring Boot 最大的區(qū)別是開發(fā)人員需要單獨配置端點:
endpoints:
all.path: /actuator # 1
beans:
enabled: true
sensitive: false
health:
enabled: true
sensitive: false
flyway:
enabled: true
sensitive: false這樣 一個Springboot項目遷移到Micronaut項目就完成啦!
以上就是Springboot遷移到Micronaut實現(xiàn)過程詳解的詳細內(nèi)容,更多關于Springboot遷移Micronaut的資料請關注腳本之家其它相關文章!
相關文章
Java KindEditor粘貼圖片自動上傳到服務器功能實現(xiàn)
這篇文章主要介紹了Java KindEditor粘貼圖片自動上傳到服務器功能實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
詳解SpringBoot修改啟動端口server.port的四種方式
這篇文章主要介紹了詳解SpringBoot修改啟動端口server.port的四種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Java實現(xiàn)數(shù)據(jù)庫連接池的方法
這篇文章主要介紹了Java實現(xiàn)數(shù)據(jù)庫連接池的方法,涉及java數(shù)據(jù)庫連接池的創(chuàng)建、連接、刷新、關閉及狀態(tài)獲取的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析
這篇文章主要為大家介紹了SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-09-09

