Skip to content

开发环境

1. 概述

梵医云系统是一个基于 Spring Cloud 的微服务架构项目,采用前后端分离的开发模式。本文档介绍如何搭建和配置梵医云系统的开发环境。

2. 环境要求

2.1 硬件要求

组件最低配置推荐配置
CPU8核16 核及以上
内存32 GB48 GB 及以上
硬盘100 GB200 GB 及以上
网络100 Mbps1 Gbps

2.2 软件要求

软件版本要求说明
JDK17+Java 开发环境
Maven3.6+项目构建工具
Node.js16+前端开发环境
Git2.0+版本控制工具
IDEIntelliJ IDEA / VS Code开发工具
MySQL8.0+数据库
Redis6.0+缓存数据库
Nacos2.0+注册中心和配置中心

2.3 操作系统

  • Windows 10/11
  • macOS 10.15+
  • Linux (Ubuntu 20.04+, CentOS 7+)

3. 开发环境搭建

3.1 安装 JDK

3.1.1 下载 JDK

从 Oracle 官网下载 JDK 17 或更高版本:

https://www.oracle.com/java/technologies/downloads/

3.1.2 配置环境变量

Windows

bash
# 设置 JAVA_HOME
setx JAVA_HOME "C:\Program Files\Java\jdk-17"

# 添加到 PATH
setx PATH "%PATH%;%JAVA_HOME%\bin"

Linux/macOS

bash
# 编辑 ~/.bashrc 或 ~/.zshrc
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH

# 使配置生效
source ~/.bashrc

3.1.3 验证安装

bash
java -version

3.2 安装 Maven

3.2.1 下载 Maven

从 Maven 官网下载 Maven 3.6+:

https://maven.apache.org/download.cgi

3.2.2 配置环境变量

Windows

bash
# 设置 MAVEN_HOME
setx MAVEN_HOME "C:\Program Files\Apache\maven-3.8.6"

# 添加到 PATH
setx PATH "%PATH%;%MAVEN_HOME%\bin"

Linux/macOS

bash
# 编辑 ~/.bashrc 或 ~/.zshrc
export MAVEN_HOME=/usr/local/apache-maven-3.8.6
export PATH=$MAVEN_HOME/bin:$PATH

# 使配置生效
source ~/.bashrc

3.2.3 配置 Maven 镜像

编辑 ~/.m2/settings.xml

xml
<mirrors>
    <mirror>
        <id>aliyunmaven</id>
        <mirrorOf>*</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
</mirrors>

3.2.4 验证安装

bash
mvn -version

3.3 安装 MySQL

3.3.1 下载 MySQL

从 MySQL 官网下载 MySQL 8.0+:

https://dev.mysql.com/downloads/mysql/

3.3.2 安装 MySQL

Windows: 运行安装程序,按照提示完成安装。

Linux

bash
sudo apt-get update
sudo apt-get install mysql-server

macOS

bash
brew install mysql

3.3.3 配置 MySQL

编辑 MySQL 配置文件 my.cnf

ini
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
max_connections=1000

3.3.4 创建数据库

sql
CREATE DATABASE `db-fanyi-cloud` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

3.3.5 导入初始化脚本

bash
mysql -u root -p db-fanyi-cloud < sql/fanyi-cloud.sql

3.4 安装 Redis

3.4.1 下载 Redis

从 Redis 官网下载 Redis 6.0+:

https://redis.io/download

3.4.2 安装 Redis

Windows: 下载 Windows 版本的 Redis 或使用 WSL。

Linux

bash
sudo apt-get update
sudo apt-get install redis-server

macOS

bash
brew install redis

3.4.3 启动 Redis

Linux/macOS

bash
redis-server

Windows

bash
redis-server.exe

3.4.4 验证安装

bash
redis-cli ping

3.5 安装 Nacos

3.5.1 下载 Nacos

从 Nacos 官网下载 Nacos 2.0+:

https://github.com/alibaba/nacos/releases

3.5.2 启动 Nacos

单机模式

bash
cd nacos/bin
./startup.sh -m standalone

Windows

bash
cd nacos\bin
startup.cmd -m standalone

3.5.3 访问 Nacos

打开浏览器访问:

http://localhost:8848/nacos

默认账号密码:nacos/nacos

3.5.4 创建命名空间

登录 Nacos 后,创建 dev 命名空间。

3.6 安装 Node.js

3.6.1 下载 Node.js

从 Node.js 官网下载 Node.js 16+:

https://nodejs.org/

3.6.2 验证安装

bash
node -v
npm -v

3.6.3 配置 npm 镜像

bash
npm config set registry https://registry.npmmirror.com

3.7 安装 Git

3.7.1 下载 Git

从 Git 官网下载 Git 2.0+:

https://git-scm.com/downloads

3.7.2 配置 Git

bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

3.7.3 验证安装

bash
git --version

4. 项目结构

4.1 后端项目结构

fanyi-cloud/
├── fanyi-dependencies/          # 依赖管理
├── fanyi-framework/            # 框架模块
│   ├── fanyi-common/          # 通用模块
│   ├── fanyi-spring-boot-starter-web/  # Web 模块
│   ├── fanyi-spring-boot-starter-security/  # 安全模块
│   ├── fanyi-spring-boot-starter-mybatis/  # MyBatis 模块
│   ├── fanyi-spring-boot-starter-redis/  # Redis 模块
│   ├── fanyi-spring-boot-starter-mq/  # 消息队列模块
│   ├── fanyi-spring-boot-starter-job/  # 定时任务模块
│   ├── fanyi-spring-boot-starter-websocket/  # WebSocket 模块
│   ├── fanyi-spring-boot-starter-protection/  # 限流熔断模块
│   ├── fanyi-spring-boot-starter-biz-tenant/  # 多租户模块
│   └── fanyi-spring-boot-starter-monitor/  # 监控模块
├── fanyi-gateway/             # 网关模块
├── fanyi-module-system/        # 系统模块
├── fanyi-module-infra/        # 基础设施模块
├── fanyi-module-member/       # 会员模块
├── fanyi-module-bpm/         # 流程模块
├── fanyi-module-pay/         # 支付模块
├── fanyi-module-report/       # 报表模块
├── fanyi-module-mp/          # 微信公众号模块
├── fanyi-module-mall/        # 商城模块
├── fanyi-module-crm/         # CRM 模块
├── fanyi-module-erp/         # ERP 模块
├── fanyi-module-ai/          # AI 模块
├── fanyi-module-appointment/  # 预约模块
├── fanyi-module-partner/      # 合作伙伴模块
└── pom.xml                   # Maven 配置文件

4.2 前端项目结构

fanyi-cloud-ui/
├── src/
│   ├── api/                  # API 接口
│   ├── assets/                # 静态资源
│   ├── components/            # 组件
│   ├── router/                # 路由
│   ├── store/                 # 状态管理
│   ├── utils/                 # 工具函数
│   ├── views/                 # 页面
│   ├── App.vue                # 根组件
│   └── main.ts               # 入口文件
├── public/                   # 公共资源
├── package.json              # 依赖配置
├── vite.config.ts           # Vite 配置
└── tsconfig.json            # TypeScript 配置

5. 配置说明

5.1 后端配置

5.1.1 主配置文件

编辑 application.yaml

yaml
spring:
  profiles:
    active: dev
  cloud:
    nacos:
      server-addr: 192.168.31.10:8848
      username: nacos
      password: dWTenQzIEcMWDCzOwaRQNUVRXCqJl3f5
      config:
        namespace: dev
        group: DEFAULT_GROUP
        refresh:
          enabled: false
      discovery:
        group: DEFAULT_GROUP
        metadata:
          version: 1.0.0

5.1.2 数据库配置

在 Nacos 中配置 common.yaml

yaml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db-fanyi-cloud?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

5.1.3 Redis 配置

在 Nacos 中配置 common.yaml

yaml
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password:
    database: 0
    timeout: 5000ms
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: -1ms

5.1.4 MyBatis Plus 配置

yaml
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      id-type: NONE
      logic-delete-value: 1
      logic-not-delete-value: 0
    banner: false
  type-aliases-package: ${fanyi.info.base-package}.dal.dataobject
  encryptor:
    password: XDV71a+xqStEA3WH

5.2 前端配置

5.2.1 环境配置

编辑 .env.development

bash
VITE_APP_TITLE = 梵医云管理系统
VITE_APP_BASE_API = http://localhost:48080
VITE_APP_BASE_WS = ws://localhost:48080

5.2.2 代理配置

编辑 vite.config.ts

typescript
export default defineConfig({
  server: {
    proxy: {
      '/admin-api': {
        target: 'http://localhost:48080',
        changeOrigin: true,
      },
      '/app-api': {
        target: 'http://localhost:48080',
        changeOrigin: true,
      },
    },
  },
})

6. 启动步骤

6.1 启动基础服务

6.1.1 启动 MySQL

bash
# Linux/macOS
sudo systemctl start mysql

# Windows
net start MySQL

6.1.2 启动 Redis

bash
# Linux/macOS
redis-server

# Windows
redis-server.exe

6.1.3 启动 Nacos

bash
cd nacos/bin
./startup.sh -m standalone

6.2 启动后端服务

6.2.1 克隆项目

bash
git clone https://github.com/your-org/fanyi-cloud.git
cd fanyi-cloud

6.2.2 安装依赖

bash
mvn clean install -DskipTests

6.2.3 启动网关服务

bash
cd fanyi-gateway
mvn spring-boot:run

6.2.4 启动业务服务

bash
# 启动系统服务
cd fanyi-module-system/fanyi-module-system-biz
mvn spring-boot:run

# 启动基础设施服务
cd fanyi-module-infra/fanyi-module-infra-biz
mvn spring-boot:run

# 启动会员服务
cd fanyi-module-member/fanyi-module-member-biz
mvn spring-boot:run

6.2.5 验证服务启动

打开浏览器访问:

http://localhost:48080

6.3 启动前端服务

6.3.1 克隆项目

bash
git clone https://github.com/your-org/fanyi-cloud-ui.git
cd fanyi-cloud-ui

6.3.2 安装依赖

bash
npm install

6.3.3 启动开发服务器

bash
npm run dev

6.3.4 访问前端

打开浏览器访问:

http://localhost:5173

7. 开发工具配置

7.1 IntelliJ IDEA 配置

7.1.1 安装插件

  • Lombok Plugin
  • MyBatis Plugin
  • Alibaba Java Coding Guidelines
  • Maven Helper
  • Rainbow Brackets

7.1.2 配置 Maven

打开 Settings -> Build, Execution, Deployment -> Build Tools -> Maven

  • Maven home directory: 选择 Maven 安装目录
  • User settings file: 选择 Maven settings.xml
  • Local repository: 选择 Maven 本地仓库

7.1.3 配置 JDK

打开 Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler

  • Project bytecode version: 17
  • Target bytecode version: 17

7.2 VS Code 配置

7.2.1 安装插件

  • Volar
  • TypeScript Vue Plugin (Volar)
  • ESLint
  • Prettier
  • GitLens

7.2.2 配置 TypeScript

编辑 .vscode/settings.json

json
{
  "typescript.tsdk": "node_modules/typescript/lib",
  "volar.takeOverMode.enabled": false
}

8. 常见问题

8.1 Maven 依赖下载失败

问题:Maven 依赖下载失败或速度慢

解决方案

  1. 配置 Maven 镜像
  2. 检查网络连接
  3. 清除 Maven 本地仓库缓存
bash
rm -rf ~/.m2/repository

8.2 端口被占用

问题:启动服务时提示端口被占用

解决方案

  1. 查找占用端口的进程
bash
# Windows
netstat -ano | findstr :48080

# Linux/macOS
lsof -i :48080
  1. 结束占用端口的进程
bash
# Windows
taskkill /PID <pid> /F

# Linux/macOS
kill -9 <pid>

8.3 数据库连接失败

问题:无法连接到数据库

解决方案

  1. 检查数据库是否启动
  2. 检查数据库配置是否正确
  3. 检查防火墙设置
  4. 检查数据库用户权限

8.4 Redis 连接失败

问题:无法连接到 Redis

解决方案

  1. 检查 Redis 是否启动
  2. 检查 Redis 配置是否正确
  3. 检查防火墙设置
  4. 检查 Redis 密码配置

8.5 Nacos 连接失败

问题:无法连接到 Nacos

解决方案

  1. 检查 Nacos 是否启动
  2. 检查 Nacos 配置是否正确
  3. 检查防火墙设置
  4. 检查 Nacos 命名空间配置

8.6 前端启动失败

问题:前端启动失败

解决方案

  1. 清除 node_modules
bash
rm -rf node_modules
npm install
  1. 检查 Node.js 版本
  2. 检查 npm 镜像配置
  3. 检查端口是否被占用

9. 开发规范

9.1 代码规范

  • 遵循阿里巴巴 Java 开发手册
  • 使用 Lombok 简化代码
  • 使用 MapStruct 进行对象转换
  • 使用 MyBatis Plus 进行数据库操作

9.2 Git 规范

  • 使用语义化版本号
  • 使用规范的提交信息
  • 使用分支管理策略

9.3 接口规范

  • 使用 RESTful 风格
  • 统一返回格式
  • 统一异常处理
  • 统一日志记录

10. 注意事项

  1. 环境一致性:确保开发环境与测试环境、生产环境保持一致
  2. 依赖管理:使用 Maven 管理依赖,避免版本冲突
  3. 配置管理:使用 Nacos 管理配置,实现配置的集中管理
  4. 日志管理:合理配置日志级别,便于问题排查
  5. 安全规范:不要将敏感信息提交到代码仓库
  6. 性能优化:定期检查系统性能,优化代码和配置
  7. 备份策略:定期备份数据库和配置文件
  8. 监控告警:配置系统监控和告警,及时发现问题

11. 相关文档