Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解_第1頁
Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解_第2頁
Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解_第3頁
Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解_第4頁
Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解目錄設(shè)置上下文示例應(yīng)用程序常見變化遷移web層遷移數(shù)據(jù)訪問層遷移執(zhí)行器

設(shè)置上下文

JVM是一項(xiàng)偉大的技術(shù)。現(xiàn)代版本將運(yùn)行的字節(jié)碼編譯為本機(jī)代碼,具體取決于現(xiàn)有的工作負(fù)載。出于這個(gè)原因,JVM應(yīng)用程序在運(yùn)行時(shí)性能方面與本機(jī)可執(zhí)行文件不相上下,甚至勝過本機(jī)可執(zhí)行文件。

JVM應(yīng)用程序有一個(gè)預(yù)熱時(shí)間,在此期間它們性能不佳。在運(yùn)行時(shí)加載類沒有幫助。Spring和JakartaEE等框架一直在使用類路徑掃描和反射,這使得啟動(dòng)時(shí)間更長。對于長時(shí)間運(yùn)行的進(jìn)程,例如傳統(tǒng)的應(yīng)用程序服務(wù)器,這不是問題。

在容器的上下文中,它是。因?yàn)槿藗儼讶萜鳟?dāng)作牛而不是寵物來處理,所以Kubernetes等平臺會(huì)定期殺死pod并安排新的pod。啟動(dòng)時(shí)間越長,JVM的相關(guān)性就越低。在需要快速自動(dòng)縮放pod數(shù)量的無服務(wù)器環(huán)境中,情況會(huì)變得更糟。

為了趕上潮流,Oracle提供了SubstrateVM。GraalVM的子組件SubstrateVM允許將JVM字節(jié)碼轉(zhuǎn)換為本機(jī)可執(zhí)行文件。為此,SubstrateVM編譯字節(jié)碼AOT。出于這個(gè)原因,您需要在運(yùn)行時(shí)顯式地向它提供JVM上可用的信息。例如反射的情況。請注意,某些JVM功能未移植到GraalVM。此外,AOT編譯是一個(gè)耗時(shí)的過程。

結(jié)果是,一方面,我們擁有JVM及其框架所利用的所有功能;另一方面,我們有本機(jī)可執(zhí)行文件,需要精細(xì)的手動(dòng)配置和大量的構(gòu)建時(shí)間。

新一代的框架已經(jīng)產(chǎn)生,旨在找到一個(gè)中間地帶,即Micronaut和Quarkus。它們都旨在生成字節(jié)碼AOT。請注意,此AOT與上面提到的不同。兩個(gè)框架都沒有在運(yùn)行時(shí)使用昂貴的反射,而是在構(gòu)建時(shí)生成額外的類。這也使我們能夠避免在啟動(dòng)時(shí)進(jìn)行類路徑掃描。簡而言之,這個(gè)想法是關(guān)于在構(gòu)建時(shí)提供盡可能多的代碼。

示例應(yīng)用程序

我希望遷移的示例應(yīng)用程序足夠簡單,這樣我就可以自己遷移它,但又不至于變得微不足道。它由以下部分組成:

SpringMVC實(shí)現(xiàn)的控制器層由SpringDataJPA實(shí)現(xiàn)的存儲庫層一個(gè)JPA實(shí)體通過SpringBoot在啟動(dòng)時(shí)生成模式和數(shù)據(jù)插入SpringBoot執(zhí)行器,啟用了health和beans端點(diǎn),無需身份驗(yàn)證即可訪問

該應(yīng)用程序是用Kotlin編寫的。我將使用H2作為數(shù)據(jù)庫來簡化整個(gè)設(shè)置。

常見變化

第一個(gè)變化是替換父POM。

parent

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-parent/artifactId

version2.3.5.RELEASE/version

relativePath/!--lookupparentfromrepository--

/parent

parent

groupIdio.micronaut/groupId

artifactIdmicronaut-parent/artifactId

version2.1.3/version

/parent

因?yàn)镸icronaut在構(gòu)建時(shí)生成字節(jié)碼,所以我們需要在編譯期間添加注釋處理器。因此,最后的第二步是在POM中配置它。

plugin

groupIdorg.jetbrains.kotlin/groupId

artifactIdkotlin-maven-plugin/artifactId

version${kotlin.version}/version

executions

execution

idkapt/id

goals

goalkapt/goal

/goals

configuration

annotationProcessorPaths

annotationProcessorPath

groupIdio.micronaut/groupId

artifactIdmicronaut-inject-java/artifactId!--1--

version${micronaut.version}/version

/annotationProcessorPath

annotationProcessorPath

groupIdio.micronaut.data/groupId

artifactIdmicronaut-data-processor/artifactId!--2--

version${micronaut.data.version}/version

/annotationProcessorPath

/annotationProcessorPaths

/configuration

/execution

/executions

/plugin

處理依賴注入處理持久化相關(guān)的類

您可以通過查看target/classes文件夾來檢查那些額外的類。例如,示例應(yīng)用程序顯示以下內(nèi)容:

$Person$Introspection$$0.classPersonRepository$Intercepted$$proxy0.class

$Person$Introspection$$1.classPersonRepository$Intercepted$$proxy1.class

$Person$Introspection$$2.classPersonRepository$Intercepted$$proxy10.clas

$Person$Introspection$$3.classPersonRepository$Intercepted$$proxy2.class

$Person$Introspection.classPersonRepository$Intercepted$$proxy3.class

$Person$IntrospectionRef.classPersonRepository$Intercepted$$proxy4.class

$PersonControllerDefinition$$exec1.classPersonRepository$Intercepted$$proxy5.class

$PersonControllerDefinition$$exec2.classPersonRepository$Intercepted$$proxy6.class

$PersonControllerDefinition.classPersonRepository$Intercepted$$proxy7.class

$PersonControllerDefinitionClass.classPersonRepository$Intercepted$$proxy8.class

$PersonRepository$InterceptedDefinition.classPersonRepository$Intercepted$$proxy9.class

$PersonRepository$InterceptedDefinitionClass.classPersonRepository$Intercepted.class

Person.classPersonRepository.class

PersonController.classSpringToMicronautApplicationKt.class

Micronaut創(chuàng)建包含Introspection并Intercepted通過kapt.

為了啟動(dòng)應(yīng)用程序,SpringBoot引用了一個(gè)類。

@SpringBootApplication

classSpringToMicronautApplication

funmain(args:ArrayString){

runApplicationSpringToMicronautApplication(*args)

}

Micronaut允許我們只使用標(biāo)準(zhǔn)main函數(shù)。

funmain(args:ArrayString){

build()

.args(*args)

.packages("ch.frankel.springtomicronaut")

.start()

}

SpringBoot插件可以main自動(dòng)找到函數(shù)。在Micronaut中,當(dāng)前版本要求您在POM中顯式設(shè)置它:

properties

exec.mainClassch.frankel.s2m.SpringToMicronautApplicationKt/exec.mainClass

/properties

遷移web層

遷移到web層需要:

用相關(guān)的Micronaut依賴項(xiàng)替換SpringBoot啟動(dòng)器用Micronaut的注釋替換SpringBoot的注釋

為了使應(yīng)用程序成為Web應(yīng)用程序,Micronaut要求添加嵌入式服務(wù)器依賴項(xiàng)。Tomcat、Jetty和Undertow可用。由于SpringBoot默認(rèn)是Tomcat,所以我們使用Tomcat:

dependency

groupIdio.micronaut.servlet/groupId

artifactIdmicronaut-http-server-tomcat/artifactId

scoperuntime/scope

/dependency

Spring和Micronaut的注解幾乎是一一對應(yīng)的。使用Micronaut只是使用一個(gè)包的注釋而不是另一個(gè)包的注釋的問題。Controller不同之處在于Spring提供了使用專門的注解將序列化為JSON的能力,@RestController.Micronaut不需要也不需要在Controller注解上設(shè)置屬性。

春天微航o.s.w.b.a.RestControlleri.m.h.a.Controller(produces=[TEXT_JSON])o.s.w.b.a.GetMappingi.m.h.a.Geto.s.w.b.a.PathVariablei.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的依賴項(xiàng)而不是SpringBoot的將Micronaut的SpringBoot替換Repository為Micronaut的使用Micronaut創(chuàng)建模式并加載初始數(shù)據(jù)

要?jiǎng)?chuàng)建數(shù)據(jù)源和連接池,SpringBoot需要一個(gè)SpringDatastarter和相關(guān)的驅(qū)動(dòng)程序。Micronaut需要三個(gè)不同的部分:

數(shù)據(jù)訪問依賴驅(qū)動(dòng)程序依賴連接池依賴

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-data-jpa/artifactId

/dependency

dependency

groupIdio.micronaut.data/groupId

artifactIdmicronaut-data-hibernate-jpa/artifactId

version${micronaut.data.version}/version

/dependency

dependency

groupIdio.micronaut.sql/groupId

artifactIdmicronaut-jdbc-hikari/artifactId

/dependency

請注意,如果您忘記了連接池,您將在運(yùn)行時(shí)遇到此錯(cuò)誤:

NobackingRepositoryOperationsconfiguredforrepository.Checkyourconfigurationandtryagain

SpringDataJPA在運(yùn)行時(shí)生成存儲庫的實(shí)現(xiàn)。MicronautData在構(gòu)建時(shí)生成它們。對于開發(fā)人員來說,主要區(qū)別在于存儲庫接口必須使用Micronaut的@Repository.

@Repository

interfacePersonRepository:CrudRepositoryPerson,Long

需要配置Micronaut以掃描存儲庫和實(shí)體:

jpa.default:

packages-to-scan:

-'ch.frankel.springtomicronaut'

要?jiǎng)?chuàng)建模式,您可以通過兩種不同的方式配置SpringBoot:依賴Hibernate的模式創(chuàng)建或create.sql在類路徑的根目錄中提供一個(gè)文件。同樣,要插入初始數(shù)據(jù),您可以添加一個(gè)data.sql.

Micronaut不提供開箱即用的機(jī)制來插入數(shù)據(jù)。但它提供了與Flyway的集成。放置Flyway的遷移的默認(rèn)位置是db/migration,就像SpringBoot一樣。

dependency

groupIdio.micronaut.flyway/groupId

artifactIdmicronaut-flyway/artifactId

version2.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ū)動(dòng)程序依賴性保持不變。雖然SpringBoot使用默認(rèn)參數(shù)創(chuàng)建連接,但Micronaut需要顯式配置它:

datasources.default:

url:jdbc:h2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論