Windows Server 2022部署SpringBoot+Vue全流程指南 1. Windows Server 2022部署前后端项目概述最近接手了一个企业级应用系统的部署任务客户要求将SpringBootVue前后端分离项目部署到Windows Server 2022环境。这种部署场景在企业内部系统中非常常见特别是那些历史遗留系统较多、运维团队对Windows更熟悉的传统行业客户。与Linux部署相比Windows环境有其独特的配置方式和注意事项。在Windows Server上部署前后端分离项目主要涉及以下几个核心组件IIS作为Web服务器托管前端静态资源Java运行环境(JRE/JDK)运行后端服务数据库服务(MySQL/SQL Server等)可能的中间件如Redis、消息队列等2. 环境准备与基础配置2.1 Windows Server 2022初始配置新安装的Windows Server 2022默认是最小化安装我们需要先进行基础环境配置启用GUI管理工具如果安装的是Server Core版本Install-WindowsFeature Server-Gui-Mgmt-Infra,Server-Gui-Shell -Restart安装.NET Framework 4.8Install-WindowsFeature NET-Framework-45-Core配置系统防火墙New-NetFirewallRule -DisplayName Allow HTTP -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow New-NetFirewallRule -DisplayName Allow HTTPS -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow注意生产环境建议只开放必要的端口并根据实际需求调整防火墙规则。2.2 IIS安装与配置IIS是Windows平台最常用的Web服务器安装步骤如下通过服务器管理器添加角色和功能Install-WindowsFeature Web-Server,Web-ASP,Web-Asp-Net45,Web-Mgmt-Console -IncludeManagementTools安装URL重写模块用于前端路由 从Microsoft官网下载并安装URL Rewrite模块配置应用程序池为前端应用创建专用应用程序池设置.NET CLR版本为无托管代码将托管管道模式设为集成3. 前端项目部署3.1 Vue项目构建与发布在开发环境构建生产版本npm run build将生成的dist文件夹内容复制到服务器上的发布目录如C:\wwwroot\frontendIIS中配置网站物理路径指向发布目录绑定域名和端口设置默认文档为index.html配置URL重写规则解决前端路由404问题rule nameHandle History Mode stopProcessingtrue match url.* / conditions logicalGroupingMatchAll add input{REQUEST_FILENAME} matchTypeIsFile negatetrue / add input{REQUEST_FILENAME} matchTypeIsDirectory negatetrue / /conditions action typeRewrite url/ / /rule3.2 静态资源优化配置启用静态内容压缩Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpCompressionStatic配置客户端缓存 在web.config中添加staticContent clientCache cacheControlModeUseMaxAge cacheControlMaxAge30.00:00:00 / /staticContent4. 后端服务部署4.1 Java环境配置安装JDK 11推荐使用AdoptOpenJDKchoco install adoptopenjdk11 -y配置环境变量[Environment]::SetEnvironmentVariable(JAVA_HOME, C:\Program Files\AdoptOpenJDK\jdk-11.0.11.9-hotspot, Machine)验证安装java -version4.2 SpringBoot应用部署打包应用mvn clean package -DskipTests将生成的jar文件复制到服务器如C:\wwwroot\backend创建Windows服务推荐使用NSSMnssm install MyBackendService C:\Program Files\AdoptOpenJDK\jdk-11.0.11.9-hotspot\bin\java.exe -jar C:\wwwroot\backend\myapp.jar配置服务自动重启nssm set MyBackendService AppRestartDelay 50004.3 数据库连接配置在application.properties中配置生产环境数据库连接spring.datasource.urljdbc:sqlserver://localhost:1433;databaseNamemydb spring.datasource.usernamesa spring.datasource.passwordyourStrongPassword加密敏感配置推荐使用Jasyptspring.datasource.passwordENC(加密后的密码)5. 前后端联调与优化5.1 跨域问题解决后端配置CORSSpringBoot示例Configuration public class CorsConfig implements WebMvcConfigurer { Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(/api/**) .allowedOrigins(https://yourdomain.com) .allowedMethods(GET, POST, PUT, DELETE) .allowCredentials(true); } }前端axios配置axios.defaults.withCredentials true5.2 性能优化建议前端启用Gzip压缩使用CDN分发静态资源实现懒加载路由后端配置JVM参数Xms/Xmx启用响应压缩server.compression.enabledtrue数据库优化索引配置连接池参数spring.datasource.hikari.maximum-pool-size206. 监控与维护6.1 基础监控配置配置Windows性能监控关键指标CPU、内存、磁盘I/O、网络使用Performance Monitor创建数据收集器集应用健康检查SpringBoot Actuator配置management.endpoints.web.exposure.includehealth,info,metrics management.endpoint.health.show-detailsalways6.2 日志管理方案统一日志配置RollingFile nameFileAppender fileNamelogs/app.log filePatternlogs/app-%d{yyyy-MM-dd}-%i.log PatternLayout pattern%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n/ Policies TimeBasedTriggeringPolicy interval1 modulatetrue/ SizeBasedTriggeringPolicy size100 MB/ /Policies /RollingFile日志集中收集可选ELK方案安装Filebeat收集日志配置Logstash解析规则Kibana可视化展示7. 安全加固措施7.1 基础安全配置SSL证书配置申请正规CA证书在IIS中绑定HTTPS配置HTTP强制跳转HTTPS账户安全# 禁用默认Administrator账户 Rename-LocalUser -Name Administrator -NewName CustomAdmin # 配置密码策略 secedit /export /cfg secpolicy.inf # 修改文件后应用 secedit /configure /db secedit.sdb /cfg secpolicy.inf7.2 应用层安全SpringSecurity配置Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers(/api/public/**).permitAll() .anyRequest().authenticated() .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS); } }定期漏洞扫描使用Nessus等工具进行扫描关注Windows Update推送的安全补丁8. 自动化部署方案8.1 基础脚本自动化部署脚本示例deploy.ps1param( [string]$frontendPath, [string]$backendPath ) # 停止现有服务 Stop-Service -Name MyBackendService -ErrorAction SilentlyContinue # 清理旧文件 Remove-Item -Path C:\wwwroot\frontend\* -Recurse -Force Remove-Item -Path C:\wwwroot\backend\* -Recurse -Force # 复制新文件 Copy-Item -Path $frontendPath\* -Destination C:\wwwroot\frontend\ -Recurse Copy-Item -Path $backendPath\*.jar -Destination C:\wwwroot\backend\ # 重启服务 Start-Service -Name MyBackendService8.2 CI/CD集成可选Jenkins配置示例设置Windows节点配置Pipeline脚本pipeline { agent any stages { stage(Build Frontend) { steps { bat npm install bat npm run build } } stage(Build Backend) { steps { bat mvn clean package -DskipTests } } stage(Deploy) { steps { powershell C:\scripts\deploy.ps1 -frontendPath ./dist -backendPath ./target } } } }在实际部署过程中我发现Windows Server环境对文件路径大小写不敏感这可能导致从Linux开发环境迁移过来时出现资源加载问题。建议在开发阶段就统一使用小写文件名避免部署时出现问题。另外IIS的应用程序池回收设置需要根据实际负载调整默认设置可能不适合高并发场景。

本月热点