CENTOS7部署tomcat及其负载均衡

Tomcat服务器:
是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。一般来说,Tomcat虽然和Apache或者nginx这些web服务器一样,具有处理器HTML页面的功能。然而由于处理静态HTML的能力远不及Apache或者Nginx,所以tomcat通常是作为一个servlet和JSP容器,单独运行在后端

案例环境
主机:Tomcat服务器
操作系统:Centos7
nginx服务器地址:192.168.79.100
mysql服务器:192.168.79.100
java服务器:192.168.79.100/101 两台实现
主要软件:jdk tomcat nginx mysql java-demo

准备工作:

systemctl stop iptables
systemctl stop fiewalld

安装tomcat:

tar xf jdk-8u221-linux-x64.tar.gz -C /usr/local/
#解压local下
mv jdk1.8.0_221/ jdk 
#改名
vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH

source /etc/profile 

[root@www ~]# java -version #改好直接看版本
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

tar xf apache-tomcat-8.5.16.tar.gz  -C /usr/local/

mv apache-tomcat-8.5.16/ tomcat

[root@www ~]# cd /usr/local/tomcat/
[root@www tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work


上传java项目 java-demo 到服务器

tar xf java-demo.tar.gz

[root@www src]# mv java-demo/ /usr/local/tomcat/webapps/ROOT/
#项目移到专门放部署的项目路径上

[root@www src]# vim /usr/local/tomcat/java-demo/src/main/resources/application.yml
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://192.168.79.100:3306/test? characterEncoding=utf-8
    username: root 
    password: 123 
#修改ip地址 用户名和mysql密码

vim tables_ly_tomcat.sql
/*
MySQL - 5.6.30-log : Database - test
*/
CREATE DATABASE IF NOT EXISTS `test`  DEFAULT CHARACTER SET utf8 ;
USE `test`;
#修改test

连接数据库授权

打包java-demo

yum -y install maven
mvn clean package -Dmaven.skip.test=True
#打包命令 跳过单元测试
打包出来有个target文件 为打包的文件
[root@www java-demo]# ls
db  pom.xml  src  target

mv ly-simple-tomcat-0.0.1-SNAPSHOT.war  /usr/local/tomcat/webapps/ROOT/
#讲war包移动到 webapps/ROOT下 
unzip ly-simple-tomcat-0.0.1-SNAPSHOT.war
[root@www ROOT]# ls
ly-simple-tomcat-0.0.1-SNAPSHOT.war  META-INF  WEB-INF

scp ly-simple-tomcat-0.0.1-SNAPSHOT.war [email protected]:/usr/local/tomcat
/webapps/ROOT/The authenticity of host '192.168.79.101 (192.168.79.101)' can't be established.
ECDSA key fingerprint is SHA256:QXjNsQ9JWCCd209Snw+x4cpBRFw7N3kzzuo2YIsLnAI.
ECDSA key fingerprint is MD5:aa:f2:6c:6d:91:db:de:d9:6a:4d:e9:ef:3e:c1:a6:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.79.101' (ECDSA) to the list of known hosts.
[email protected]'s password: 
ly-simple-tomcat-0.0.1-SNAPSHOT.war                         100%   17MB  91.5MB/s   00:00
#将war包传送到101服务器的WEB/ROOT下

[root@www ROOT]# vim /usr/local/tomcat/conf/server.xml
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
          <Context docBase="/usr/local/tomcat/webapps/ROOT/" path="" reloadable="true" />
          
  #部署项目存放的路径        docBase="/usr/local/tomcat/webapps/ROOT/"
      
    [root@www bin]# ./startup.sh bin下开启tomcat

CENTOS7部署tomcat及其负载均衡_第1张图片

现 用nginx 实现 100 101 登录项目 实现均衡负载

101基于100部署tomcat

[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml
 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
            <Context docBase="/usr/local/tomcat/webapps/ROOT"  path="" reloadable="true" />
</Host>

[root@localhost ROOT]# ls
ly-simple-tomcat-0.0.1-SNAPSHOT.war

[root@localhost ROOT]# unzip ly-simple-tomcat-0.0.1-SNAPSHOT.war
cd /usr/local/tomcat/bin/
./startup


进入100nginx服务器

[root@www conf]# pwd
/usr/local/nginx/conf
vim nginx.conf

keepalive_timeout  65;
    upstream ROOT {   #上游  地址池 实现均衡负载
               server 192.168.79.100:8080 weight=1;  #权重
               server 192.168.79.101:8080 weight=1;

    }
    #gzip  on;

    server {
        listen       80;
        server_name  www.thr.com; #100域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://ROOT; #引用上游
        }
        
        重启
        

你可能感兴趣的:(LINUX,tomcat)