用Ansible从零开始部署Spring Boot Web应用:全栈自动化部署指南
mhr18 2025-05-02 12:01 55 浏览 0 评论
在现代Web开发中,构建一个可靠的、可扩展的Web应用程序需要综合利用多种技术栈。本文将通过实际案例,详细讲解如何从零开始部署一个以Spring Boot为核心的Web应用程序,包括后端、数据库、缓存、中间件和前端的全栈配置。我们将利用Ansible来实现自动化部署,以简化繁琐的配置流程。
场景概述
该应用由以下组件组成:
- Spring Boot:核心后端服务,打包为一个可运行的JAR文件。
- MySQL:提供关系型数据库支持。
- Redis:作为缓存服务,加速数据访问。
- Vue.js:构建用户交互的前端界面。
- Nginx:反向代理和静态文件服务。
我们将假设目标服务器运行CentOS7,Ubuntu 20.04或类似的Linux发行版,并通过Ansible进行远程配置。
部署步骤
1. 准备服务器环境
首先,确保目标服务器已经安装了以下基本工具:
- OpenSSH:用于Ansible的远程连接。
- Python:Ansible需要Python支持。
在控制节点安装Ansible:
# CentOS 7 系统
yum install ansible -y
# Ubuntu 系统
sudo apt update && sudo apt install -y ansible
验证Ansible安装是否成功:
ansible --version
2. 规划目录结构
为了让项目部署更加清晰,我们规划以下目录结构:
├── ansible
│ ├── playbooks
│ │ ├── deploy.yml # 主Playbook文件
│ ├── inventory # 主机清单
│ ├── files # 上传的静态文件和配置
│ │ ├── springboot-app.jar
│ │ ├── nginx.conf
│ │ └── vue-dist/
将必要的文件上传到files目录中,包括Spring Boot的JAR包、Nginx配置文件,以及Vue的打包结果(通常是dist目录)。
3. 编写Playbook
接下来,我们将逐步编写Playbook,分步完成部署。
3.1 安装必需的服务
安装Java环境、MySQL、Redis和Nginx:
- name: Install Required Services
hosts: all
become: true
tasks:
- name: Install Java, MySQL, Redis, and Nginx (Ubuntu)
apt:
name:
- openjdk-11-jdk
- mysql-server
- redis-server
- nginx
state: present
update_cache: yes
when: ansible_os_family == 'Debian' # Ubuntu系统
- name: Install Java, MySQL, Redis, and Nginx (CentOS)
yum:
name:
- openjdk-11-jdk
- mysql-server
- redis-server
- nginx
state: present
when: ansible_os_family == 'RedHat' # CentOS系统
3.2 配置MySQL数据库
配置数据库和用户:
- name: Configure MySQL Database
mysql_db:
name: springboot_db
state: present
login_user: root
login_password: "your_mysql_root_password"
- name: Create MySQL User
mysql_user:
name: springboot_user
password: springboot_password
priv: 'springboot_db.*:ALL'
state: present
login_user: root
login_password: "your_mysql_root_password"
3.3 配置Redis
确保Redis服务正在运行:
- name: Ensure Redis is Running (Ubuntu)
service:
name: redis-server
state: started
enabled: true
when: ansible_os_family == 'Debian' # Ubuntu系统
- name: Ensure Redis is Running (CentOS)
service:
name: redis
state: started
enabled: true
when: ansible_os_family == 'RedHat' # CentOS系统
3.4 部署Spring Boot应用
现在我们可以上传Spring Boot应用的JAR包并配置systemd服务,使其能够自动启动。
- name: Upload Spring Boot Application
copy:
src: files/springboot-app.jar
dest: /opt/springboot-app/springboot-app.jar
owner: root
group: root
mode: '0755'
- name: Configure Spring Boot SystemD Service
copy:
dest: /etc/systemd/system/springboot-app.service
content: |
[Unit]
Description=Spring Boot Application
After=network.target
[Service]
User=root
ExecStart=/usr/bin/java -jar /opt/springboot-app/springboot-app.jar
Restart=always
[Install]
WantedBy=multi-user.target
- name: Reload SystemD and Start Spring Boot Service
command: systemctl daemon-reload
- name: Ensure Spring Boot Service is Running
service:
name: springboot-app
state: started
enabled: true
3.5 配置Nginx
我们将配置Nginx作为反向代理,并将前端Vue应用的静态文件服务到指定目录。
- name: Upload Vue Static Files
copy:
src: files/vue-dist/
dest: /var/www/html/vue-app/
owner: www-data
group: www-data
mode: '0755'
- name: Configure Nginx
copy:
src: files/nginx.conf
dest: /etc/nginx/sites-available/springboot-app
- name: Enable Nginx Configuration
file:
src: /etc/nginx/sites-available/springboot-app
dest: /etc/nginx/sites-enabled/springboot-app
state: link
- name: Remove Default Nginx Configuration
file:
path: /etc/nginx/sites-enabled/default
state: absent
- name: Restart Nginx
service:
name: nginx
state: restarted
3.6 完整Playbook
将所有任务整合到一个完整的Playbook中,最终形成deploy.yml,代码如下:
---
- name: Deploy Spring Boot Web Application
hosts: all
become: true
vars:
mysql_root_password: "{{ mysql_root_password }}" # MySQL root 密码
mysql_database: springboot_db # 数据库名称
mysql_user: springboot_user # 数据库用户
mysql_password: "{{ mysql_password }}" # 数据库用户密码
tasks:
# 安装服务(Java, MySQL, Redis, Nginx)
- name: Install Java, MySQL, Redis, and Nginx (Ubuntu)
apt:
name:
- openjdk-11-jdk
- mysql-server
- redis-server
- nginx
state: present
update_cache: yes
when: ansible_os_family == 'Debian' # Ubuntu系统
- name: Install Java, MySQL, Redis, and Nginx (CentOS)
yum:
name:
- java-11-openjdk
- mysql-server
- redis
- nginx
state: present
when: ansible_os_family == 'RedHat' # CentOS系统
# 配置MySQL数据库
- name: Configure MySQL Database
mysql_db:
name: "{{ mysql_database }}"
state: present
login_user: root
login_password: "{{ mysql_root_password }}"
- name: Create MySQL User
mysql_user:
name: "{{ mysql_user }}"
password: "{{ mysql_password }}"
priv: "{{ mysql_database }}.*:ALL"
state: present
login_user: root
login_password: "{{ mysql_root_password }}"
# 确保Redis服务正在运行
- name: Ensure Redis is Running (Ubuntu)
service:
name: redis-server
state: started
enabled: true
when: ansible_os_family == 'Debian' # Ubuntu系统
- name: Ensure Redis is Running (CentOS)
service:
name: redis
state: started
enabled: true
when: ansible_os_family == 'RedHat' # CentOS系统
# 上传Spring Boot应用JAR文件
- name: Upload Spring Boot Application
copy:
src: files/springboot-app.jar
dest: /opt/springboot-app/springboot-app.jar
owner: root
group: root
mode: '0755'
# 配置Spring Boot SystemD服务
- name: Configure Spring Boot SystemD Service
copy:
dest: /etc/systemd/system/springboot-app.service
content: |
[Unit]
Description=Spring Boot Application
After=network.target
[Service]
User=root
ExecStart=/usr/bin/java -jar /opt/springboot-app/springboot-app.jar
Restart=always
[Install]
WantedBy=multi-user.target
- name: Reload SystemD and Start Spring Boot Service
command: systemctl daemon-reload
- name: Ensure Spring Boot Service is Running
service:
name: springboot-app
state: started
enabled: true
# 上传Vue前端静态文件
- name: Upload Vue Static Files
copy:
src: files/vue-dist/
dest: /var/www/html/vue-app/
owner: www-data
group: www-data
mode: '0755'
# 配置Nginx
- name: Configure Nginx
copy:
src: files/nginx.conf
dest: /etc/nginx/sites-available/springboot-app
- name: Enable Nginx Configuration
file:
src: /etc/nginx/sites-available/springboot-app
dest: /etc/nginx/sites-enabled/springboot-app
state: link
- name: Remove Default Nginx Configuration
file:
path: /etc/nginx/sites-enabled/default
state: absent
- name: Restart Nginx
service:
name: nginx
state: restarted
通过以上步骤,您可以在CentOS和Ubuntu系统上自动化部署一个完整的Spring Boot Web应用,包括后端(Spring Boot)、数据库(MySQL)、缓存(Redis)、前端(Vue.js)以及反向代理(Nginx)。通过Ansible的自动化部署,不仅提高了部署效率,还使得运维工作更加便捷、可重复。如果您有任何问题或改进建议,欢迎留言讨论。
相关推荐
- Dubai's AI Boom Lures Global Tech as Emirate Reinvents Itself as Middle East's Silicon Gateway
-
AI-generatedimageAsianFin--Dubaiisrapidlytransformingitselffromadesertoilhubintoaglob...
- OpenAI Releases o3-pro, Cuts o3 Prices by 80% as Deal with Google Cloud Reported to Make for Compute Needs
-
TMTPOST--OpenAIisescalatingthepricewarinlargelanguagemodel(LLM)whileseekingpartnershi...
- 黄仁勋说AI Agent才是未来!但究竟有些啥影响?
-
,抓住风口(iOS用户请用电脑端打开小程序)本期要点:详解2025年大热点你好,我是王煜全,这里是王煜全要闻评论。最近,有个词被各个科技大佬反复提及——AIAgent,智能体。黄仁勋在CES展的发布...
- 商城微服务项目组件搭建(五)——Kafka、Tomcat等安装部署
-
1、本文属于mini商城系列文档的第0章,由于篇幅原因,这篇文章拆成了6部分,本文属于第5部分2、mini商城项目详细文档及代码见CSDN:https://blog.csdn.net/Eclipse_...
- Python+Appium环境搭建与自动化教程
-
以下是保姆级教程,手把手教你搭建Python+Appium环境并实现简单的APP自动化测试:一、环境搭建(Windows系统)1.安装Python访问Python官网下载最新版(建议...
- 零配置入门:用VSCode写Java代码的正确姿
-
一、环境准备:安装JDK,让电脑“听懂”Java目标:安装Java开发工具包(JDK),配置环境变量下载JDKJava程序需要JDK(JavaDevelopmentKit)才能运行和编译。以下是两...
- Mycat的搭建以及配置与启动(mycat2)
-
1、首先开启服务器相关端口firewall-cmd--permanent--add-port=9066/tcpfirewall-cmd--permanent--add-port=80...
- kubernetes 部署mysql应用(k8s mysql部署)
-
这边仅用于测试环境,一般生产环境mysql不建议使用容器部署。这里假设安装mysql版本为mysql8.0.33一、创建MySQL配置(ConfigMap)#mysql-config.yaml...
- Spring Data Jpa 介绍和详细入门案例搭建
-
1.SpringDataJPA的概念在介绍SpringDataJPA的时候,我们首先认识下Hibernate。Hibernate是数据访问解决技术的绝对霸主,使用O/R映射(Object-Re...
- 量子点格棋上线!“天衍”邀您执子入局
-
你是否能在策略上战胜量子智能?这不仅是一场博弈更是一次量子智力的较量——量子点格棋正式上线!试试你能否赢下这场量子智局!游戏玩法详解一笔一画间的策略博弈游戏目标:封闭格子、争夺领地点格棋的基本目标是利...
- 美国将与阿联酋合作建立海外最大的人工智能数据中心
-
当地时间5月15日,美国白宫宣布与阿联酋合作建立人工智能数据中心园区,据称这是美国以外最大的人工智能园区。阿布扎比政府支持的阿联酋公司G42及多家美国公司将在阿布扎比合作建造容量为5GW的数据中心,占...
- 盘后股价大涨近8%!甲骨文的业绩及指引超预期?
-
近期,美股的AI概念股迎来了一波上升行情,微软(MSFT.US)频创新高,英伟达(NVDA.US)、台积电(TSM.US)、博通(AVGO.US)、甲骨文(ORCL.US)等多股亦出现显著上涨。而从基...
- 甲骨文预计新财年云基础设施营收将涨超70%,盘后一度涨8% | 财报见闻
-
甲骨文(Oracle)周三盘后公布财报显示,该公司第四财季业绩超预期,虽然云基建略微逊于预期,但管理层预计2026财年云基础设施营收预计将增长超过70%,同时资本支出继上年猛增三倍后,新财年将继续增至...
- Springboot数据访问(整合MongoDB)
-
SpringBoot整合MongoDB基本概念MongoDB与我们之前熟知的关系型数据库(MySQL、Oracle)不同,MongoDB是一个文档数据库,它具有所需的可伸缩性和灵活性,以及所需的查询和...
- Linux环境下,Jmeter压力测试的搭建及报错解决方法
-
概述 Jmeter最早是为了测试Tomcat的前身JServ的执行效率而诞生的。到目前为止,它的最新版本是5.3,其测试能力也不再仅仅只局限于对于Web服务器的测试,而是涵盖了数据库、JM...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- Dubai's AI Boom Lures Global Tech as Emirate Reinvents Itself as Middle East's Silicon Gateway
- OpenAI Releases o3-pro, Cuts o3 Prices by 80% as Deal with Google Cloud Reported to Make for Compute Needs
- 黄仁勋说AI Agent才是未来!但究竟有些啥影响?
- 商城微服务项目组件搭建(五)——Kafka、Tomcat等安装部署
- Python+Appium环境搭建与自动化教程
- 零配置入门:用VSCode写Java代码的正确姿
- Mycat的搭建以及配置与启动(mycat2)
- kubernetes 部署mysql应用(k8s mysql部署)
- Spring Data Jpa 介绍和详细入门案例搭建
- 量子点格棋上线!“天衍”邀您执子入局
- 标签列表
-
- oracle位图索引 (74)
- oracle批量插入数据 (65)
- oracle事务隔离级别 (59)
- oracle 空为0 (51)
- oracle主从同步 (56)
- oracle 乐观锁 (53)
- redis 命令 (78)
- php redis (88)
- redis 存储 (66)
- redis 锁 (69)
- 启动 redis (66)
- redis 时间 (56)
- redis 删除 (67)
- redis内存 (57)
- redis并发 (52)
- redis 主从 (69)
- redis 订阅 (51)
- redis 登录 (54)
- redis 面试 (58)
- 阿里 redis (59)
- redis 搭建 (53)
- redis的缓存 (55)
- lua redis (58)
- redis 连接池 (61)
- redis 限流 (51)