相关软件链接:

一、初始化Linux运行springboot环境

1、安装yum下载上传插件sz(下载)、rz(上传)安装

yum install lrzsz -y

2、安装jdk11
下载好后上传解压

tar -zxvf jdk-11.0.12_linux-x64_bin.tar.gz 

vim /etc/profile

##添加环境变量
export JAVA_HOME=/opt/jdk-11.0.12
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export MAVEN_HOME=/opt/maven
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:/opt/node-v10.22.1-linux-x64/bin:/opt/node-v10.22.1-linux-x64/lib/node_modules/cnpm/bin

##刷新环境变量
source profile

java -version

2、安装nginx
Nginx安装配置菜鸟教程 runoob.com:https://www.runoob.com/linux/nginx-install-setup.html

##安装gcc依赖
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
##下载nginx安装包
cd /usr/local/src
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
##如果没有wget先下下载vget
yum -y install wget

wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

tar zxvf pcre-8.35.tar.gz

cd pcre-8.35

./configure

make && make install

pcre-config --version

cd /usr/local/src/

wget http://nginx.org/download/nginx-1.6.2.tar.gz

tar zxvf nginx-1.6.2.tar.gz

cd nginx-1.6.2

./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35

make

make install

/usr/local/src/

cd /usr/local/src/

cd nginx-1.6.2

make install

/usr/local/webserver/nginx/sbin/nginx -v

/usr/sbin/groupadd www 

/usr/sbin/useradd -g www www

/usr/local/webserver/nginx/sbin/nginx -t

/usr/local/webserver/nginx/sbin/nginx

firewall-cmd --zone=public --add-port=80/tcp --permanent 

firewall-cmd --reload

firewall-cmd --zone=public --add-port=8848/tcp --permanent 

firewall-cmd --reload

Nginx启动报错:

[root@10-36-230-236 logs]# nginx -s reload
nginx: [error] open() "/usr/local/webserver/nginx/logs/nginx.pid" failed (2: No such file or directory)

执行 nginx -c /usr/local/webserver/nginx/conf/nginx.conf

[root@10-36-230-236 logs]# nginx -c /usr/local/webserver/nginx/conf/nginx.conf
[root@10-36-230-236 logs]# ps -ef | grep nginx
root     13040  3785  0 09:26 pts/0    00:00:00 tail -f nginx.pid
root     25376     1  0 09:59 ?        00:00:00 nginx: master process nginx -c /usr/local/webserver/nginx/conf/nginx.conf
nobody   25377 25376  0 09:59 ?        00:00:00 nginx: worker process
root     25386  3785  0 09:59 pts/0    00:00:00 grep --color=auto nginx
[root@10-36-230-236 logs]# nginx -s reload
[root@10-36-230-236 logs]# ps -ef | grep nginx
root     13040  3785  0 09:26 pts/0    00:00:00 tail -f nginx.pid
root     25376     1  0 09:59 ?        00:00:00 nginx: master process nginx -c /usr/local/webserver/nginx/conf/nginx.conf
nobody   25427 25376  0 09:59 ?        00:00:00 nginx: worker process
root     25435  3785  0 09:59 pts/0    00:00:00 grep --color=auto nginx

nginx: invalid option: “-s relaod”

[root@ecs-wlzckj02 sbin]# ./nginx -s relaod
nginx: invalid option: "-s relaod"
[root@ecs-wlzckj02 sbin]# nginx -c /usr/local/webserver/nginx/conf/nginx.conf
-bash: nginx: command not found
##这个是在nginx的目录下,cd /usr/local/webserver/nginx/sbin/
[root@ecs-wlzckj02 sbin]# ./nginx -s relaod
nginx: invalid option: "-s relaod"
[root@ecs-wlzckj02 sbin]# ps -ef | grep nginx
root       822  1264  0 15:47 pts/0    00:00:00 grep --color=auto nginx
[root@ecs-wlzckj02 sbin]# ./nginx -s relaod
nginx: invalid option: "-s relaod"

3、安装node环境
下载安装包:wget https://nodejs.org/dist/v10.22.1/node-v10.22.1-linux-x64.tar.xz
解压:

tar -xvf node-v8.11.3-linux-x64.tar.xz
//注意: -xf 是解压 .tar.xz 包   而 -zxvf 是解压 .tar.gz包

环境变量上面已经配置好,直接刷新环境变量

source /etc/profile

4、linux安装maven

[root@node2 ~]# wget -c --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.8.3/binaries/apache-maven-3.8.3-bin.tar.gz
[root@node2 ~]# tar -zxvf apache-maven-3.8.3-bin.tar.gz
[root@node2 ~]# mv apache-maven-3.8.3 /usr/local/maven

配置环境变量:

[root@node2 ~]# vim /etc/profile
...
export JAVA_HOME=/usr/local/jdk1.8.0_231
export MAVEN_HOME=/usr/local/maven
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

使配置生效:

[root@node2 ~]# . /etc/profile

验证mvn:

[root@node2 ~]# mvn -v
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/maven
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_231/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.el7.x86_64", arch: "amd64", family: "unix"

配置阿里镜像源本地仓库地址

#在/usr/local下创建maven-repo 文件夹
cd /usr/local
mkdir maven-repo

#编写maven配置
vim /usr/local/maven/conf/settings.xml

#在 <settings>标签下添加下面配置
<localRepository>root/responsity</localRepository>

#编写maven配置
vim /usr/local/maven/conf/settings.xml

#在 <mirrors>标签下添加下面配置
<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

#保存退出

有条件的配置私服的配置:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>D:\repository</localRepository>


  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <proxies>

  </proxies>

  <servers>
    
	<server>
      <id>ncdg-nexus-release</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>ncdg-nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
	<server>
      <id>maven-public</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors>
      
	  
	 <mirror>
      <id>maven-public</id>
      <name>maven-public</name>
      <url>http://61.164.136.50:8186/repository/maven-public/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
	
  </mirrors>

  <profiles>
   
	<!--jdk配置-->
	<profile>
		  <id>jdk-11</id>
		  <activation>
			<activeByDefault>true</activeByDefault>
			<jdk>11</jdk>
		  </activation>

		  <properties>
			<maven.compiler.source>11</maven.compiler.source>
			<maven.compiler.target>11</maven.compiler.target>
			<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
		  </properties>
	</profile>


  </profiles>

  <activeProfiles>
     <!--需要激活 <profile>中的ID才生效--> 
	 <!--<activeProfile>ncdg-nexus-release</activeProfile>-->
     <activeProfile>jdk-11</activeProfile>
  </activeProfiles>


</settings>

二、docker安装

1、在线安装docker

#查看centos版本 在 CentOS 7安装docker要求系统为64位、系统内核版本为 3.10 以上,可以使用以下命令查看
[root@10-36-230-236 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 
#查看系统内核版本
[root@10-36-230-236 ~]# uname -r
3.10.0-957.el7.x86_64
#查看是否已安装docker列表
[root@10-36-230-236 ~]# yum list installed | grep docker
containerd.io.x86_64                 1.4.11-3.1.el7                 @docker-ce-stable
docker-ce.x86_64                     3:20.10.10-3.el7               @docker-ce-stable
docker-ce-cli.x86_64                 1:20.10.10-3.el7               @docker-ce-stable
docker-ce-rootless-extras.x86_64     20.10.10-3.el7                 @docker-ce-stable
docker-scan-plugin.x86_64            0.9.0-3.el7                    @docker-ce-stable
#清除已安装的docker
[root@10-36-230-236 ~]# sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
已加载插件:fastestmirror, langpacks
参数 docker 没有匹配
参数 docker-client 没有匹配
参数 docker-client-latest 没有匹配
参数 docker-common 没有匹配
参数 docker-latest 没有匹配
参数 docker-latest-logrotate 没有匹配
参数 docker-logrotate 没有匹配
参数 docker-engine 没有匹配
不删除任何软件包
#安装docker所需的依赖
#依赖
sudo yum install -y yum-utils   device-mapper-persistent-data   lvm2
#镜像源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#依赖
sudo yum install docker-ce docker-ce-cli containerd.io
#启动docker
sudo systemctl start docker
#查看docker的状态
systemctl status docker
#镜像加速
vim /etc/docker/daemon.json
{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
#重新启动服务:
 sudo systemctl daemon-reload
 sudo systemctl restart docker

三、docker安装数据库

1、docker安装mysql并初始化数据库

mysql挂载配置文件和数据目录

docker run --privileged=true --name mysql -d -it --restart=always --net=host -v /opt/mysql/data:/var/lib/mysql -v mysql.conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=test mysql --default-time-zone=+8:00 --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

–restart=always 代表开启启动

–privileged=true 代表进入容器内部为管理员身份

-d 表示后台运行容器 并返回容器Id

-v 把mysql产生的数据同步到本地 防止数据丢失

-e 容器传参 设置mysql的初始密码

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

mysql.conf : 标识符,直接挂载文件 -v /opt/mysql/conf:/etc/mysql/conf.d -v /opt/mysql/my.cnf:/etc/mysql/my.cnf会导致宿主机目录的覆盖掉容器目录,通过标识符将mysql容器目录的配置文件挂载出来。
通过volume查询标识文件所处的位置

[root@localhost mysql]# docker volume ls
DRIVER    VOLUME NAME
local     mysql.conf
local     mysql.data
[root@localhost mysql]# docker inspect mysql.conf
[
    {
        "CreatedAt": "2022-06-02T10:29:34+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/mysql.conf/_data",
        "Name": "mysql.conf",
        "Options": null,
        "Scope": "local"
    }
]
[root@localhost mysql]# cd /var/lib/docker/volumes/mysql.conf/_data

2、docker安装redis单机版

docker run --name redis -d -it --net=host -e TZ=Asia/Shanghai redis redis-server --requirepass 123456

docker操作redis

#进入容器
[root@yanglao-prod-1-223 setting]# docker exec -it redis /bin/bash
#连接redis
root@yanglao-prod-1-223:/data# redis-cli
#AUTH 用户名 密码
127.0.0.1:6379> AUTH abc123
OK
#查询key
127.0.0.1:6379> get setting:all:map::allMap
#删除key
127.0.0.1:6379> del setting:all:map::allMap
(integer) 1
127.0.0.1:6379> get setting:all:map::allMap
(nil)

3、docker安装消息队列

#下载镜像
docker pull webcenter/activemq

docker run --name activemq -d \
-p 8161:8161 \
-p 61616:61616 \
-p 61613:61613 \
webcenter/activemq

webcenter/activemq镜像启动没成功

#使用下面这个镜像成功
docker pull rmohr/activemq

docker run --name my-activemq -d \
-p 61616:61616 \
-p 8161:8161 rmohr/activemq

firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload

四、部署开源项目

以开源项目jeepay开源版为例:
https://docs.jeequan.com/docs/jeepay/deploy

1、将项目下载下来导入idea并修改配置:

修改pom文件添加:

<dependency>
         <groupId>javax.xml.bind</groupId>
         <artifactId>jaxb-api</artifactId>
</dependency>

linux不停机发布springboot项目_maven


修改三个文件中build配置

<!-- 作为可执行jar -->
    <build>
        <finalName>${project.artifactId}</finalName>

        <!-- resources资源配置项 -->
        <resources>
            <!-- 通用资源文件 -->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

            <!-- 放置通用配置yml文件, 开发时仅配置一套参数即可。   实际生产环境下应在每个项目下 与jar同级目录下新建application.yml覆写对应参数。  -->
            <resource>
<!--                <directory>../conf/devCommons</directory>-->
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>*.yml</include>
                </includes>
                <filtering>true</filtering>
<!--                <includes><include>**/*.yml</include></includes>-->
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

根据配置文件中选择的消息队列注释provide

linux不停机发布springboot项目_docker_02


修改三个配置文件application.yml

linux不停机发布springboot项目_maven_03


修改根目录的配置

linux不停机发布springboot项目_spring boot_04


修改三个配置文件的数据库配置、消息队列配置

linux不停机发布springboot项目_docker_05


linux不停机发布springboot项目_nginx_06


linux不停机发布springboot项目_nginx_07


linux不停机发布springboot项目_nginx_08

2、打包上传

mvn clean package -Dmaven.test.skip=true -Ptest

将jar包上传到目录

[root@yanglao-prod-1-223 opt]# ll /home/jeepay/service
total 0
drwxr-xr-x. 3 root root 81 Jun 10 16:29 manager
drwxr-xr-x. 4 root root 57 Jun 16 08:39 merchant
drwxr-xr-x. 4 root root 56 Jun 16 11:24 payment
[root@yanglao-prod-1-223 opt]#

启动容器

docker run --name jeepay-manager --net=host -it -d --restart=always -e TZ=Asia/Shanghai -v /home/jeepay/service:/home/jeepay/service -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime openjdk:11-jdk-oracle sh -c 'cd /home/jeepay/service/manager && java -Dfile.encoding=UTF-8 -jar jeepay-manager.jar --spring.profiles.active=prod'
docker run --name jeepay-payment --net=host -it -d --restart=always -e TZ=Asia/Shanghai -v /home/jeepay/service:/home/jeepay/service -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime openjdk:11-jdk-oracle sh -c 'cd /home/jeepay/service/payment && java -Dfile.encoding=UTF-8 -jar jeepay-payment.jar  --spring.profiles.active=prod'
docker run --name jeepay-merchant --net=host -it -d --restart=always -e TZ=Asia/Shanghai -v /home/jeepay/service:/home/jeepay/service -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime openjdk:11-jdk-oracle sh -c 'cd /home/jeepay/service/merchant && java -Dfile.encoding=UTF-8 -jar jeepay-merchant.jar  --spring.profiles.active=prod'```

## 3、**部署前端**
上传dist文件

```bash
[root@yanglao-prod-1-223 service]# ll /home/jeepay/html/
total 0
drwxr-xr-x. 6 root root 71 Jun 13 09:06 manager
drwxr-xr-x. 6 root root 71 Jun 13 11:11 merchant
[root@yanglao-prod-1-223 service]#

修改nginx配置:

linux不停机发布springboot项目_maven_09

[root@yanglao-prod-1-223 service]# cat /usr/local/webserver/nginx/conf/jeepay.conf 
server
{
    listen 19217;
    server_name localhost;
    index index.html index.htm ;
    root /home/jeepay/html/manager;

    #解决vue刷新404问题
    try_files $uri $uri/ /index.html;

    location /api/
    {
      
        proxy_set_header Host  $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:9217;
    }

}

server
{
    listen 19218;
    server_name localhost;
    index index.html index.htm;
    root /home/jeepay/html/merchant;

    #解决vue刷新404问题
    try_files $uri $uri/ /index.html;

    location /api/
    {
        
        proxy_set_header Host  $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:9218;
        # 启用支持websocket连接
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}
server
{
    listen 19216;
    server_name localhost;
    index index.html index.htm;
    root /home/jeepay/html/payment;

    location /
    {
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host  $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:9216;
    }

}