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

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

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

相关推荐

CentOS 7 搭建 Harbor2.4.1 Docker镜像仓库

上一篇文章我们使用了registry镜像来搭建Docker私有镜像仓库,但是使用体验不是很好,没有一个可管理的UI界面,管理很麻烦。本篇文章将介绍一个新的搭建Docker镜像仓库的工具叫做Har...

简单认识认识mqtt及mosquitto(mqtt报文解析)

某项目中使用了MQTT作为进程间的通信方式,之前没用过,这两篇笔记我们就来一起学习一下这种方式。MQTT的一些介绍以下介绍内容来自《[野火]《LwIP应用开发实战指南》MQTT协议全称是Messa...

全源码打造高性能 LNMP 架构: 实战教程(2025最新版)

适用场景:企业生产环境、自建Web服务、深度性能调优操作系统:CentOS7/8、RockyLinux、Debian、Ubuntu(本文以CentOSStream9为例)技术栈:N...

Nacos3.0重磅来袭!全面拥抱AI,单机及集群模式安装详细教程!

之前和大家分享过JDK17的多版本管理及详细安装过程,然后在项目升级完jdk17后又发现之前的注册和配置中心nacos又用不了,原因是之前的nacos1.3版本的,版本太老了,已经无法适配当前新的JD...

Ubuntu24.04.2 企业级MinIO存储系统部署指南

一、概要1.1MinIO架构解析MinIO是一款高性能的云原生对象存储系统,采用Golang开发并遵循ApacheLicensev2.0协议。其核心架构基于纠删码(ErasureCode)技...

从零打造自己的 国产鸿蒙(OpenHarmony)定制系统-完整可落地流程

适用版本:OpenHarmony4.0/5.0Standard目标人群:想在x86PC、RK3568开发板或自有硬件上裁剪、加品牌、预装应用并生成可刷机镜像的开发者/团队目录环境准...

一次暂未成功的dify安装经历(dify怎么安装)

前几天在阿里云买了一台机,这几天一直在尝试安装dify,到现在还没安装上我是按这个教程装的https://blog.csdn.net/2401_82469710/article/details/14...

ZLMediaKit教程(五)支持webrtc(webrtc lib)

ZLMediaKit系列文章(共六篇):ZLMediaKit流媒体(一)编译安装ZLMediaKit教程(二)主程序和配置文件解析ZLMediaKit教程(三)URL规则ZLMediaKit教程...

Linux程序安装与管理指南(linux程序安装命令大全)

在Linux系统中,安装和管理程序主要通过包管理器和手动编译安装两种主要方式实现。以下是详细的操作指南,涵盖常见发行版(如Ubuntu/Debian、CentOS/RHEL、Fedora等)的用法。一...

离线状态下安装 Nginx 各个模块?这篇攻略让你轻松搞定

你是不是也在为离线状态下安装Nginx各个模块而发愁?在互联网大厂后端开发工作中,我们常常会遇到一些特殊的网络环境,比如公司内部的离线服务器,或是处于隔离状态的测试环境。当需要在这些离线环境中安装...

Rust实践:Win10环境下的openssl交叉编译

Rust支持跨平台,可以指定生成目标平台,交叉编译也是支持的。当然,想要交叉编译成功,还需要指定平台的编译器(如:msvc、gcc等)。openssl是C语言开发的库,如果在Rust代码中用到open...

Linux下Blackwell架构显卡(RTX5070/5090)编译PaddlePaddle指南

Blackwell显卡架构如RTX5070\5090等显卡当前Paddle预编译版本中包含的GPU架构(即SM架构)是有限的,比如常见的SM75(T4)、SM86(A10)、SM89(...

突破操作系统界限,掌握Linux的必备指南

#头条创作挑战赛#简介Linux是一种开源的操作系统,它的核心思想是自由和开放。Linux以其稳定性、可靠性和安全性而闻名,被广泛用于服务器和嵌入式设备中。Linux创始人Linux安装在安装Linu...

Linux日常高频使用的100条命令,强烈建议收藏

查看系统信息如何查看系统版本:uname-alsb_release-acat/etc/os-release如何查看系统内核信息:uname-r如何查看系统CPU信息:lscpucat...

Linux文件系统结构全解析(linux文件结构详解)

对Linux新手而言,“一切皆文件”的设计哲学常让人既兴奋又困惑——打开终端输入ls/,看到的bin、etc、var等目录到底有什么用?如何快速定位关键文件?本文将从Linux文件系统的底层逻...

取消回复欢迎 发表评论: