Spring Boot 3 中的 Docker Compose 支持
1. 概述
Spring Boot 3 具有新功能,例如將我們的應用程序構建為 GraalVM 本機映像或 Java 17 基線。然而,另一種相關支持是 Docker Compose。
在本教程中,我們將了解如何將 Docker Compose 工作流程與 Spring Boot 3 集成。
2. Spring Boot 3 Docker Compose 支持提供什麼?
通常,我們會基於 docker-compose.yml 運行 docker docker-compose up
來啟動容器,並運行docker-compose.yml
docker-compose down
來停止容器。現在,我們可以將這些 Docker Compose 命令委託給 Spring Boot 3。當 Spring Boot 應用程序啟動或停止時,它還將管理我們的容器。
此外,它還內置了對多種服務的管理,例如 SQL 數據庫、MongoDB、Cassandra 等。因此,我們可能不需要在應用程序資源文件中復製配置類或屬性。
最後,我們將看到我們可以將此支持與自定義 Docker 映像和 Docker Compose 配置文件一起使用。
3. 設置
我們需要 Docker Compose 和 Spring Boot 3 來探索這一新支持。
3.1. Docker 組合
Docker Compose需要已安裝Docker引擎。它們很容易安裝,但根據操作系統的不同可能會有所不同。
Docker 在我們的主機中作為服務運行。通過 Docker 鏡像,我們可以將容器作為系統中的輕量級進程運行。我們可以將一個鏡像視為最小 Linux 內核之上的多個鏡像層。
3.2.春季啟動3
有幾種方法可以設置 Spring Boot 3 項目。例如,我們可以使用 3.1.0 版本中的 Spring初始值設定項。但是,我們始終需要 Spring Boot 3 入門庫來用於項目中包含的依賴項。
首先,我們添加一個父POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<relativePath />
</parent>
我們希望為我們的應用程序使用 REST 端點,因此我們需要Web依賴項;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我們將連接到一個示例數據庫。有多種開箱即用的支持。我們將使用MongoDB :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
為了檢查我們的應用程序是否已啟動並運行,我們將使用 Spring Boot Actuator進行檢查:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
最後,我們將添加Docker Compose依賴項。如果我們想使用其他項目功能但排除 Docker Compose 支持,我們可以將optional
標籤設置為true
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<version>3.1.1</version>
</dependency>
如果我們使用 Gradle,我們可能會考慮使用Spring Boot Gradle 插件來進行類似 BOM 的依賴管理。
4. Spring Boot 3 與 Docker Compose 應用程序啟動
我們將使用 MongoDB 數據庫創建一個 Spring Boot 3 應用程序。一旦我們在啟動時有了spring-boot-docker-compose
依賴項,我們的應用程序就會啟動docker-compose.yml
文件中的所有服務。
4.1. Docker 撰寫文件
首先,我們創建一個docker-compose.yml
文件:
version: '3.8'
services:
db:
image: mongo:latest
ports:
- '27017:27017'
volumes:
- db:/data/db
volumes:
db:
driver:
local
4.2.彈簧簡介
我們需要告訴 Spring Boot 3 Docker Compose 文件的名稱及其路徑。我們可以將其添加到application-{profile}
屬性或 YAML 文件中。我們將使用docker-compose
Spring 配置文件。因此,我們將創建一個application-docker-compose.yml
配置文件:
spring:
docker:
compose:
enabled: true
file: docker-compose.yml
4.3.數據庫配置
我們不需要數據庫配置。 Docker Compose 支持將創建一個默認的。但是,我們仍然可以使用配置文件添加 MongoDB 配置,例如:
@Profile("!docker-compose")
這樣,我們就可以選擇是否使用Docker Compose支持。如果我們不使用配置文件,應用程序將期望數據庫已經在運行。
4.4.模型
然後,我們為通用項目創建一個簡單的Document
類:
@Document("item")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Item {
@Id
private String id;
private String name;
private int quantity;
private String category;
}
4.5.休息控制器
最後,讓我們定義一個具有一些 CRUD 操作的控制器:
@RestController
@RequestMapping("/item")
@RequiredArgsConstructor
public class ItemController {
....
@PostMapping(consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<Item> save(final @RequestBody Item item) {
return ResponseEntity.ok(itemRepository.save(item));
}
// other endpoints
}
5. 應用測試
我們可以通過從我們最喜歡的 IDE 或命令行啟動主 Spring Boot 3 類來啟動應用程序。
5.1.申請開始
讓我們記住提及 Spring 配置文件。例如,從命令行,我們可以使用Spring Boot maven 插件:
mvn spring-boot:run -Pdocker-compose -Dspring-boot.run.profiles=docker-compose
我們還添加了專用的 Maven 構建配置文件 ( -Pdocker-compose
),以防其他配置文件存在。
現在,如果我們執行docker ps
,我們將看到 MongoDb 容器正在運行:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77a9667b291a mongo:latest "docker-entrypoint.s…" 21 hours ago Up 10 minutes 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp classes-db-1
我們現在可以對我們的應用程序進行一些實時測試。
5.2.申請檢查
我們可以使用執行器端點檢查我們的應用程序是否已啟動並正在運行:
curl --location 'http://localhost:8080/actuator/health'
如果一切正常,我們應該得到200
狀態:
{
"status": "UP"
}
對於數據庫檢查,讓我們在端點http://localhost:8080/item.
例如,讓我們看一個curl
Post請求:
curl --location 'http://localhost:8080/item' \
--header 'Content-Type: application/json' \
--data '{
"name" : "Tennis Ball",
"quantity" : 5,
"category" : "sport"
}'
我們將收到包含生成的項目id
響應:
{
"id": "64b117b6a805f7296d8412d9",
"name": "Tennis Ball",
"quantity": 5,
"category": "sport"
}
5.3.應用程序關閉
最後,關閉 Spring Boot 3 應用程序也會停止我們的容器。我們可以通過執行docker ps -a
看到:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77a9667b291a mongo:latest "docker-entrypoint.s…" 22 hours ago Exited (0) 5 seconds ago classes-db-1
6.Docker Compose 支持功能
讓我們快速說明最相關的 Docker Compose 支持功能。
6.1.服務連接
此支持將在啟動時自動發現多種服務。我們已經看到了 MongoDB。但是,還有其他一些,例如 Redis 或 ElasticSearch。服務連接將查找並使用本地映射的端口。我們可以跳過配置類或屬性。這是由 Spring Boot 使用ConnectionDetails
抽象來完成的。
6.2.自定義圖像
我們可以通過應用label
來使用自定義 Docker 鏡像:
version: '3.8'
services:
db:
image: our-custom-mongo-image
ports:
- '27017:27017'
volumes:
- db:/data/db
labels:
org.springframework.boot.service-connection: mongo
volumes:
db:
driver:
local
6.3.等待容器準備就緒
有趣的是,Spring Boot 3 將自動檢查容器準備情況。容器可能需要一些時間才能完全準備好。因此,這個功能讓我們可以使用healthcheck
命令來查看容器是否準備就緒。
6.4.激活 Docker Compose 配置文件
我們可以在運行時在不同的 Docker Compose配置文件之間切換。我們的服務定義可能很複雜,因此我們可能想要選擇啟用哪個配置文件,例如,如果我們處於調試或生產環境中。我們可以通過使用配置屬性來實現這一點:
spring.docker.compose.profiles.active=myprofile
7. Docker Compose 支持的好處
在生產環境中,我們的docker服務可以分佈在不同的實例上。因此,在這種情況下,我們可能不需要這種支持。但是,我們可以激活從docker-compose.yml
定義加載的 Spring 配置文件,僅用於本地開發。
這種支持與我們的 IDE 很好地集成,我們不會在命令行上來回跳轉來啟動和停止 Docker 服務。
從 3.1 版本開始支持。總體而言,已經有了一些很好的功能,例如多服務連接、服務準備就緒的默認檢查以及使用 Docker Compose 配置文件的可能性。
八、結論
在本文中,我們了解了 Spring Boot 3.1.0 中新的 Docker Compose 支持。我們了解瞭如何使用它設置和創建 Spring Boot 3 應用程序。
繼 Spring Boot 易於開發的特點之後,這種支持也很方便,並且已經具備了很好的特性。在啟動和停止應用程序時,Spring Boot 3 管理 Docker 服務的生命週期。
與往常一樣,本文中提供的代碼可用 在 GitHub 上。與往常一樣,本文中提供的代碼可用 在 GitHub 上。