Liberica 運行時容器上的 Spring Boot 應用程式
一、簡介
在本教程中,我們將探索運行使用 Spring Boot 作為 Docker 容器建立的標準 Java 應用程式的方法。更具體地說,我們將在 Alpaquita Linux 之上使用 Liberica JDK 來建立將運行我們的應用程式的 Docker 映像。
Liberica JDK 和 Alpaquita Linux 是 BellSoft 產品的一部分。 BellSoft是一家致力於讓 Java 成為雲端原生應用程式首選語言的組織。透過他們有針對性的產品,他們承諾以更低的成本提供更好的體驗。
2. 一個簡單的 Spring Boot 應用程式
讓我們先用 Java 創建一個簡單的應用程序,我們將繼續對其進行容器化。我們將使用Spring Boot來創建此應用程式。 Spring Boot 可以輕鬆地以最少的配置建立獨立的、生產級的基於 Spring 的應用程式。
初始化 Spring Boot 應用程式最簡單的方法是使用Spring Boot CLI 。它允許我們直接從我們最喜歡的命令列使用 start.spring.io 建立一個新專案:
$ spring init --build=gradle --dependencies=web spring-bellsoft
在這裡,我們新增web
作為依賴項,允許我們使用 RESTful API 和 Apache Tomcat 作為預設嵌入式容器來建立應用程式。我們在這裡選擇 Gradle 作為建置工具。但是,它選擇 Java 作為語言,並預設選擇許多其他內容。
然後,我們可以將生成的專案匯入到我們最喜歡的 IDE(例如 IntelliJ Idea)中,並開始開發應用程式。如前所述,我們將保持這一點非常簡單。因此,我們將添加一個簡單的 REST API,它將數字作為輸入並傳回等於或小於該數字的斐波那契數列:
@RestController
public class DemoController {
@GetMapping("/api/v1/fibs")
public List<Integer> getFibonacciSeriesBelowGivenInteger(Integer input) {
List<Integer> result;
if (input == 0)
result = List.of(0);
else {
int n = 0; int m = 1;
result = new ArrayList<>(Arrays.asList(n));
while (m <= input) {
result.add(m);
m = n + m; n = m - n;
}
}
return result;
}
}
在 Gradle 中建立應用程式就像執行以下命令一樣簡單,該命令使用先前產生的 Gradle 包裝器:
./gradlew clean build
產生的專案的預設打包是 JAR ,這意味著上述命令成功後將在輸出目錄「 ./build/libs
」中建立最終的可執行 JAR。我們可以使用這個 JAR 檔案啟動應用程式:
java -jar ./build/libs/spring-bellsoft-0.0.1-SNAPSHOT.jar
然後,我們可以調用 API 並查看它是否正常工作:
$ curl http://localhost:8080/api/v1/fibs?input=5
[0,1,1,2,3,5]
我們為本教程的其餘部分創建一個簡單的應用程式的工作到此結束。我們將使用這個可部署的應用程式來容器化該應用程式。
3. 容器化我們的應用程式
容器是打包程式碼及其所有依賴項的標準軟體單元。它是作業系統虛擬化的一種形式,提供一致的應用程式部署方式。如今,它已成為在雲端環境中運行任何應用程式的預設選擇。
我們需要一個容器平台來將我們的簡單應用程式作為容器運行。容器平台除其他外還提供容器引擎來建立和管理容器。 Docker是最受歡迎的平台,旨在建置、共用和運行容器應用程式。
容器引擎從容器鏡像建立容器。容器映像是一個不可變的靜態文件,其中包含容器運行所需的所有內容。但是,它共享主機的作業系統核心。因此,它提供了完全的隔離,但仍然是輕量級的:
建立 Docker 映像的方法之一是將建立映像的方法描述為 Dockerfile 。然後,我們可以使用 Docker 守護程式從 Dockerfile 建立映像。 Docker 的原始映像格式現已成為開放容器計劃 (OCI)映像規格。
將應用程式作為容器運行的主要優勢之一是它可以跨多個環境提供一致的部署體驗。例如,假設我們交付使用 Java 17 建置的簡單應用程序,但部署在具有 Java 11 運行時的環境中。
為了避免這種意外,容器映像允許我們打包應用程式的所有關鍵依賴項,例如作業系統二進位檔案/庫和 Java 運行時。透過這樣做,我們可以確保我們的應用程式無論部署在哪個環境中都會表現相同。
4.Liberica運行時容器
容器鏡像由多層堆疊而成。每一層代表對檔案系統的特定修改。通常,我們會從最符合應用程式要求的基礎映像開始,並在其之上建立附加層。
BellSoft 提供了多種在雲端為基礎的環境中執行 Java 應用程式進行高度最佳化的映像。它們是使用 Alpaquita Linux 和 Liberica JDK 建構的。在使用這些影像之前,讓我們先檢查一下其 BellSoft 組件的優點。
4.1. Alpaquita Linux 的優點
Alpaquita Linux是一個基於Alpine Linux的輕量級作業系統。它專為 Java 量身定制,並針對雲端原生應用程式的部署進行了最佳化。其結果是基礎映像大小為 3.22 MB,並且需要少量資源才能運作。
Alpaquita Linux 有兩個版本,一個基於最佳化的musl libc ,另一個基於glib libc 。這裡,libc 指的是 ISO C 標準中指定的C 程式語言的標準函式庫。它為多個任務提供巨集、類型定義和函數。
除了針對 Java 應用程式進行最佳化之外,Alpaquita Linux還為我們的部署提供了多種安全功能。其中包括網路功能、自訂建置選項和進程隔離。它還包括內核強化,例如內核鎖定和內核模組簽名。
此外,Alpaquita Linux 針對部署進行了最佳化,因為它使用核心模組壓縮來減少套件的大小。它提供了可靠且快速的堆疊來運行具有核心優化和記憶體管理等效能功能的應用程式。
Alpaquita Linux僅封裝了少量作業系統元件。但是,我們可以從Alpaquita APK 儲存庫安裝額外的模組和附加套件。最重要的是,Alpaquita Linux 的 LTS 版本有四年的支援生命週期。
4.2. Liberica JDK 的優點
Liberica JDK是用於現代 Java 部署的開源 Java 執行階段。它來自 OpenJDK 的主要貢獻者 BellSoft,並承諾為 Java 應用程式的雲端、伺服器和桌面使用提供單一執行時間。也建議將其用於基於 Spring 的 Java 應用程式。
Liberica JDK 支援各種架構,例如 x86 64/32 位元、ARM、PowerPC 和 SPARC。它還支援多種作業系統,例如 Windows、macOS 和大多數 Linux 發行版。此外,它支援當今使用的幾乎所有 Java 版本。
Liberica JDK 的關鍵優勢之一是它相當輕量。基於 Alpaquita Linux for Java 17 的 Liberica 執行時間容器小於 70 MB。它承諾更好的效能,同時減少流量、儲存、記憶體消耗和總成本。
它還具有用於使用 JDK 運行時的多種工具。可以完全存取用於監控和更新的工具。此外,使用者還可以存取 Liberica Administration Center (LAC),這是一種用於執行時間監控、許可證控制和安全性更新的企業工具。
Liberica JDK 針對 Java SE 規範進行了 TCK 驗證,並在每次發布之前進行了徹底的暴露測試。此外,BellSoft 透過錯誤修復、安全性修補程式和其他所需的改進來保證 Liberica JDK 至少八年的生命週期。
5. 建立容器鏡像
現在,我們準備好使用 Alpaquita Linux 和 Liberica JDK 來容器化我們的簡單應用程式。第一步是選擇具有這些依賴項的基礎映像。我們始終可以建立基礎映像,但值得慶幸的是, BellSoft 在 Docker Hub 上維護了多個映像可供選擇。
5.1.選擇 Liberica 運行時容器
這些是 Alpaquita Linux 映像,具有不同的 Liberica JDK lite 和 Liberica JRE 選項可供選擇。我們通常可以從標籤中識別這一點,該標籤可能包含以下內容之一:
- jdk:搭配 Liberica JDK Lite 版本的 Alpaquita Linux 映像
- jdk-all:打包 Liberica JDK,可用於使用 jlink 工具建立自訂運行時
- jre:僅包含用於執行 Java 應用程式的 Liberica JRE
在這裡,除了圖像包含的 JDK 版本之外,圖像的標籤還顯示了有關圖像的許多其他資訊。讓我們來看看 BellSoft 使用的圖像標籤的約定:
[JDK type]-Java version-[slim]-[OS release]-[libc type]-[date]
在這裡,標籤的不同部分講述了圖像的特定方面,並幫助我們從許多可用圖像中立即選擇:
- JDK type:JDK 的類型(JDK、jdk-all 和 jre,如我們之前所見)
- Java版本:JDK符合的Java版本
- slim:指示影像是否是苗條的
- OS version:作業系統的版本(目前只有
stream
) - libc 類型:標準 C 函式庫的類型(glibc 或 musl,如我們之前所見)
- 日期:圖片的發布日期
5.2.容器化我們的應用程式
現在,我們已經有了圖像標籤中的所有資訊來選擇正確的標籤。例如,如果我們希望應用程式使用帶有 glibc 的 Java 17,我們必須選擇標籤「 jdk-17-glibc
」。
一旦我們選擇了正確的基礎映像標籤,下一步就是建立一個 Dockerfile 來定義我們如何為應用程式建立容器映像:
FROM bellsoft/liberica-runtime-container:jdk-17-glibc
VOLUME /tmp
ARG JAR_FILE=build/libs/java-bellsoft-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
這個相當簡單的 Dockerfile 聲明我們希望從提到的 Liberica 運行時容器開始並複製我們的應用程式 fat JAR 。我們還定義了入口點,其中包含在容器實例化後運行應用程式的指令。
我們應該將此 Dockerfile 放置在應用程式程式碼庫目錄的根目錄中。然後,我們可以使用以下命令在本機儲存庫中建立容器鏡像:
docker buildx build -t spring-bellsoft .
預設情況下,這將從註冊表 Docker Hub 中提取基礎映像,並為我們的應用程式建立容器映像。然後,我們可以將此映像作為容器運行:
docker run --name fibonacci -d -p 8080:8080 spring-bellsoft
請注意,我們已將本地端口 8080 與容器端口 8080 進行映射。因此,我們可以像本教程前面所做的那樣訪問我們的應用程式:
$ curl http://localhost:8080/api/v1/fibs?input=5
[0,1,1,2,3,5]
我們使用 BellSoft 發布的 Liberica 運行時容器對本教程前面創建的簡單應用程式進行容器化的工作到此結束。當然,嘗試更複雜的應用程式和我們可用的 Liberica 運行時容器的其他變體會很有趣。
六,結論
在本教程中,我們了解了為基於 Spring Boot 的簡單 Java 應用程式建立容器的基礎知識。我們探索了選擇BellSoft Liberica運行時容器來執行此操作的選項。在此過程中,我們創建了一個簡單的應用程式並將其容器化。
這有助於我們了解 Alpaquita Linux 和 Liberica JDK(Liberica 運行時容器的組成部分)的優勢。這些是 BellSoft 的一些核心產品,該公司致力於優化基於雲端的環境的 Java 應用程式。