Golang 在後端開發、並發操作方面表現出色,是建立可擴展和高效能後端應用程式的完美套件。由於缺乏圍繞 Go 工作區的微服務架構的帖子,這是透過不同服務共享模組化程式碼的令人難以置信的工具,我決定分享我的實現。
mkdir docker touch docker/Dockerfile.authentication touch docker/Dockerfile.users mkdir -p services/authentication mkdir -p services/users mkdir -p shared/utils touch docker-compose.yml
以下 shell 指令將產生以下資料夾樹結構
在專案的根目錄下,只需使用簡單的指令 go work init 建立一個 go 工作區,這將產生 go.work 檔案
接下來,初始化所有能夠保存依賴項的不同 go 項目,並執行程式碼庫。
cd services/authentication && go mod init github.com/LegationPro/ms/services/authentication cd ../.. && cd services/users && go mod init github.com/LegationPro/ms/services/users cd ../.. && cd shared && go mod init github.com/LegationPro/ms/shared
在執行以下命令後,您的專案應如下所示
接下來,我們將填充 go 工作區,並透過執行以下命令告訴它工作區的一部分是什麼
去工作使用 ./services/authentication ./services/users ./shared
這將填入 go.work 檔案
go 1.23.1 use ( ./services/authentication ./services/users ./shared )
讓我們先來看看 docker-compose.yml。
您的 docker-compose.yml 檔案應如下所示
services: authentication: build: context: . dockerfile: docker/Dockerfile.authentication volumes: - ./services/authentication:/app/authentication - ./shared:/app/shared ports: - "8081:8081" users: build: context: . dockerfile: docker/Dockerfile.users volumes: - ./services/users:/app/users - ./shared:/app/shared ports: - "8082:8082"
我們告訴 docker-compose 使用以下服務,即身份驗證和使用者。
我們給出的是根上下文,因此我們可以存取根層級的檔案和資料夾。
提供 dockerfile 位置。
定義容器的給定體積,並在最後公開容器運行的連接埠。
設定 Dockerfile 非常簡單且直接。
我們拉取最新的 golang alpine 鏡像,分配一個工作目錄,移動一些程式碼,調整它以適應 go 工作空間結構,然後簡單地運行它。
docker/Dockerfile.authentication
# Pull golang image FROM golang:1.23-alpine # Switch to /app as the working directory WORKDIR /app # Copy the authentication codebase over to our container COPY ./services/authentication /app/authentication/ # Copy the shared codebase and libraries that are shared across our apps inside the container COPY ./shared /app/shared # Initialize go workspace inside of our container RUN go work init # Assign different codebases to go workspaces RUN go work use ./authentication ./shared # Simply run our service with this simple command CMD ["go", "run", "./authentication"]
Dockerfile.users
# Pull golang image FROM golang:1.23-alpine # Switch to /app as the working directory WORKDIR /app # Copy the authentication codebase over to our container COPY ./services/users /app/users/ # Copy the shared codebase and libraries that are shared across our apps inside the container COPY ./shared /app/shared # Initialize go workspace inside of our container RUN go work init # Assign different codebases to go workspaces RUN go work use ./users ./shared # Simply run our service with this simple command CMD ["go", "run", "./users"]
services/authentication/main.go
package main import ( "fmt" "github.com/LegationPro/ms/shared/utils" ) func main() { fmt.Println(utils.SomeAuthFunc()) }
services/users/main.go
package main import ( "fmt" "github.com/LegationPro/ms/shared/utils" ) func main() { fmt.Println(utils.SomeUserFunc()) }
分享/utils/utils.go
package utils func SomeAuthFunc() string { return "Some auth func" } func SomeUserFunc() string { return "Some user func" }
現在的結構應該是這樣的
docker-compose up --build
為了確保一切正常,輸出應如下所示:
就是這樣,您就擁有了一個功能齊全的 Go Workspace 模組化微服務架構設定! ??
原始碼:https://github.com/LegationPro/go-microservice-modular-docker-setup
感謝您閱讀我的博文,希望這對您有所幫助❤️!
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3