百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

用Ansible从零开始部署Spring Boot Web应用:全栈自动化部署指南

ztj100 2025-05-14 18:28 20 浏览 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:

Bash
# CentOS 7 系统
yum install ansible -y

# Ubuntu 系统
sudo apt update && sudo apt install -y ansible

验证Ansible安装是否成功:

Bash
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的自动化部署,不仅提高了部署效率,还使得运维工作更加便捷、可重复。如果您有任何问题或改进建议,欢迎留言讨论。

相关推荐

30天学会Python编程:16. Python常用标准库使用教程

16.1collections模块16.1.1高级数据结构16.1.2示例...

强烈推荐!Python 这个宝藏库 re 正则匹配

Python的re模块(RegularExpression正则表达式)提供各种正则表达式的匹配操作。...

Python爬虫中正则表达式的用法,只讲如何应用,不讲原理

Python爬虫:正则的用法(非原理)。大家好,这节课给大家讲正则的实际用法,不讲原理,通俗易懂的讲如何用正则抓取内容。·导入re库,这里是需要从html这段字符串中提取出中间的那几个文字。实例一个对...

Python数据分析实战-正则提取文本的URL网址和邮箱(源码和效果)

实现功能:Python数据分析实战-利用正则表达式提取文本中的URL网址和邮箱...

python爬虫教程之爬取当当网 Top 500 本五星好评书籍

我们使用requests和re来写一个爬虫作为一个爱看书的你(说的跟真的似的)怎么能发现好书呢?所以我们爬取当当网的前500本好五星评书籍怎么样?ok接下来就是学习python的正确姿...

深入理解re模块:Python中的正则表达式神器解析

在Python中,"re"是一个强大的模块,用于处理正则表达式(regularexpressions)。正则表达式是一种强大的文本模式匹配工具,用于在字符串中查找、替换或提取特定模式...

如何使用正则表达式和 Python 匹配不以模式开头的字符串

需要在Python中使用正则表达式来匹配不以给定模式开头的字符串吗?如果是这样,你可以使用下面的语法来查找所有的字符串,除了那些不以https开始的字符串。r"^(?!https).*&...

先Mark后用!8分钟读懂 Python 性能优化

从本文总结了Python开发时,遇到的性能优化问题的定位和解决。概述:性能优化的原则——优化需要优化的部分。性能优化的一般步骤:首先,让你的程序跑起来结果一切正常。然后,运行这个结果正常的代码,看看它...

Python“三步”即可爬取,毋庸置疑

声明:本实例仅供学习,切忌遵守robots协议,请不要使用多线程等方式频繁访问网站。#第一步导入模块importreimportrequests#第二步获取你想爬取的网页地址,发送请求,获取网页内...

简单学Python——re库(正则表达式)2(split、findall、和sub)

1、split():分割字符串,返回列表语法:re.split('分隔符','目标字符串')例如:importrere.split(',','...

Lavazza拉瓦萨再度牵手上海大师赛

阅读此文前,麻烦您点击一下“关注”,方便您进行讨论和分享。Lavazza拉瓦萨再度牵手上海大师赛标题:2024上海大师赛:网球与咖啡的浪漫邂逅在2024年的上海劳力士大师赛上,拉瓦萨咖啡再次成为官...

ArkUI-X构建Android平台AAR及使用

本教程主要讲述如何利用ArkUI-XSDK完成AndroidAAR开发,实现基于ArkTS的声明式开发范式在android平台显示。包括:1.跨平台Library工程开发介绍...

Deepseek写歌详细教程(怎样用deepseek写歌功能)

以下为结合DeepSeek及相关工具实现AI写歌的详细教程,涵盖作词、作曲、演唱全流程:一、核心流程三步法1.AI生成歌词-打开DeepSeek(网页/APP/API),使用结构化提示词生成歌词:...

“AI说唱解说影视”走红,“零基础入行”靠谱吗?本报记者实测

“手里翻找冻鱼,精心的布局;老漠却不言语,脸上带笑意……”《狂飙》剧情被写成歌词,再配上“科目三”背景音乐的演唱,这段1分钟30秒的视频受到了无数网友的点赞。最近一段时间随着AI技术的发展,说唱解说影...

AI音乐制作神器揭秘!3款工具让你秒变高手

在音乐创作的领域里,每个人都有一颗想要成为大师的心。但是面对复杂的乐理知识和繁复的制作过程,许多人的热情被一点点消磨。...

取消回复欢迎 发表评论: