tomcat部署,jenkins部署使用

文章目录

      • 1.tomcat简介
        • 1.1.java环境安装
        • 1.2.tomcat项目部署
      • 3.jenkins简介
        • 3.1部署jenkins
        • 3.2使用jenkins部署项目
      • 4.使用jenkins
      • 5.程序打包的工具

1.tomcat简介

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
诀窍是,当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。目前Tomcat最新版本为9.0。

tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中。而tomcat本身的运行也依赖于jdk环境。

tomcat应用场景:lnmt

1.1.java环境安装
//安装jdk环境
[root@localhost yum.repos.d]# dnf -y install java17-openjdk*
//省略安装过程
//查看安装的版本
[root@localhost ~]# java -version
1.2.tomcat项目部署

下载地址https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz

//下载软件包
[root@localhost ~]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
//解压
[root@localhost ~]# ls
anaconda-ks.cfg  apache-tomcat-9.0.65.tar.gz
[root@localhost ~]# tar xf apache-tomcat-9.0.65.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-9.0.65  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@localhost local]# mv apache-tomcat-9.0.65/ tomcat
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src  tomcat
[root@localhost local]# cd tomcat/
[root@localhost tomcat]# ls
BUILDING.txt     LICENSE  README.md      RUNNING.txt  conf  logs  webapps
CONTRIBUTING.md  NOTICE   RELEASE-NOTES  bin          lib   temp  work
//webapps  放应用,java程序
//temp   放临时文件,一段时间自动删除
//logs    日志
[root@localhost tomcat]# 

//创建测试文件
[root@localhost tomcat]# cd webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test
//添加网页文件
[root@localhost test]# vim index.jsp
[root@localhost test]# cat index.jsp 
<html>
<head>
<title>runtime</title>
</head>
<body>
    <%
        out.println("hello world");
    %>
</body>
</html>
//企业尽量不用环境变量,有可能会有多个tomcat,启动一个另一个就不好找到,
[root@localhost test]# echo 'export PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh   
[root@localhost test]# source /etc/profile.d/tomcat.sh
[root@localhost test]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
[root@localhost test]# ss -antl
State        Recv-Q       Send-Q                   Local Address:Port               Peer Address:Port       Process       
LISTEN       0            128                            0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0            128                            0.0.0.0:111                     0.0.0.0:*                        
LISTEN       0            32                       192.168.122.1:53                      0.0.0.0:*                        
LISTEN       0            128                               [::]:22                         [::]:*                        
LISTEN       0            1                   [::ffff:127.0.0.1]:8005    //出现端口                      *:*                        
LISTEN       0            128                               [::]:111                        [::]:*                        
LISTEN       0            100                                  *:8080  //首页端口                        *:*                        
//关闭服务器和seliunx
[root@localhost test]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost test]# vim /etc/selinux/config 
[root@localhost test]# setenforce 0

在网页可以访问
tomcat部署,jenkins部署使用_第1张图片
刚刚添加的java文件
tomcat部署,jenkins部署使用_第2张图片

[root@localhost tomcat]# ls bin/
bootstrap.jar       ciphers.sh                    daemon.sh     setclasspath.bat  startup.sh            version.bat
catalina-tasks.xml  commons-daemon-native.tar.gz  digest.bat    setclasspath.sh   tomcat-juli.jar       version.sh
catalina.bat        commons-daemon.jar            digest.sh     shutdown.bat      tomcat-native.tar.gz
catalina.sh         configtest.bat                makebase.bat  shutdown.sh       tool-wrapper.bat
ciphers.bat         configtest.sh                 makebase.sh   startup.bat       tool-wrapper.sh
[root@localhost tomcat]# pwd
/usr/local/tomcat
//catalina.sh     总控开关,开启或关闭
//catalina.bat     在windows,总控开关,开启或关闭
//commons-daemon-native.tar.gz     守护进程
//configtest.bat        配置文件检查
//shutdown.sh       关机
//shutdown.sh    关闭服务
//startup.sh    启动服务
//version.sh     查看版本信息
#企业开机关机用法,多个tomcat情况,不添加环境变量,在bin下开启或关机
[root@localhost bin]# ./configtest.sh stop
//省略过程
[root@localhost bin]# ./configtest.sh start
//省略过程
  • 设置配置文件
[root@localhost ~]# cd /usr/local/tomcat/conf/
[root@localhost conf]# ls
Catalina         catalina.properties  jaspic-providers.xml  logging.properties  tomcat-users.xml  web.xml
catalina.policy  context.xml          jaspic-providers.xsd  server.xml          tomcat-users.xsd
[root@localhost conf]# vim server.xml 

//取消注释,用于tomcat跟apache的效率 ,开启AJP协议
//注意如果有多个tomcat,则改一下端口号,比如8009变8010,没用过的端口都行,文件里的端口全都要变,有一些端口要重复注意填写.单个tomcat不用
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443" />
 //重启
[root@localhost conf]# catalina.sh stop
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@localhost conf]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
[root@localhost conf]# ss -antl
State        Recv-Q       Send-Q                   Local Address:Port               Peer Address:Port       Process       
LISTEN       0            128                            0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0            128                            0.0.0.0:111                     0.0.0.0:*                        
LISTEN       0            32                       192.168.122.1:53                      0.0.0.0:*                        
LISTEN       0            128                               [::]:22                         [::]:*                        
LISTEN       0            1                   [::ffff:127.0.0.1]:8005     //出现                     *:*                        
LISTEN       0            100                              [::1]:8009      //出现                [::]:*                        
LISTEN       0            128                               [::]:111                        [::]:*                        
LISTEN       0            100                                  *:8080      //出现                    *:*                    

tomcat部署,jenkins部署使用_第3张图片

tomcat部署,jenkins部署使用_第4张图片
tomcat部署,jenkins部署使用_第5张图片

  • 添加用户
[root@localhost ~]# vim /usr/local/tomcat/conf/tomcat-users.xml   //此文件内添加下面两行
<role rolename="manager-gui"/>     //添加
<user username="tomcat" password="tomcat" roles="manager-gui"/>    //添加,改密码,用户,类型
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="" roles="tomcat"/>
  <user username="both" password="" roles="tomcat,role1"/>

//重启
[root@localhost ~]# catalina.sh stop
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@localhost ~]# ss -antl
State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
LISTEN        0             128                        0.0.0.0:111                     0.0.0.0:*                          
LISTEN        0             32                   192.168.122.1:53                      0.0.0.0:*                          
LISTEN        0             128                           [::]:22                         [::]:*                          
LISTEN        0             128                           [::]:111                        [::]:*                          
[root@localhost ~]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
[root@localhost ~]# ss -antl
State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
LISTEN        0             128                        0.0.0.0:111                     0.0.0.0:*                          
LISTEN        0             32                   192.168.122.1:53                      0.0.0.0:*                          
LISTEN        0             128                           [::]:22                         [::]:*                          
LISTEN        0             100                          [::1]:8009                       [::]:*                          
LISTEN        0             128                           [::]:111                        [::]:*                          
LISTEN        0             100                              *:8080                          *:*                          
[root@localhost ~]# ss -antl
State        Recv-Q       Send-Q                   Local Address:Port               Peer Address:Port       Process       
LISTEN       0            128                            0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0            128                            0.0.0.0:111                     0.0.0.0:*                        
LISTEN       0            32                       192.168.122.1:53                      0.0.0.0:*                        
LISTEN       0            128                               [::]:22                         [::]:*                        
LISTEN       0            1                   [::ffff:127.0.0.1]:8005                          *:*                        
LISTEN       0            100                              [::1]:8009                       [::]:*                        
LISTEN       0            128                               [::]:111                        [::]:*                        
LISTEN       0            100                                  *:8080                          *:*             
[root@localhost webapps]# ls
ROOT  docs  examples  host-manager  manager  test
[root@localhost webapps]# cd manager/     //状态里面,在项目里面改配置
[root@localhost manager]# pwd
/usr/local/tomcat/webapps/manager
[root@localhost manager]# vim META-INF/context.xml 

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
          allow="192\.168\.70\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />   //更改成这样,改自己主机的网段
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
//程序里面的类容改了,不用重启
//conf目录的必须要重启

刷新了就可以登录
tomcat部署,jenkins部署使用_第6张图片
停止项目
tomcat部署,jenkins部署使用_第7张图片
这些都是/usr/local/tomcat/webapps/manager目录的类容
tomcat部署,jenkins部署使用_第8张图片
tomcat部署,jenkins部署使用_第9张图片
tomcat部署,jenkins部署使用_第10张图片
//打不开,添加角色
tomcat部署,jenkins部署使用_第11张图片

[root@localhost ~]# vim /usr/local/tomcat/conf/tomcat-users.xml 

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>    //添加角色
<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>  //改成这样
<!--



//重启
[root@localhost ~]# catalina.sh stop
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@localhost ~]# ss -antl
State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
LISTEN        0             128                        0.0.0.0:111                     0.0.0.0:*                          
LISTEN        0             32                   192.168.122.1:53                      0.0.0.0:*                          
LISTEN        0             128                           [::]:22                         [::]:*                          
LISTEN        0             128                           [::]:111                        [::]:*                          
[root@localhost ~]# ps -ef | grep tomcat
root      511251  229197  0 08:15 pts/2    00:00:00 grep --color=auto tomcat
[root@localhost ~]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
[root@localhost ~]# ps -ef | grep tomcat
root      511553       1 69 08:15 pts/2    00:00:04 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root      511810  229197  0 08:15 pts/2    00:00:00 grep --color=auto tomcat
[root@localhost ~]# ss -antl
State        Recv-Q       Send-Q                   Local Address:Port               Peer Address:Port       Process       
LISTEN       0            128                            0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0            128                            0.0.0.0:111                     0.0.0.0:*                        
LISTEN       0            32                       192.168.122.1:53                      0.0.0.0:*                        
LISTEN       0            128                               [::]:22                         [::]:*                        
LISTEN       0            1                   [::ffff:127.0.0.1]:8005                          *:*                        
LISTEN       0            100                              [::1]:8009                       [::]:*                        
LISTEN       0            128                               [::]:111                        [::]:*                        
LISTEN       0            100                                  *:8080                          *:*        
[root@localhost manager]# cd ..
[root@localhost webapps]# ls
ROOT  docs  examples  host-manager  manager  test
[root@localhost webapps]# vim host-manager/META-INF/context.xml 

//放行端口

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
          allow="192\.168\.70\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />  改成自己端口
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

~                                                                       

刷新
tomcat部署,jenkins部署使用_第12张图片
这些都是/usr/local/tomcat/webapps/manager目录的类容,其中一个项目
tomcat部署,jenkins部署使用_第13张图片
流程控制打包工具jenkins
//java程序需要打包
下载官网
https://www.jenkins.io/
下载方法
tomcat部署,jenkins部署使用_第14张图片

3.jenkins简介

jenkins就是一个整合工具,把代码从git或者其他代码托管平台拉取过来,然后通过脚本语言编写执行流程,达到了项目一键启动的目的
准备工作
发布的包放到wabapps目录下

3.1部署jenkins
[root@localhost webapps]# ls
ROOT  docs  examples  host-manager  manager  test
[root@localhost webapps]# mv * /opt/
[root@localhost webapps]# ls
[root@localhost ~]# ls
anaconda-ks.cfg  apache-tomcat-9.0.65.tar.gz  jenkins.war
[root@localhost ~]# cp jenkins.war /usr/local/tomcat/webapps/

[root@localhost webapps]# ls
jenkins.war
//重启
[root@localhost ~]# catalina.sh stop
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@localhost ~]# ss -antl
State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
LISTEN        0             128                        0.0.0.0:22                      0.0.0.0:*                          
LISTEN        0             128                        0.0.0.0:111                     0.0.0.0:*                          
LISTEN        0             32                   192.168.122.1:53                      0.0.0.0:*                          
LISTEN        0             128                           [::]:22                         [::]:*                          
LISTEN        0             128                           [::]:111                        [::]:*                          

[root@localhost ~]# ps -ef | grep tomcat
root      634521  229197  0 08:48 pts/2    00:00:00 grep --color=auto tomcat
[root@localhost ~]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
[root@localhost ~]# ss -atnl
State        Recv-Q       Send-Q                   Local Address:Port               Peer Address:Port       Process       
LISTEN       0            128                            0.0.0.0:22                      0.0.0.0:*                        
LISTEN       0            128                            0.0.0.0:111                     0.0.0.0:*                        
LISTEN       0            32                       192.168.122.1:53                      0.0.0.0:*                        
LISTEN       0            128                               [::]:22                         [::]:*                        
LISTEN       0            1                   [::ffff:127.0.0.1]:8005       //出现三个说明成功                   *:*                        
LISTEN       0            100                              [::1]:8009         //出现              [::]:*                        
LISTEN       0            128                               [::]:111          //出现              [::]:*                        
LISTEN       0            100                                  *:8080        
                  
[root@localhost webapps]# ls  //自动解压
jenkins    jenkins.war

等一下

tomcat部署,jenkins部署使用_第15张图片
tomcat部署,jenkins部署使用_第16张图片

[root@localhost jenkins]# cat /root/.jenkins/secrets/initialAdminPassword
edff0db19e22484b9e067a3161d283b9
//装个拉包工具
[root@localhost jenkins]# dnf -y install git
//省略过程
//安装项目程序
[root@localhost ~]# git clone https://github.com/chendao2015/tomcat-java-demo.git   

安装推荐的插件
tomcat部署,jenkins部署使用_第17张图片
新手就直接用推荐插件
tomcat部署,jenkins部署使用_第18张图片
tomcat部署,jenkins部署使用_第19张图片
tomcat部署,jenkins部署使用_第20张图片

3.2使用jenkins部署项目
安装 IP
toment
jenkins
192.168.70.130
toment 192.168.70.131

准备工作

//建立远程连接与192.168.70.131
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:IOrwWasrpoPXiktXOQttwUhxK34HPngTjEWYP7LA4rQ root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|   o=+           |
|  .oB .          |
|.  +oB.          |
|.+.o=+=.         |
|= +++%..S        |
| E +*.*          |
|..+o..           |
|=oo..            |
|*=+o             |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id root@192.168.70.131
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.70.131 (192.168.70.131)' can't be established.
ECDSA key fingerprint is SHA256:Md8H02CePBkuSyXZCFORHc5EkxknUdwRXZwYcgn1Dsk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.70.131's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
//登录测试
[root@localhost ~]# ssh root@192.168.70.131
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Aug 18 06:05:38 2022 from 192.168.70.1
//关闭防火墙和selinux
[root@localhost webapps]# systemctl disable --now firewall
[root@localhost webapps]# setenforce 0
[root@localhost ~]# exit
logout
Connection to 192.168.70.131 closed.
[root@localhost ~]# 

tomcat部署,jenkins部署使用_第21张图片

tomcat部署,jenkins部署使用_第22张图片

  • 生成拉取脚本
  • 编写流水线脚本
  • 发布
    注意:真实情况发布要备份
    tomcat部署,jenkins部署使用_第23张图片

tomcat部署,jenkins部署使用_第24张图片
tomcat部署,jenkins部署使用_第25张图片

tomcat部署,jenkins部署使用_第26张图片

pipeline {
    agent any 
    stages {
        stage("pull code"){
            steps {
                git 'https://github.com/chendao2015/tomcat-java-demo.git'  //前面生成的脚本
            }
            
        }  
    }
}

编写脚本

//安装打包工具
[root@localhost ~]# yum install maven -y

tomcat部署,jenkins部署使用_第27张图片

pipeline {
    agent any 
    stages {
        stage("pull code"){
            steps {
                git 'https://github.com/chendao2015/tomcat-java-demo.git'  //拉包
            }
        }  
        stage("make pkg"){
            steps {
                sh """
                    mvn clean package -Dmaven.test.skip=true   //打包
                """
            }
        }  
    }
}
[root@localhost ~]# cd .jenkins/workspace/test
[root@localhost test1]# ls
db  Dockerfile  LICENSE  pom.xml  README.md  src 

tomcat部署,jenkins部署使用_第28张图片

tomcat部署,jenkins部署使用_第29张图片

//查看
[root@localhost test1]# cd target/   //target为打包后生成的目录
[root@localhost target]# ls
classes            ly-simple-tomcat-0.0.1-SNAPSHOT      maven-archiver
generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT.war  maven-status

添加发布脚本

pipeline {
    agent any 
    stages {
        stage("pull code"){
            steps {
                git 'https://github.com/chendao2015/tomcat-java-demo.git'
            }
        }    
        stage("make pkg"){
            steps {
                sh """
                    mvn clean package -Dmaven.test.skip=true
                """
            }
        } 
        stage("publish"){
            steps {
                sh """
                    cd /root/.jenkins/workspace/test/target/
                   scp ly-simple-tomcat-0.0.1-SNAPSHOT.war   [email protected]:/usr/local/tomcat/webapps/
                   ssh [email protected] '/usr/local/tomcat/bin/catalina.sh stop && /usr//local/tomcat/bin/catalina.sh start'
                   """
            }
        }
    }
}

192.168.70.131主机查看

[root@localhost ~]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# ls
docs  examples  host-manager  ly-simple-tomcat-0.0.1-SNAPSHOT  ly-simple-tomcat-0.0.1-SNAPSHOT.war  manager  ROOT

tomcat部署,jenkins部署使用_第30张图片

4.使用jenkins

tomcat部署,jenkins部署使用_第31张图片
运行java程序,多给内存,不卡
tomcat部署,jenkins部署使用_第32张图片
tomcat部署,jenkins部署使用_第33张图片
tomcat部署,jenkins部署使用_第34张图片
tomcat部署,jenkins部署使用_第35张图片
tomcat部署,jenkins部署使用_第36张图片

tomcat部署,jenkins部署使用_第37张图片
tomcat部署,jenkins部署使用_第38张图片
tomcat部署,jenkins部署使用_第39张图片

5.程序打包的工具

  • maven
  • ant
  • gradle
    注意:打包工具版本要与开发人员一致,不一致可能会出问题
    打包是根据pom.xml来打包的,这个pom.xml是maven打包
    manven
    简介:
    Maven是一个跨平台的项目管理工具,是Apache组织中的一个颇为成功的开源项目。Maven主要服务基于java的项目构建,项目信息管理和依赖管理。
    作用:
    1.Maven可以创建项目。
    2.Maven可以引入依赖的jar包。

ant
简介:
Ant是一个将软件编译、测试、部署等步骤联系在一起的自动化工具,Ant的主要用途是构建Java应用程序。
Ant提供了许多内置的任务,可以编译、组装、测试和运行Java应用程序。Ant还可以有效地用于构建非Java应用程序,例如C或C++应用程序。
优点:
Ant主要有以下优点:

跨平台。Ant是纯Java语言编写的,所以具有很好的跨平台性。
操作简单。Ant由一个内置任务和可选任务组成,运行时需要一个XML文件(构建文件)。
易维护集成。构建文件是XML格式的文件,结构清晰易于维护,由于跨平台和操作简单,很容易集成到开发环境中。

gradle
简介:
Gradle 就是一个自动化构建项目的工具,用来帮助我们自动构建项目。就像我们在写 Java 项目的时候,如果没有构建工具,我们需要先执行 javac 命令先将 Java 源码编译成 class 文件,然后再执行 jar 命令再把 class 文件打成 jar 包。有了构建工具我们只需要点一个按钮就可以了。

你可能感兴趣的:(tomcat,linux,java,jenkins)