若依微服务(RuoYi-Cloud)部署上云实战:Linux服务器+Nginx配置全流程与常见问题排查
若依微服务RuoYi-Cloud部署上云实战Linux服务器Nginx配置全流程与常见问题排查当微服务架构的项目开发接近尾声如何将若依微服务全家桶包括多个后端Jar包、前端Vue项目高效、稳定地部署到Linux云服务器成为每个全栈开发者和运维人员必须面对的挑战。本文将从实战角度出发不仅提供标准部署步骤更会深入探讨生产环境中的关键配置细节和典型问题排查思路帮助您避开那些官方文档中未曾提及的坑。1. 环境准备与基础配置部署若依微服务到Linux服务器前需要确保服务器环境满足以下基础要求操作系统推荐使用CentOS 7.x或Ubuntu 18.04 LTS及以上版本Java环境JDK 1.8必须与开发环境版本一致数据库MySQL 5.7建议使用与开发环境相同的版本缓存服务Redis 5.0注册中心Nacos 2.0.3Web服务器Nginx 1.18提示生产环境强烈建议使用版本管理工具如rbenv、nvm等安装Node.js而非直接使用系统包管理器安装以避免权限问题。1.1 关键服务安装与配置Nacos生产环境配置# 下载Nacos 2.0.3 wget https://github.com/alibaba/nacos/releases/download/2.0.3/nacos-server-2.0.3.tar.gz tar -zxvf nacos-server-2.0.3.tar.gz cd nacos/conf # 修改集群配置单机模式可跳过 vim cluster.conf # 修改应用配置 vim application.properties关键配置项说明配置项推荐值说明server.port8848保持默认端口nacos.core.auth.enabledtrue生产环境必须开启认证db.num1使用外部MySQL数据库db.url.0jdbc:mysql://127.0.0.1:3306/nacos数据库连接地址db.usernacos数据库用户名db.password强密码数据库密码Redis安全配置# 修改Redis配置文件 vim /etc/redis/redis.conf # 关键配置项 requirepass your_strong_password bind 127.0.0.1 protected-mode yes2. 后端服务部署实战2.1 项目打包与优化在项目根目录执行打包命令前需要特别注意# 清理并打包整个项目跳过测试 mvn clean package -Dmaven.test.skiptrue # 单独打包特定模块 mvn clean package -pl ruoyi-auth -am -Dmaven.test.skiptrue打包完成后各模块的Jar文件会生成在对应模块的target目录下。生产环境部署建议优先确保以下核心服务ruoyi-gatewayAPI网关服务ruoyi-auth认证中心ruoyi-system系统模块ruoyi-monitor监控中心2.2 服务启动脚本编写生产环境推荐使用systemd管理服务以下是一个gateway服务的示例配置# /etc/systemd/system/ruoyi-gateway.service [Unit] DescriptionRuoYi Cloud Gateway Service Afternetwork.target [Service] Typesimple Userappuser WorkingDirectory/opt/ruoyi-cloud ExecStart/usr/bin/java -Xms512m -Xmx512m -jar /opt/ruoyi-cloud/ruoyi-gateway.jar SuccessExitStatus143 Restartalways RestartSec10 EnvironmentNACOS_SERVER127.0.0.1:8848 EnvironmentJAVA_OPTS-Dfile.encodingUTF-8 [Install] WantedBymulti-user.target启动并启用服务sudo systemctl daemon-reload sudo systemctl start ruoyi-gateway sudo systemctl enable ruoyi-gateway2.3 日志管理最佳实践生产环境日志管理建议采用以下方案日志目录结构/var/log/ruoyi-cloud/ ├── gateway │ ├── info.log │ └── error.log ├── auth │ ├── info.log │ └── error.log └── system ├── info.log └── error.logLogback配置示例appender nameFILE classch.qos.logback.core.rolling.RollingFileAppender file/var/log/ruoyi-cloud/${spring.application.name}/info.log/file rollingPolicy classch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy fileNamePattern/var/log/ruoyi-cloud/${spring.application.name}/info.%d{yyyy-MM-dd}.%i.log/fileNamePattern maxFileSize50MB/maxFileSize maxHistory30/maxHistory totalSizeCap1GB/totalSizeCap /rollingPolicy encoder pattern%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n/pattern /encoder /appender3. 前端部署与Nginx配置3.1 Vue项目生产构建在前端项目目录(ruoyi-ui)中执行# 安装依赖建议使用yarn yarn install --production # 构建生产环境代码 yarn build:prod构建完成后dist目录将包含所有静态资源。需要特别注意以下配置vue.config.js中的publicPath必须与Nginx配置匹配.env.production中的API地址必须指向网关服务确保静态资源路径正确避免404错误3.2 Nginx高级配置完整的生产环境Nginx配置示例upstream ruoyi-gateway { server 127.0.0.1:8080; keepalive 64; } server { listen 80; server_name yourdomain.com; # 前端静态资源 location / { root /opt/ruoyi-cloud/dist; index index.html; try_files $uri $uri/ /index.html; } # API代理配置 location /prod-api/ { proxy_pass http://ruoyi-gateway/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 重要WebSocket支持 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; # 超时设置 proxy_connect_timeout 60s; proxy_read_timeout 600s; proxy_send_timeout 600s; } # 静态资源缓存 location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ { root /opt/ruoyi-cloud/dist; expires 365d; access_log off; } # 禁止访问隐藏文件 location ~ /\. { deny all; } }4. 常见问题排查指南4.1 服务启动失败排查流程当某个微服务无法启动时可按以下步骤排查检查日志首先查看服务日志通常位于/var/log/ruoyi-cloud/目录下验证依赖服务# 检查Nacos是否健康 curl http://127.0.0.1:8848/nacos/v1/ns/health/instance?serviceNameruoyi-gateway # 检查Redis连接 redis-cli -h 127.0.0.1 -p 6379 -a yourpassword ping验证端口占用netstat -tulnp | grep 8080检查JVM参数确保内存设置合理避免OOM4.2 前端访问常见问题问题1页面加载但接口请求失败检查浏览器开发者工具中的Network面板确认API请求地址是否正确验证Nginx的/prod-api/路径是否代理到正确的网关地址检查网关服务是否正常运行问题2静态资源404错误确认Nginx配置中的root路径是否正确指向dist目录检查文件权限chown -R nginx:nginx /opt/ruoyi-cloud/dist验证vue.config.js中的publicPath配置4.3 性能优化建议数据库连接池配置# application-prod.yml spring: datasource: druid: initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: false test-on-return: falseJVM调优参数# 生产环境推荐配置 java -server -Xms2g -Xmx2g -XX:MetaspaceSize256m \ -XX:MaxMetaspaceSize512m -XX:UseG1GC \ -XX:MaxGCPauseMillis200 -XX:ParallelGCThreads8 \ -XX:ConcGCThreads4 -XX:InitiatingHeapOccupancyPercent70 \ -jar your-application.jarRedis缓存优化合理设置缓存过期时间避免内存溢出对大对象考虑使用压缩// RedisTemplate配置 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());对热点数据启用本地缓存Caffeine作为二级缓存在实际部署过程中我们发现Nacos的配置管理是最容易出问题的环节。特别是在多环境部署时一定要严格区分dev、test、prod等不同环境的配置。曾经遇到过一个案例测试环境的Redis配置意外覆盖了生产环境导致系统间歇性故障。因此建议在Nacos中为不同环境创建完全独立的命名空间并在服务启动时明确指定-Dspring.cloud.nacos.config.namespaceprod-environment-id
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2443430.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!