diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3eac1ff --- /dev/null +++ b/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +*.md +.git +.vs +.vscode +bin +obj +logs +*.user diff --git a/DEPLOY.md b/DEPLOY.md new file mode 100644 index 0000000..e768c23 --- /dev/null +++ b/DEPLOY.md @@ -0,0 +1,72 @@ +# Docker 部署说明 + +## 快速启动 + +### 1. 直接使用 docker-compose 启动 +```bash +# 启动服务(需要提前配置好外部 MySQL 连接) +docker-compose up -d + +# 查看日志 +docker-compose logs -f api + +# 停止服务 +docker-compose down +``` + +### 2. 单独构建镜像 +```bash +# 构建镜像 +docker build -t assetmanager-api:latest . + +# 运行容器(需要自行配置 MySQL 连接) +docker run -d -p 7040:8080 \ + -e ConnectionStrings__Default="server=你的MySQL地址;Database=assetmanager;Uid=root;Pwd=你的密码;CharSet=utf8mb4;" \ + -e Jwt__SecretKey="你的自定义密钥" \ + assetmanager-api:latest +``` + +## 环境变量说明 + +| 环境变量 | 说明 | 默认值 | +|---------|------|--------| +| `ASPNETCORE_ENVIRONMENT` | 运行环境 | `Production` | +| `ConnectionStrings__Default` | MySQL 连接字符串 | - | +| `Jwt__SecretKey` | JWT 签名密钥 | 开发环境默认值 | +| `Jwt__Issuer` | JWT 签发者 | `AssetManager` | +| `Jwt__Audience` | JWT 受众 | `AssetManager` | +| `Wechat__AppId` | 微信小程序 AppId | 测试默认值 | +| `Wechat__AppSecret` | 微信小程序 AppSecret | 测试默认值 | +| `Tiingo__ApiKey` | Tiingo 股票数据 API Key | 测试默认值 | +| `TZ` | 时区 | `Asia/Shanghai` | + +## 端口说明 + +| 端口 | 服务 | 说明 | +|------|------|------| +| `7040` | 后端 API | HTTP 接口 | + +## 数据持久化 + +- 应用日志:存储在 `logs` 卷中 + +## 生产环境注意事项 + +1. **必须修改默认密码和密钥**: + - MySQL 连接密码 + - JWT 签名密钥 (`Jwt__SecretKey`) + - 微信 AppId/AppSecret + - Tiingo API Key + +2. 建议使用反向代理(Nginx/Caddy)提供 HTTPS 访问 + +## 常见问题 + +### 启动后 API 访问 500 +检查 MySQL 连接是否正常,确认连接字符串配置正确。 + +### 微信登录失败 +检查 `Wechat__AppId` 和 `Wechat__AppSecret` 是否配置正确。 + +### 市场数据获取失败 +检查 `Tiingo__ApiKey` 是否有效,网络是否能访问 Tiingo API。 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5a3a0dc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,45 @@ +# 使用 .NET 8 SDK 作为构建镜像 +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +WORKDIR /src + +# 复制解决方案文件和项目文件 +COPY ["AssetManager.sln", "."] +COPY ["AssetManager.API/AssetManager.API.csproj", "AssetManager.API/"] +COPY ["AssetManager.Services/AssetManager.Services.csproj", "AssetManager.Services/"] +COPY ["AssetManager.Infrastructure/AssetManager.Infrastructure.csproj", "AssetManager.Infrastructure/"] +COPY ["AssetManager.Models/AssetManager.Models.csproj", "AssetManager.Models/"] +COPY ["AssetManager.Data/AssetManager.Data.csproj", "AssetManager.Data/"] + +# 还原 NuGet 包 +RUN dotnet restore "AssetManager.API/AssetManager.API.csproj" + +# 复制所有文件并构建 +COPY . . +WORKDIR "/src/AssetManager.API" +RUN dotnet build "AssetManager.API.csproj" -c Release -o /app/build + +# 发布应用 +FROM build AS publish +RUN dotnet publish "AssetManager.API.csproj" -c Release -o /app/publish /p:UseAppHost=false + +# 构建最终运行时镜像 +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +WORKDIR /app +EXPOSE 8080 +EXPOSE 8081 + +# 设置时区为 Asia/Shanghai +ENV TZ=Asia/Shanghai +RUN apt-get update && apt-get install -y tzdata && ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +# 从发布阶段复制文件 +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . + +# 配置环境变量(可以在运行时覆盖) +ENV ASPNETCORE_ENVIRONMENT=Production +ENV ASPNETCORE_URLS=http://+:8080 + +# 启动应用 +ENTRYPOINT ["dotnet", "AssetManager.API.dll"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..204b689 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +version: '3.8' + +services: + # 后端 API 服务 + api: + build: . + container_name: assetmanager-api + restart: always + ports: + - "7040:8080" + environment: + - ASPNETCORE_ENVIRONMENT=Production + # 外部 MySQL 连接配置(自行修改) + - ConnectionStrings__Default=server=你的MySQL地址;Database=assetmanager;Uid=你的用户名;Pwd=你的密码;CharSet=utf8mb4;Allow User Variables=true; + # JWT 配置 + - Jwt__SecretKey=your-strong-secret-key-here-change-in-production + - Jwt__Issuer=AssetManager + - Jwt__Audience=AssetManager + # 微信配置 + - Wechat__AppId=wx245f0f3ebcfcf5a7 + - Wechat__AppSecret=809c740129bc8b434177ce12ef292dd0 + # Tiingo API 配置 + - Tiingo__ApiKey=bd00fee76d3012b047473078904001b33322cb46 + # 时区 + - TZ=Asia/Shanghai + volumes: + - logs:/app/logs + +volumes: + logs: