IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Linux:记录一次完整的后端项目部署过程 -> 正文阅读

[系统运维]Linux:记录一次完整的后端项目部署过程

一、背景

公司的CI/CD环境出现了故障,影响项目联调测试,于是协调运维给了台服务器,准备自己来玩

初步设想是通过脚本一键自动从git拉代码、maven自动打包、自动启动项目

二、服务器环境

已装好Centos、有外网

三、环境准备

先建好目录

cd /data
# 在/data目录下建一个project目录,用于存放项目相关文件
mkdir project
# 切换到project,再创建一个gitlab目录,用于存在git clone下载的代码
cd project
mkdir gitlab
1. 安装Java

参考网上资料,jdk要求建议1.8+

我安装的是 jdk-8u341-linux-x64.tar.gz,先把包下载下来,然后上传到服务器(我是在/usr/local目录下建了个java目录,然后把包上传到这个目录),然后执行如下命令解压

tar -xzvf jdk-8u341-linux-x64.tar.gz

然后配置一下环境变量

vim /etc/profile

在最后面加上

#java environment
export JAVA_HOME=/usr/local/java/jdk1.8.0_341
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

然后执行如下命令让环境变量生效

source /etc/profile

最后验证一下,输入:java -version,看到如下则代表成功

在这里插入图片描述

2. 安装Git

我是先移除了服务器自带的git

yum -y remove git

然后重新安装了下

yum -y install git

执行完检查下,看到如下则ok

在这里插入图片描述

3. 安装Maven

切换到/data/apps目录

先下载maven包

wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.8.2/binaries/apache-maven-3.8.2-bin.tar.gz

然后解压

tar -xzvf apache-maven-3.8.2-bin.tar.gz
cd apache-maven-3.8.2/
# 查看当前目录,复制下来
pwd

获取到目录
在这里插入图片描述
然后去配置maven的环境变量

也是先vim /etc/profile,在最后输入如下

#maven enviroment
export MAVEN_HOME=/data/apps/apache-maven-3.8.2
export PATH=${PATH}:${MAVEN_HOME}/bin

然后执行如下命令让环境变量生效

source /etc/profile

验证一下:mvn -v, 看到如下代表成功

在这里插入图片描述
/data目录下建一个目录,名为:mvnrepository 用于存放jar包

在这里插入图片描述

进入到:/data/apps/apache-maven-3.8.2/conf 目录,先将默认的settings.xml文件备份

mv settings.xml settings.xml.bak

然后新建一下包含公司私服的配置文件,取名:settings.xml,内容参考如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.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>-->
    
    <!-- 这里改成上一步建好的maven仓库地址-->
	<localRepository>/data/mvnrepository</localRepository>
	<!-- interactiveMode
     | This will determine whether maven prompts you when it needs input. If set to false,
     | maven will use a sensible default value, perhaps based on some other setting, for
     | the parameter in question.
     |
     | Default: true
    <interactiveMode>true</interactiveMode>-->
	<!-- offline
     | Determines whether maven should attempt to connect to the network when executing a build.
     | This will have an effect on artifact downloads, artifact deployment, and others.
     |
     | Default: false
    <offline>false</offline>-->
	<!-- pluginGroups
     | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
     | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
     | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
     |-->
	<pluginGroups>
		<!-- pluginGroup
         | Specifies a further group identifier to use for plugin lookup.
        <pluginGroup>com.your.plugins</pluginGroup>-->
	</pluginGroups>
	<!-- proxies
     | This is a list of proxies which can be used on this machine to connect to the network.
     | Unless otherwise specified (by system property or command-line switch), the first proxy
     | specification in this list marked as active will be used.
     |-->
	<proxies>
		<!-- proxy
         | Specification for one proxy, to be used in connecting to the network.
         |
        <proxy><id>optional</id><active>true</active><protocol>http</protocol><username>proxyuser</username><password>proxypass</password><host>proxy.host.net</host><port>80</port><nonProxyHosts>local.net|some.host.com</nonProxyHosts></proxy>-->
	</proxies>
	<!-- servers
     | This is a list of authentication profiles, keyed by the server-id used within the system.
     | Authentication profiles can be used whenever maven must make a connection to a remote server.
     |-->
	<servers>
		<!-- server
         | Specifies the authentication information to use when connecting to a particular server, identified by
         | a unique name within the system (referred to by the 'id' attribute below).
         |
         | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
         |       used together.
         |
        <server><id>deploymentRepo</id><username>repouser</username><password>repopwd</password></server>-->
		<!-- Another sample, using keys to authenticate.
        <server><id>siteServer</id><privateKey>/path/to/private/key</privateKey><passphrase>optional; leave empty if not used.</passphrase></server>-->
		<server>
			<id>nexus</id>
			<!-- 私服用户名 -->
			<username>admin</username>
			<!-- 私服密码 -->
			<password>xxxx</password>
		</server>
		<server>
			<id>nexus-snapshots</id>
			<!-- 私服用户名 -->
			<username>admin</username>
			<!-- 私服密码 -->
			<password>xxxx</password>
		</server>
	</servers>
	<!-- mirrors
     | This is a list of mirrors to be used in downloading artifacts from remote repositories.
     |
     | It works like this: a POM may declare a repository to use in resolving certain artifacts.
     | However, this repository may have problems with heavy traffic at times, so people have mirrored
     | it to several places.
     |
     | That repository definition will have a unique id, so we can create a mirror reference for that
     | repository, to be used as an alternate download site. The mirror site will be the preferred
     | server for that repository.
     |-->
	<mirrors>
		<!-- mirror
         | Specifies a repository mirror site to use instead of a given repository. The repository that
         | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
         | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
         |
        <mirror><id>mirrorId</id><mirrorOf>repositoryId</mirrorOf><name>Human Readable Name for this Mirror.</name><url>http://my.repository.com/repo/path</url></mirror>-->
		<mirror>
			<!-- 阿里云仓库 -->
			<id>aliyun</id>
			<name>aliyun maven</name>
			<mirrorOf>central,!com.e-iceblue,!oss-snapshot,!nexus-agile-bpm,!release,!alfresco,!spring,!elastic.co
            </mirrorOf>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		</mirror>
		<mirror>
			<!--公司私有镜像 -->
			<id>self-public</id>
			<name>nexus maven</name>
			<mirrorOf>*,!com.e-iceblue,!oss-snapshot,!nexus-agile-bpm,!release,!alfresco,!spring,!elastic.co
            </mirrorOf>
			<url>http://xxx.xxx.xxx.xxx:8081/repository/maven-public/</url>
		</mirror>
	</mirrors>
	<!-- profiles
     | This is a list of profiles which can be activated in a variety of ways, and which can modify
     | the build process. Profiles provided in the settings.xml are intended to provide local machine-
     | specific paths and repository locations which allow the build to work in the local environment.
     |
     | For example, if you have an integration testing plugin - like cactus - that needs to know where
     | your Tomcat instance is installed, you can provide a variable here such that the variable is
     | dereferenced during the build process to configure the cactus plugin.
     |
     | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
     | section of this document (settings.xml) - will be discussed later. Another way essentially
     | relies on the detection of a system property, either matching a particular value for the property,
     | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
     | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
     | Finally, the list of active profiles can be specified directly from the command line.
     |
     | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
     |       repositories, plugin repositories, and free-form properties to be used as configuration
     |       variables for plugins in the POM.
     |
     |-->
	<profiles>
		<profile>
			<id>alibaba</id>
			<properties>
				<maven.compiler.source>1.8</maven.compiler.source>
				<maven.compiler.target>1.8</maven.compiler.target>
				<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
			</properties>
			<repositories>
				<repository>
					<id>nexus</id>
					<name>nexus</name>
					<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>nexus</id>
					<name>nexus</name>
					<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
		<profile>
			<id>jdk18</id>
			<activation>
				<activeByDefault>true</activeByDefault>
				<jdk>1.8</jdk>
			</activation>
			<properties>
				<maven.compiler.source>1.8</maven.compiler.source>
				<maven.compiler.target>1.8</maven.compiler.target>
				<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
			</properties>
		</profile>
		<profile>
			<id>nexus</id>
			<repositories>
				<repository>
					<id>central</id>
					<url>http://central</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>central</id>
					<url>http://central</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
		<profile>
			<id>self</id>
			<properties>
				<maven.compiler.source>1.8</maven.compiler.source>
				<maven.compiler.target>1.8</maven.compiler.target>
				<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
			</properties>
			<repositories>
				
				<repository>
					<id>nexus</id>
					<name>nexus</name>
					<url>http://xxx.xxx.xxx.xxx:8081/repository/maven-releases/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>nexus</id>
					<name>nexus</name>
					<url>http://xxx.xxx.xxx.xxx:8081/repository/maven-releases/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
	</profiles>
	<!-- activeProfiles
     | List of profiles that are active for all builds.
     |
    <activeProfiles><activeProfile>alwaysActiveProfile</activeProfile><activeProfile>anotherAlwaysActiveProfile</activeProfile></activeProfiles>-->
	<activeProfiles>
		<activeProfile>alibaba</activeProfile>
		<activeProfile>self</activeProfile>
		<activeProfile>nexus</activeProfile>
	</activeProfiles>
</settings>
四、具体实施
1. 先把Git上的代码拉到服务器目录

切换到/data/project/gitlab目录,执行如下命令,把相关的几个项目拉到本地

cd /data/project/gitlab/
git clone http://xxx.xxx.xxx/hcloud-cloud-plus.git
git clone http://xxx.xxx.xxx/hcloud-commons-plus.git

切换到目录,看一下代码有没有拉下来

在这里插入图片描述

2. 尝试编译

切换到其中一个项目目录

cd /hcloud-cloud-plus

在这里插入图片描述
然后执行如下maven命令测试

/data/apps/apache-maven-3.8.2/bin/mvn -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -f pom.xml clean package

在这里插入图片描述
项目build成功,证明拉代码和打包这一步走通了,接下来就是写项目启动脚本

3. 编写脚本

/data/project目录下新建一个名为:start.sh的脚本,内容如下:

start()方法里的内容要根据自己项目的实际情况调整

#!/bin/bash

# 操作符:start|stop|restart|status
ACTION=$1
# 模块名:eg hcloud-epidemic
MODULER=$2
# 代码分支
BRANCH=$3

# 如果操作符为空则予以提示
if [ "$ACTION" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {start|stop|restart|status} \033[0m"
    exit 1
fi

# 判断有没有传模块名、没传予以提示
if [ "$MODULER" = "" ];
then
    echo -e "\033[0;31m 未输入应用名 \033[0m"
    exit 1
fi

# 判断有没有传分支,没有传给一个默认分支
if [ "$BRANCH" = "" ];
then
    BRANCH="feature-220915"
fi

JAVA_OPT="-Xms256M -Xmx1024M -Xss512k -XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=128M -XX:+UseG1GC"
# JAVA_OPT=$JAVA_OPT" -Dspring.profiles.active=$PROFILES"
echo $JAVA_OPT

# 启动(重点关注这个里面的内容)
function start()
{
	count=`ps -ef | grep java | grep $MODULER | grep -v grep | wc -l`
	if [ $count != 0 ];then
		echo "$MODULER is running..."
	else
		# 1、先切换到项目代码目录
		cd gitlab/hcloud-cloud-plus/$MODULER/
		echo "开始拉取模块:【$MODULER】的git代码,分支为:【$BRANCH】..."
		# 2、拉取指定(或者默认)分支的代码
		git pull origin $BRANCH
		echo "开始maven打包..."
		# 3、指定配置开始打包
		/data/apps/apache-maven-3.8.2/bin/mvn -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -f pom.xml clean package
		echo "开始启动$MODULER-server..."
		# 4、启动项目
		nohup java -jar $JAVA_OPT /data/project/gitlab/hcloud-cloud-plus/$MODULER/$MODULER-server/target/$MODULER-server.jar > /data/project/$MODULER.log 2>&1 &
		echo "模块$MODULER-server启动完成..."
		# 打印完成时间
		echo $(date +%F%n%T)
	fi
}

# 停止
function stop()
{
	echo "Stop $MODULER"
	pid=`ps -ef | grep java | grep $MODULER-server | grep -v grep | awk '{print $2}'`
	count=`ps -ef | grep java | grep $MODULER | grep -v grep | wc -l`

	if [ $count != 0 ];then
	    kill $pid
    	count=`ps -ef | grep java | grep $MODULER-server | grep -v grep | wc -l`

      pid=`ps -ef | grep java | grep $MODULER-server | grep -v grep | awk '{print $2}'`
      kill -9 $pid

      echo "Stop $MODULER Success"
	fi
}

# 重启
function restart()
{
	stop
	sleep 2
	start
}

# 查看运行状态
function status()
{
    count=`ps -ef | grep java | grep $MODULER-server | grep -v grep | wc -l`
    if [ $count != 0 ];then
        echo "$MODULER is running..."
    else
        echo "$MODULER is not running..."
    fi
}

case $ACTION in
	start)
	start;;
	stop)
	stop;;
	restart)
	restart;;
	status)
	status;;
	*)

	echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {start|stop|restart|status}  {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
	  \033[0;33m sh  $0  start esmart-test.jar \033[0m"
esac

给大家看下我们其中一个项目的结构,然后应该能明白我为什么在start()方法里要这样写

在这里插入图片描述

其实用到的就是hcloud-epidemic-server这个module打好的jar包

4. 测试脚本

回到/data/project目录,执行start.sh脚本

在这里插入图片描述

hcloud-authority.log这几个以.log结尾的文件是脚本启动后生成的,我是后截的图,所以有这三个

执行脚本

./start.sh start hcloud-epidemic

在这里插入图片描述
在这里插入图片描述
在/data/project下生成了一个hcloud-epidemic.log日志文件,可以打开看一下日志

# 查看近1000行,及后续的滚动日志
tail -1000f hcloud-epidemic.log

项目启动成功

在这里插入图片描述
不想看日志,直接ctrl + c结束掉就行(不会影响程序在后台执行)

这个脚本还支持停止、重启、查看运行状态等操作,写个脚本,放到开机欢迎屏,方便其它开发知道咋使用

编辑:/etc/motd文件

vim /etc/motd

输入如下内容:

*******************xxx项目测试服务器********************************
前端:前端打好的包上传解压到:/usr/local/nginx/html/ 目录下即可
后端:项目目录:/data/project/
后端启动方式:(自动拉取git以及maven打包,后端只须提交代码到对应的分支即可)
启动:./start.sh start hcloud-epidemic
带项目分支:./start.sh start hcloud-epidemic dev (不带分支默认使用的feature-220915分支)
重启:./start.sh restart hcloud-epidemic 
查看状态:./start.sh status hcloud-epidemic 
停止:./start.sh stop hcloud-epidemic 
hcloud-authority 和 hcloud-oauth的启动方法同上
********************xxx项目测试服务器*********************************

然后重新打开一下shell窗口,可以看到项目说明

在这里插入图片描述

至此一个简单的CI/CD完成,算是给开发节省了一点工作量吧

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-09-24 21:30:38  更:2022-09-24 21:32:25 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/28 18:33:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计