Jenkins deploys microservice projects

As a novice, he made an online education project of Gu Li in front of the video. After the test runs through, if you want to try to deploy it to your own cloud server, you are not idle. Closer to home

  • Introduction
    In the microservice architecture, with more and more services, the packaging and deployment of services will become a rather troublesome thing. For example, my edu-online project currently has 10 services that need to be deployed. Is there any way for us to deploy it once, and then click Execute to automatically deploy it? Of course there is! Below we use Jenkins to complete the automated deployment work in a microservice architecture.

  • Basic use of Jenkins
    For basic use of Jenkins, please refer to Using Jenkins to package and deploy SpringBoot applications with one click

  • Pre-preparation
    Upload your local project to the remote warehouse, install the git plug-in in the idea, and configure git. For the configuration of git, you can refer to the whole process of Git installation and configuration . Here I uploaded it to gitee.
    Upload the foreground of the project to gitee

git pull origin master --allow-unrelated-histories
git add .
git commit -m '提交说明'
git push origin master 
  • Server environment preparation
    1 Install JDK
    This article uses jdk-8u181-linux-x64.tar.gz;
    2 Install maven
    This article uses apache-maven-3.6.1-bin.tar.gz
    3 Install nacos
    This article uses service registration and does not want to remotely Connected, it's all on the server directly. Use nacos-server-1.4.2.tar.gz
    4 to install mysql
    as above, similar to the server not worth its life, anyway, it is about to expire. Mysql-5.7.36-linux-glibc2.12-x86_64.tar is used.
    5 After installing node
    and deploying the back-end code, arrange the front page by the way. Using node-v10.14.2-linux-x64.tar.gz
    6 Install redis
    This article uses redis-5.0.5.tar.gz. When I installed so many things, and then run it in the background, after I booted it up, I found out that the card was installed, and the CPU usage was so high that when I clicked on jenkins to deploy it, it took me a long time to wait.
    7 Install httpd
    Some linux versions already have Apache pre-installed, and it is already a service, so first check whether the httpd service already exists in the system. I don’t know why it doesn’t exist on my server anyway, so install it. For details, please refer to the installation, configuration, and service of Apache (httpd) in Linux
  • Installation and configuration of Jenkins
    Here I am directly lazy and save trouble, downloaded the war package and run it directly. War package download address: Download the war package and run it after the work is done. Start the war package command
nohup java -jar jenkins.war &

At this time, open the browser access address: http://ip:8080, you can access jenkins. The first time you log in, you need to enter the administrator password, then create a user, install a plug-in, create an instance, etc. Here I configure it for the video. After a series of jenkins installation and configuration
are just right, I came to this page. The first time I came in, there was nothing. I have already configured this.
insert image description here
There is a small problem I forgot to mention here, that is, during deployment, it may prompt that the root user is not recommended to deploy, and it involves permission issues or something. Then I said to add permission operations to the jenkins user, etc., I ignored him, and
directly uploaded them all to the root.

  • Start the deployment
    First create a task for the parent project. By default, there is no maven project. If you want to create maven directly, you can arrange a plug-in first, but you can also create a free-style one, just write the command yourself.
    After coming in, configure the warehouse address
    insert image description here
    because maven has been adjusted, it is also more convenient here, directly
    insert image description here
clean package -Dmaven.test.skip=true

Then after a long wait to download and package dependencies. You can check the console output for errors and make changes.
insert image description hereinsert image description here
I have been SUCCESS here.
Next, arrange the modules. The simplest method used here does not use the docker deployment in the video.

BUILD_ID=DONTKILLME
#!/bin/bash
#获取你想运行jar包的进程号
pid=`ps -ef | grep api_gateway-1.0-SNAPSHOT.jar | grep -v grep | awk '{print $2}'`
#如果存在则把该进程杀掉
if [ -n "$pid" ]
then
   echo "kill -9 的pid:" $pid
   kill -9 $pid
fi
echo "复制jar包"
#把jenkins打的jar包复制到自己指定的目录下
cp  /root/.jenkins/workspace/edu_parent/infrastructure/api_gateway/target/api_gateway-1.0-SNAPSHOT.jar /usr/mydemo
echo "启动jar包"
#最后启动jar包并把日志输出到指定的文件中以便查看
nohup java -jar /usr/mydemo/api_gateway-1.0-SNAPSHOT.jar > /usr/mydemo/api_gateway.log 2>&1 & 

Other modules are similar, and so on, create new tasks for all modules and build them. View through the log file
insert image description here
and finally deploy the foreground project

#!/bin/bash
pwd
ls

node -v
npm -v
git --version
java -version

echo '构建的版本号:'${
    
    BUILD_NUMBER}
npm install
npm run build
pwd

echo '----- 以上的列出的文件是 jenkin 服务 workspace 中 vue-admin-template-master 目录下的文件-------'

echo "复制dist文件夹"
#把dist文件夹复制到httpd服务器指定的目录下
cd /var/www/html/
#mkdir vue-admin-template-master
cp -r /root/.jenkins/workspace/vue-admin-template-master/dist/* /var/www/html/vue-admin-template-master/
echo "构建完成"

When the service is up, you can access it by visiting http://ip/project name.
insert image description here
Next, you can happily access the projects you deployed on the server.

  • Written at the end
    I am Xiaobai, who is about to graduate, and it is the first time to deploy the project to the server. This article is only for records. If there are any omissions or omissions, I hope you can criticize and correct me.
    Chinese New Year is approaching, and the epidemic situation in Anyang is also tense. We have been doing nucleic acid every day for 4 consecutive days. I wish you all a happy new year in advance. I hope everyone can return home safely and reunite with their families. , Wages are rising.

Guess you like

Origin blog.csdn.net/weixin_44834205/article/details/122458594