用Ansible从零开始部署Spring Boot Web应用:全栈自动化部署指南
ztj100 2025-05-14 18:28 27 浏览 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的自动化部署,不仅提高了部署效率,还使得运维工作更加便捷、可重复。如果您有任何问题或改进建议,欢迎留言讨论。
相关推荐
- sharding-jdbc实现`分库分表`与`读写分离`
-
一、前言本文将基于以下环境整合...
- 三分钟了解mysql中主键、外键、非空、唯一、默认约束是什么
-
在数据库中,数据表是数据库中最重要、最基本的操作对象,是数据存储的基本单位。数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的。每一行代表一条唯一的记录,每一列代表记录中的一个域。...
- MySQL8行级锁_mysql如何加行级锁
-
MySQL8行级锁版本:8.0.34基本概念...
- mysql使用小技巧_mysql使用入门
-
1、MySQL中有许多很实用的函数,好好利用它们可以省去很多时间:group_concat()将取到的值用逗号连接,可以这么用:selectgroup_concat(distinctid)fr...
- MySQL/MariaDB中如何支持全部的Unicode?
-
永远不要在MySQL中使用utf8,并且始终使用utf8mb4。utf8mb4介绍MySQL/MariaDB中,utf8字符集并不是对Unicode的真正实现,即不是真正的UTF-8编码,因...
- 聊聊 MySQL Server 可执行注释,你懂了吗?
-
前言MySQLServer当前支持如下3种注释风格:...
- MySQL系列-源码编译安装(v5.7.34)
-
一、系统环境要求...
- MySQL的锁就锁住我啦!与腾讯大佬的技术交谈,是我小看它了
-
对酒当歌,人生几何!朝朝暮暮,唯有己脱。苦苦寻觅找工作之间,殊不知今日之事乃我心之痛,难道是我不配拥有工作嘛。自面试后他所谓的等待都过去一段时日,可惜在下京东上的小金库都要见低啦。每每想到不由心中一...
- MySQL字符问题_mysql中字符串的位置
-
中文写入乱码问题:我输入的中文编码是urf8的,建的库是urf8的,但是插入mysql总是乱码,一堆"???????????????????????"我用的是ibatis,终于找到原因了,我是这么解决...
- 深圳尚学堂:mysql基本sql语句大全(三)
-
数据开发-经典1.按姓氏笔画排序:Select*FromTableNameOrderByCustomerNameCollateChinese_PRC_Stroke_ci_as//从少...
- MySQL进行行级锁的?一会next-key锁,一会间隙锁,一会记录锁?
-
大家好,是不是很多人都对MySQL加行级锁的规则搞的迷迷糊糊,一会是next-key锁,一会是间隙锁,一会又是记录锁。坦白说,确实还挺复杂的,但是好在我找点了点规律,也知道如何如何用命令分析加...
- 一文讲清怎么利用Python Django实现Excel数据表的导入导出功能
-
摘要:Python作为一门简单易学且功能强大的编程语言,广受程序员、数据分析师和AI工程师的青睐。本文系统讲解了如何使用Python的Django框架结合openpyxl库实现Excel...
- 用DataX实现两个MySQL实例间的数据同步
-
DataXDataX使用Java实现。如果可以实现数据库实例之间准实时的...
- MySQL数据库知识_mysql数据库基础知识
-
MySQL是一种关系型数据库管理系统;那废话不多说,直接上自己以前学习整理文档:查看数据库命令:(1).查看存储过程状态:showprocedurestatus;(2).显示系统变量:show...
- 如何为MySQL中的JSON字段设置索引
-
背景MySQL在2015年中发布的5.7.8版本中首次引入了JSON数据类型。自此,它成了一种逃离严格列定义的方式,可以存储各种形状和大小的JSON文档,例如审计日志、配置信息、第三方数据包、用户自定...
你 发表评论:
欢迎- 一周热门
-
-
MySQL中这14个小玩意,让人眼前一亮!
-
旗舰机新标杆 OPPO Find X2系列正式发布 售价5499元起
-
【VueTorrent】一款吊炸天的qBittorrent主题,人人都可用
-
面试官:使用int类型做加减操作,是线程安全吗
-
C++编程知识:ToString()字符串转换你用正确了吗?
-
【Spring Boot】WebSocket 的 6 种集成方式
-
PyTorch 深度学习实战(26):多目标强化学习Multi-Objective RL
-
pytorch中的 scatter_()函数使用和详解
-
与 Java 17 相比,Java 21 究竟有多快?
-
基于TensorRT_LLM的大模型推理加速与OpenAI兼容服务优化
-
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)