欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解

 更新時(shí)間:2023年05月11日 14:54:19   作者:飽飽巴士  
這篇文章主要為大家?介紹了Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

設(shè)置上下文

JVM是一項(xiàng)偉大的技術(shù)?,F(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 和 Jakarta EE 等框架一直在使用類路徑掃描和反射,這使得啟動(dòng)時(shí)間更長(zhǎng)。對(duì)于長(zhǎng)時(shí)間運(yùn)行的進(jìn)程,例如傳統(tǒng)的應(yīng)用程序服務(wù)器,這不是問題。

在容器的上下文中,它是。因?yàn)槿藗儼讶萜鳟?dāng)作牛而不是寵物來處理,所以 Kubernetes等平臺(tái)會(huì)定期殺死 pod 并安排新的 pod。啟動(dòng)時(shí)間越長(zhǎng),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 上可用的信息。例如反射的情況。請(qǐng)注意,某些 JVM 功能未移植到 GraalVM。此外,AOT編譯是一個(gè)耗時(shí)的過程。

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

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

示例應(yīng)用程序

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

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

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

常見變化

第一個(gè)變化是替換父 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>

因?yàn)?Micronaut 在構(gòu)建時(shí)生成字節(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>
  • 處理依賴注入
  • 處理持久化相關(guān)的類

您可以通過查看target/classes文件夾來檢查那些額外的類。例如,示例應(yīng)用程序顯示以下內(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)建包含IntrospectionIntercepted通過kapt.

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

@SpringBootApplication
class SpringToMicronautApplication
fun main(args: Array<String>) {
  runApplication<SpringToMicronautApplication>(*args)
}

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

fun main(args: Array<String>) {
  build()
    .args(*args)
    .packages("ch.frankel.springtomicronaut")
    .start()
}

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

<properties>
  ...
  <exec.mainClass>ch.frankel.s2m.SpringToMicronautApplicationKt</exec.mainClass>
</properties>

遷移 web 層

遷移到 web 層需要:

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

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

<dependency>
  <groupId>io.micronaut.servlet</groupId>
  <artifactId>micronaut-http-server-tomcat</artifactId>
  <scope>runtime</scope>
</dependency>

Spring 和 Micronaut 的注解幾乎是一一對(duì)應(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.Get
o.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)而不是 Spring Boot 的
  • 將 Micronaut 的 Spring Boot 替換Repository為 Micronaut 的
  • 使用 Micronaut 創(chuàng)建模式并加載初始數(shù)據(jù)

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

  • 數(shù)據(jù)訪問依賴
  • 驅(qū)動(dòng)程序依賴
  • 連接池依賴
<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>

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

No backing RepositoryOperations configured for repository. Check your configuration and try again

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

@Repository
interface PersonRepository : CrudRepository<Person, Long>

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

jpa.default:
  packages-to-scan:
    - 'ch.frankel.springtomicronaut'

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

Micronaut 不提供開箱即用的機(jī)制來插入數(shù)據(jù)。但它提供了與 Flyway 的集成。放置 Flyway 的遷移的默認(rèn)位置是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ū)動(dòng)程序依賴性保持不變。雖然 Spring Boot 使用默認(rèn)參數(shù)創(chuàng)建連接,但 Micronaut 需要顯式配置它:

datasources.default:
  url: jdbc:h2:mem:test
  driverClassName: org.h2.Driver
  username: sa
  dialect: H2

遷移執(zhí)行器

Micronaut 還提供管理端點(diǎn)。它與 Spring Boot 的基本相同。

需要替換依賴項(xiàng):

<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ā)人員需要單獨(dú)配置端點(diǎn):

endpoints:
  all.path: /actuator        # 1
  beans:
    enabled: true
    sensitive: false
  health:
    enabled: true
    sensitive: false
  flyway:
    enabled: true
    sensitive: false

這樣 一個(gè)Springboot項(xiàng)目遷移到Micronaut項(xiàng)目就完成啦!

以上就是Springboot遷移到Micronaut實(shí)現(xiàn)過程詳解的詳細(xì)內(nèi)容,更多關(guān)于Springboot遷移Micronaut的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表詳解

    Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表詳解

    鏈表是一種物理存儲(chǔ)單元上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),java代碼實(shí)現(xiàn)單鏈表,插入,刪除和遍歷等功能,這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Java KindEditor粘貼圖片自動(dòng)上傳到服務(wù)器功能實(shí)現(xiàn)

    Java KindEditor粘貼圖片自動(dòng)上傳到服務(wù)器功能實(shí)現(xiàn)

    這篇文章主要介紹了Java KindEditor粘貼圖片自動(dòng)上傳到服務(wù)器功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • 詳解SpringBoot修改啟動(dòng)端口server.port的四種方式

    詳解SpringBoot修改啟動(dòng)端口server.port的四種方式

    這篇文章主要介紹了詳解SpringBoot修改啟動(dòng)端口server.port的四種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java制作android 日歷代碼分享

    java制作android 日歷代碼分享

    本文給大家分享的是一段使用java制作Android日歷的代碼,非常簡(jiǎn)單實(shí)用,實(shí)現(xiàn)了讀取日歷事件、插入事件、編輯日歷事件、查看日歷等功能,有需要的小伙伴參考下
    2015-03-03
  • Java實(shí)現(xiàn)數(shù)據(jù)庫連接池的方法

    Java實(shí)現(xiàn)數(shù)據(jù)庫連接池的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)數(shù)據(jù)庫連接池的方法,涉及java數(shù)據(jù)庫連接池的創(chuàng)建、連接、刷新、關(guān)閉及狀態(tài)獲取的常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • servlet3文件上傳操作

    servlet3文件上傳操作

    這篇文章主要介紹了servlet3文件上傳操作的相關(guān)資料,需要的朋友可以參考下
    2017-11-11
  • Springboot網(wǎng)站第三方登錄 微信登錄

    Springboot網(wǎng)站第三方登錄 微信登錄

    這篇文章主要為大家詳細(xì)介紹了Springboot網(wǎng)站第三方登錄 ,微信登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析

    SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析

    這篇文章主要為大家介紹了SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2023-09-09
  • Java?JSON處理庫之Gson的用法詳解

    Java?JSON處理庫之Gson的用法詳解

    Gson是Google開發(fā)的一款Java?JSON處理庫,旨在簡(jiǎn)化Java開發(fā)人員操作JSON數(shù)據(jù)的過程,本文就來和大家簡(jiǎn)單聊聊Gson的原理與具體使用吧
    2023-05-05
  • java 語句塊的使用詳解及實(shí)例

    java 語句塊的使用詳解及實(shí)例

    這篇文章主要介紹了java 用語句塊的正確方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評(píng)論