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

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

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

相关推荐

0722-6.2.0-如何在RedHat7.2使用rpm安装CDH(无CM)

文档编写目的在前面的文档中,介绍了在有CM和无CM两种情况下使用rpm方式安装CDH5.10.0,本文档将介绍如何在无CM的情况下使用rpm方式安装CDH6.2.0,与之前安装C5进行对比。环境介绍:...

ARM64 平台基于 openEuler + iSula 环境部署 Kubernetes

为什么要在arm64平台上部署Kubernetes,而且还是鲲鹏920的架构。说来话长。。。此处省略5000字。介绍下系统信息;o架构:鲲鹏920(Kunpeng920)oOS:ope...

生产环境starrocks 3.1存算一体集群部署

集群规划FE:节点主要负责元数据管理、客户端连接管理、查询计划和查询调度。>3节点。BE:节点负责数据存储和SQL执行。>3节点。CN:无存储功能能的BE。环境准备CPU检查JDK...

在CentOS上添加swap虚拟内存并设置优先级

现如今很多云服务器都会自己配置好虚拟内存,当然也有很多没有配置虚拟内存的,虚拟内存可以让我们的低配服务器使用更多的内存,可以减少很多硬件成本,比如我们运行很多服务的时候,内存常常会满,当配置了虚拟内存...

国产深度(deepin)操作系统优化指南

1.升级内核随着deepin版本的更新,会自动升级系统内核,但是我们依旧可以通过命令行手动升级内核,以获取更好的性能和更多的硬件支持。具体操作:-添加PPAs使用以下命令添加PPAs:```...

postgresql-15.4 多节点主从(读写分离)

1、下载软件[root@TX-CN-PostgreSQL01-252software]#wgethttps://ftp.postgresql.org/pub/source/v15.4/postg...

Docker 容器 Java 服务内存与 GC 优化实施方案

一、设置Docker容器内存限制(生产环境建议)1.查看宿主机可用内存bashfree-h#示例输出(假设宿主机剩余16GB可用内存)#Mem:64G...

虚拟内存设置、解决linux内存不够问题

虚拟内存设置(解决linux内存不够情况)背景介绍  Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级。所以,程序和数据如果在内存的话,会有非常快的读写速度。但是,内存...

Elasticsearch性能调优(5):服务器配置选择

在选择elasticsearch服务器时,要尽可能地选择与当前业务量相匹配的服务器。如果服务器配置太低,则意味着需要更多的节点来满足需求,一个集群的节点太多时会增加集群管理的成本。如果服务器配置太高,...

Es如何落地

一、配置准备节点类型CPU内存硬盘网络机器数操作系统data节点16C64G2000G本地SSD所有es同一可用区3(ecs)Centos7master节点2C8G200G云SSD所有es同一可用区...

针对Linux内存管理知识学习总结

现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的。对于内存部分需要知道:地址映射内存管理的方式缺页异常先来看一些基本的知识,在进程看来,内存分为内...

MySQL进阶之性能优化

概述MySQL的性能优化,包括了服务器硬件优化、操作系统的优化、MySQL数据库配置优化、数据库表设计的优化、SQL语句优化等5个方面的优化。在进行优化之前,需要先掌握性能分析的思路和方法,找出问题,...

Linux Cgroups(Control Groups)原理

LinuxCgroups(ControlGroups)是内核提供的资源分配、限制和监控机制,通过层级化进程分组实现资源的精细化控制。以下从核心原理、操作示例和版本演进三方面详细分析:一、核心原理与...

linux 常用性能优化参数及理解

1.优化内核相关参数配置文件/etc/sysctl.conf配置方法直接将参数添加进文件每条一行.sysctl-a可以查看默认配置sysctl-p执行并检测是否有错误例如设置错了参数:[roo...

如何在 Linux 中使用 Sysctl 命令?

sysctl是一个用于配置和查询Linux内核参数的命令行工具。它通过与/proc/sys虚拟文件系统交互,允许用户在运行时动态修改内核参数。这些参数控制着系统的各种行为,包括网络设置、文件...

取消回复欢迎 发表评论: