feat: 初始化后端微服务架构骨架
This commit is contained in:
45
backend/Makefile
Normal file
45
backend/Makefile
Normal file
@@ -0,0 +1,45 @@
|
||||
# Makefile for backend services
|
||||
|
||||
# 默认目标
|
||||
.DEFAULT_GOAL := help
|
||||
|
||||
# 帮助信息
|
||||
help:
|
||||
@echo "Available commands:"
|
||||
@echo " make build - 构建所有服务"
|
||||
@echo " make up - 启动所有服务"
|
||||
@echo " make up-dev - 启动开发模式服务(热更新)"
|
||||
@echo " make down - 停止所有服务"
|
||||
@echo " make logs - 查看所有服务日志"
|
||||
@echo " make gen-proto - 生成 proto 文件"
|
||||
@echo " make clean - 清理构建文件"
|
||||
|
||||
# 构建所有服务
|
||||
build:
|
||||
docker compose build
|
||||
|
||||
# 启动所有服务
|
||||
up:
|
||||
docker compose up -d
|
||||
|
||||
# 启动开发模式服务(热更新)
|
||||
up-dev:
|
||||
docker compose -f docker-compose.dev.yml up -d
|
||||
|
||||
# 停止所有服务
|
||||
down:
|
||||
docker compose down
|
||||
|
||||
# 查看所有服务日志
|
||||
logs:
|
||||
docker compose logs -f
|
||||
|
||||
# 生成 proto 文件
|
||||
gen-proto:
|
||||
@echo "Generating proto files..."
|
||||
docker compose run --rm proto-builder
|
||||
|
||||
# 清理构建文件
|
||||
clean:
|
||||
docker compose down -v
|
||||
docker rmi -f $$(docker images -q "backend_*" 2>/dev/null) 2>/dev/null || true
|
||||
85
backend/README.md
Normal file
85
backend/README.md
Normal file
@@ -0,0 +1,85 @@
|
||||
# 后端微服务架构
|
||||
|
||||
这是一个基于纯 Docker 环境构建的最小化后端骨架,不需要主机依赖。
|
||||
|
||||
## 技术栈
|
||||
- golang:1.26.1-alpine3.23
|
||||
- postgres:18.3-alpine3.23
|
||||
- redis:8.6.2-alpine
|
||||
- Docker/Docker Compose
|
||||
- nginx:1.25-alpine
|
||||
|
||||
## 目录结构
|
||||
```
|
||||
./backend/
|
||||
├── gateway/ # API 网关服务
|
||||
├── services/ # 微服务
|
||||
│ └── user-svc/ # 用户服务
|
||||
├── shared/ # 共享代码和 proto 文件
|
||||
├── scripts/ # 工具脚本
|
||||
├── docker-compose.yml # 生产环境 docker compose 文件
|
||||
├── docker-compose.dev.yml # 开发模式 docker compose 文件
|
||||
├── Makefile # Docker 命令封装
|
||||
└── README.md # 本文档
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 前置条件
|
||||
- Docker
|
||||
- Docker Compose
|
||||
|
||||
### 启动服务
|
||||
1. **构建并启动服务**
|
||||
```bash
|
||||
# 使用 Makefile
|
||||
make up
|
||||
|
||||
# 或使用脚本
|
||||
./scripts/dev-start.sh
|
||||
```
|
||||
|
||||
2. **以开发模式启动(热更新)**
|
||||
```bash
|
||||
make up-dev
|
||||
```
|
||||
|
||||
3. **生成 proto 文件**
|
||||
```bash
|
||||
make gen-proto
|
||||
```
|
||||
|
||||
4. **停止服务**
|
||||
```bash
|
||||
make down
|
||||
```
|
||||
|
||||
### 访问服务
|
||||
- Nginx: http://localhost:8080
|
||||
- 网关: http://localhost:8000
|
||||
- 用户服务: http://localhost:9000
|
||||
- PostgreSQL: localhost:5432
|
||||
- Redis: localhost:6379
|
||||
|
||||
## 开发
|
||||
|
||||
### 添加新服务
|
||||
1. 在 `services/` 目录下创建新目录
|
||||
2. 添加 `Dockerfile`、`go.mod` 和必要的目录结构
|
||||
3. 更新 `docker-compose.yml` 和 `docker-compose.dev.yml` 以包含新服务
|
||||
|
||||
### Proto 文件
|
||||
- 在 `shared/proto/common/` 目录下添加 proto 文件
|
||||
- 运行 `make gen-proto` 生成 Go 代码
|
||||
|
||||
## 生产环境
|
||||
|
||||
对于生产部署,使用 `docker-compose.yml`,它会构建优化的镜像,不包含卷挂载。
|
||||
|
||||
## 清理
|
||||
|
||||
```bash
|
||||
make clean
|
||||
```
|
||||
|
||||
这将停止所有服务,移除卷,并清理镜像。
|
||||
64
backend/docker-compose.dev.yml
Normal file
64
backend/docker-compose.dev.yml
Normal file
@@ -0,0 +1,64 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
# Nginx 网关
|
||||
nginx:
|
||||
image: nginx:1.25-alpine
|
||||
ports:
|
||||
- "8080:80"
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
depends_on:
|
||||
- gateway
|
||||
|
||||
# 网关服务
|
||||
gateway:
|
||||
build:
|
||||
context: ./gateway
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "8000:8000"
|
||||
volumes:
|
||||
- ./gateway:/app
|
||||
command: air -c .air.toml
|
||||
depends_on:
|
||||
- user-svc
|
||||
- redis
|
||||
|
||||
# 用户服务
|
||||
user-svc:
|
||||
build:
|
||||
context: ./services/user-svc
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "9000:9000"
|
||||
volumes:
|
||||
- ./services/user-svc:/app
|
||||
command: air -c .air.toml
|
||||
depends_on:
|
||||
- postgres
|
||||
|
||||
# 数据库
|
||||
postgres:
|
||||
image: postgres:18.3-alpine3.23
|
||||
environment:
|
||||
POSTGRES_USER: admin
|
||||
POSTGRES_PASSWORD: password
|
||||
POSTGRES_DB: backend
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
- ./services/user-svc/migrations:/docker-entrypoint-initdb.d
|
||||
|
||||
# Redis
|
||||
redis:
|
||||
image: redis:8.6.2-alpine
|
||||
ports:
|
||||
- "6379:6379"
|
||||
volumes:
|
||||
- redis-data:/var/lib/redis/data
|
||||
|
||||
volumes:
|
||||
postgres-data:
|
||||
redis-data:
|
||||
66
backend/docker-compose.yml
Normal file
66
backend/docker-compose.yml
Normal file
@@ -0,0 +1,66 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
# Nginx 网关
|
||||
nginx:
|
||||
image: nginx:1.25-alpine
|
||||
ports:
|
||||
- "8080:80"
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
depends_on:
|
||||
- gateway
|
||||
|
||||
# 网关服务
|
||||
gateway:
|
||||
build:
|
||||
context: ./gateway
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "8000:8000"
|
||||
depends_on:
|
||||
- user-svc
|
||||
- redis
|
||||
|
||||
# 用户服务
|
||||
user-svc:
|
||||
build:
|
||||
context: ./services/user-svc
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "9000:9000"
|
||||
depends_on:
|
||||
- postgres
|
||||
|
||||
# 数据库
|
||||
postgres:
|
||||
image: postgres:18.3-alpine3.23
|
||||
environment:
|
||||
POSTGRES_USER: admin
|
||||
POSTGRES_PASSWORD: password
|
||||
POSTGRES_DB: backend
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
- ./services/user-svc/migrations:/docker-entrypoint-initdb.d
|
||||
|
||||
# Redis
|
||||
redis:
|
||||
image: redis:8.6.2-alpine
|
||||
ports:
|
||||
- "6379:6379"
|
||||
volumes:
|
||||
- redis-data:/var/lib/redis/data
|
||||
|
||||
# Proto 编译服务
|
||||
proto-builder:
|
||||
build:
|
||||
context: ./scripts/docker-proto-builder
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./shared/proto:/proto
|
||||
|
||||
volumes:
|
||||
postgres-data:
|
||||
redis-data:
|
||||
34
backend/gateway/Dockerfile
Normal file
34
backend/gateway/Dockerfile
Normal file
@@ -0,0 +1,34 @@
|
||||
FROM golang:1.26.1-alpine3.23 as builder
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制 go.mod 和 go.sum
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# 下载依赖
|
||||
RUN go mod download
|
||||
|
||||
# 复制源代码
|
||||
COPY . .
|
||||
|
||||
# 构建应用
|
||||
RUN go build -o gateway ./cmd/main.go
|
||||
|
||||
# 运行阶段
|
||||
FROM alpine:3.23
|
||||
|
||||
# 安装必要的依赖
|
||||
RUN apk add --no-cache ca-certificates
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 从构建阶段复制二进制文件
|
||||
COPY --from=builder /app/gateway .
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 8000
|
||||
|
||||
# 启动应用
|
||||
CMD ["./gateway"]
|
||||
24
backend/scripts/dev-start.sh
Normal file
24
backend/scripts/dev-start.sh
Normal file
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 纯 Docker 启动脚本
|
||||
echo "Starting backend services with Docker..."
|
||||
|
||||
# 进入脚本所在目录的父目录(backend 目录)
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
|
||||
# 构建服务
|
||||
echo "Building services..."
|
||||
docker compose build
|
||||
|
||||
# 启动服务
|
||||
echo "Starting services..."
|
||||
docker compose up -d
|
||||
|
||||
# 查看服务状态
|
||||
echo "Services status:"
|
||||
docker compose ps
|
||||
|
||||
echo "Backend services started successfully!"
|
||||
echo "Nginx: http://localhost:8080"
|
||||
echo "Gateway: http://localhost:8000"
|
||||
echo "User Service: http://localhost:9000"
|
||||
14
backend/scripts/docker-proto-builder/Dockerfile
Normal file
14
backend/scripts/docker-proto-builder/Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
||||
FROM golang:1.26.1-alpine3.23
|
||||
|
||||
# 安装 protoc 和相关工具
|
||||
RUN apk add --no-cache protobuf-dev
|
||||
|
||||
# 安装 protoc-gen-go 和 protoc-gen-go-grpc
|
||||
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
|
||||
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /proto
|
||||
|
||||
# 生成 proto 文件的命令
|
||||
CMD protoc --go_out=. --go-grpc_out=. common/*.proto
|
||||
12
backend/scripts/gen-proto.sh
Normal file
12
backend/scripts/gen-proto.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Docker 内编译 proto 脚本
|
||||
echo "Generating proto files..."
|
||||
|
||||
# 进入脚本所在目录的父目录(backend 目录)
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
|
||||
# 运行 proto-builder 容器
|
||||
docker compose run --rm proto-builder
|
||||
|
||||
echo "Proto files generated successfully!"
|
||||
34
backend/services/user-svc/Dockerfile
Normal file
34
backend/services/user-svc/Dockerfile
Normal file
@@ -0,0 +1,34 @@
|
||||
FROM golang:1.26.1-alpine3.23 as builder
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制 go.mod 和 go.sum
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# 下载依赖
|
||||
RUN go mod download
|
||||
|
||||
# 复制源代码
|
||||
COPY . .
|
||||
|
||||
# 构建应用
|
||||
RUN go build -o user-svc ./cmd/main.go
|
||||
|
||||
# 运行阶段
|
||||
FROM alpine:3.23
|
||||
|
||||
# 安装必要的依赖
|
||||
RUN apk add --no-cache ca-certificates
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 从构建阶段复制二进制文件
|
||||
COPY --from=builder /app/user-svc .
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 9000
|
||||
|
||||
# 启动应用
|
||||
CMD ["./user-svc"]
|
||||
Reference in New Issue
Block a user