tomcat单机变集群

单主机部署服务

zrlog博客网站搭建

环境准备:

系统:centos.7.6

数据库:5.5.64-MariaDB

主机名 IP地址 服务 目录
web01 10.0.0.7 tomcat、mariadb数据库 /app/tomcat
  •  阿里源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安装jdk

jdk下载地址:https://www.oracle.com/java/technologies/javase-jdk8-downloads.html

jdkrpm包网盘链接:https://pan.baidu.com/s/1zyFC-mTVqFPfFnTJQXaa4g 提取码:69eq

方法一:

[root@web01 ~]# rpm -ivh jdk-8u102-linux-x64.rpm
#详细信息
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8.0_102-2000:1.8.0_102-fcs  ################################# [100%]

#查看版本
[root@web01 ~]# java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

方法二:

jdk压缩包链接:https://pan.baidu.com/s/1z61kbs-hj7yfnMrpZZ4bXw 提取码:gz86

mkdir /app
tar xf jdk-8u60-linux-x64.tar.gz -C /app/
ln -s /app/jdk1.8.0_60 /app/jdk
sed -i.ori '$a export JAVA_HOME=/app/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
source /etc/profile

安装tomcat

tomcat下载地址:https://tomcat.apache.org/download-80.cgi#8.5.53

mkdir /app
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.53/bin/apache-tomcat-8.5.53.tar.gz
tar xf apache-tomcat-8.5.53.tar.gz -C /app/
ln -s /app/apache-tomcat-8.5.53/ /app/tomcat
/app/tomcat/bin/startup.sh 
  • 安装zrlog

注:计算机HOSTS文件写好主机解析

#创建网站目录
mkdir /html
#修改配置文件,加入一个host
vim /app/tomcat/conf/server.xml
      <Host name="zrlog.oldedu.com"  appBase="/html"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="zrlog_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>

#下载zrlog修改为ROOT.war到/html下
wget http://dl.zrlog.com/release/zrlog-2.1.7-768d7a6-release.war?attname=ROOT.war&ref=index
mv zrlog-2.1.7-768d7a6-release.war\?attname\=ROOT.war /html/ROOT.war
#启动tomcat
/app/tomcat/bin/shutdown.sh
/app/tomcat/bin/startup.sh 

安装mariadb数据库

#安装数据库
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb

#创建zrlog库,授权用户
create database zrlog character set utf8;
grant all on zrlog.* to 'zrlog'@'localhost' identified by '123456';

网站填写过程略。。。。

注:如果想重现安装就删除html/ROOT/WEB-INF/install.lock文件,数据库名密码存放文件是db.properties

集群部署

变迁图:

地址规划:

主机 IP地址 服务 作用
lb01 10.0.0.5 mariadb、nginx 负载均衡、处理静态请求、存储数据
web01 10.0.0.7 tomcat 处理动态请求
web02 10.0.0.8 tomcat 处理动态请求

导入zrlog数据库

#07主机将zrlog数据库导入05主机内
mysqldump -B zrlog > zrlog.sql
scp zrlog.sql  10.0.0.5:/root

#lb01主机导入数据库
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb

mysql < zrlog.sql
show databases;
#创建并授权用户,让其他主机可以连接上
grant all on zrlog.* to 'zrlog'@'10.0.0.%' identified by '123456';

注:因为只是把zrlog的表给导入过来,但是存放用户信息的mysql.user表没有,所以我们要重新创建用户信息

迁移tomcat

#web01迁移tomcat使环境一致
scp -rp /root/jdk-8u102-linux-x64.rpm 10.0.0.8:/
scp -rp /app/apache-tomcat-8.5.53 10.0.0.8:/app

#web02准备环境
rpm -ivh jdk-8u102-linux-x64.rpm
ln -s apache-tomcat-8.5.53 tomcat

迁移网站目录

#web01迁移网站
scp -rp /html 10.0.0.8:/

#tomcat网站修改数据库连接文件
vim /html/ROOT/WEB-INF/db.properties
driverClass=com.mysql.cj.jdbc.Driver
user=zrlog
password=123456
jdbcUrl=jdbc\:mysql\://10.0.0.5\:3306/zrlog?characterEncoding\=UTF-8&allowPublicKeyRetrieval\=true&useSSL\=false&serverTimezone\=GMT

#重启tomcat
/app/tomcat/bin/shutdown.sh
/app/tomcat/bin/start.sh
  • 修改前端代码文件,可以看清谁在访问
vim  /html/ROOT/include/templates/default/header.jsp

#web01
60   <a title="${_res.title}" href="${rurl}">web01</a>
#web02
60   <a title="${_res.title}" href="${rurl}">web02</a>

配置负载均衡

注:HOSTS文件修改主机域名劫持10.0.0.5 zrlog.oldedu.com

yum install nginx -y
systemctl start nginx
systemctl enable nginx

grep -Ev '^$|#' /etc/nginx/nginx.conf.default  > /etc/nginx/nginx.conf

#修改nginx配置文件
vim /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream java {
        server 10.0.0.7:8080;
        server 10.0.0.8:8080;
    }
    server {
        listen       80;
        server_name  zrlog.oldedu.com;
        root   html;
        index  index.html index.htm;
        location / {
            proxy_pass http://java;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
  }
}

#检查nginx
nginx -t
systemctl restart nginx

页面展示

网页输入:http://zrlog.oldedu.com/ 刷新,注意浏览器缓存

tomcat启动慢解决方案

没优化之前启动时间
[root@tomcat logs]# grep 'Server startup' catalina.out
03-Aug-2019 03:15:18.225 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 591050 ms

优化之后启动时间
[root@tomcat logs]# grep 'Server startup' catalina.out
03-Aug-2019 03:15:18.225 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 591050 ms
03-Aug-2019 03:22:14.112 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1326 ms

优化方法:
vi /usr/java/jdk1.8.0_102/jre/lib/security/java.security
securerandom.source=file:/dev/urandom

tomcat目录结构介绍

[root@web01 /app/tomcat]# ll
total 128
drwxr-xr-x 2 root root  4096 Aug  3 03:05 bin  #主要包含启动、关闭tomcat脚本和脚本依赖文件  非常重要
drwxr-xr-x 3 root root   198 Aug  3 03:05 conf #tomcat配置文件目录          非常重要
drwxr-xr-x 2 root root  4096 Aug  3 03:05 lib  #tomcat运行需要加载的jar包    非常重要
-rw-r--r-- 1 root root 57011 Sep 28  2015 LICENSE #license文件,不重要
drwxr-xr-x 2 root root   197 Aug  3 03:15 logs  #在运行过程中产生的日志文件   非常重要
-rw-r--r-- 1 root root  1444 Sep 28  2015 NOTICE #不重要
-rw-r--r-- 1 root root  6741 Sep 28  2015 RELEASE-NOTES #版本特性,不重要
-rw-r--r-- 1 root root 16204 Sep 28  2015 RUNNING.txt   #帮助文件,不重要
drwxr-xr-x 2 root root    30 Aug  3 03:05 temp    #存放临时文件
drwxr-xr-x 7 root root    81 Sep 28  2015 webapps #站点目录   非常重要
drwxr-xr-x 3 root root    22 Aug  3 03:05 work    #tomcat运行时产生的缓存文件

配置tomcat basic认证

vim /app/tomcat/webapps/ROOT/WEB-INF/web.xml

<web-app>
......    
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>test</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        
        <auth-constraint>
            <role-name>test100</role-name>
        </auth-constraint>
    </security-constraint> 
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Default</realm-name>
    </login-config>
</web-app>

# 添加系统角色
vim  /app/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="test100"/>
<user username="tomcat" password="123456" roles="manager-gui,test100"/>

#重启tomcat生效
/app/tomcat/bin/shutdown.sh
/app/tomcat/bin/startup.sh

登录网站:10.0.0.7:8080

注:如果没有网页查看tomcat是否开启

nginx+tomcat集群实现全栈https

注:证书是根据我们自己购买的域名在阿里云上免费申请的,这里需要把域名都改为证书的域名

  • 创建密钥存放目录
mkdir /opt/sert/
#证书位置
ll
/opt/cert/nginx/1_blog.oldqiang.com_bundle.crt
/opt/cert/nginx/2_blog.oldqiang.com.key
  • nginx.conf配置文件证书内容
server {
        listen 443 ssl;
        server_name blog.oldqiang.com;
        ssl_certificate /opt/cert/nginx/1_blog.oldqiang.com_bundle.crt;
        ssl_certificate_key /opt/cert/nginx/2_blog.oldqiang.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        root   /opt/tuchuang;
        index  index.php index.html index.htm;
        location / {
            proxy_pass http://java;
            proxy_set_header Host $http_host;
        }
}
server {
        listen 80;
        server_name blog.oldqiang.com;
        location / {
           return    302 https://blog.oldqiang.com$request_uri;
        }
}

#重启nginx生效配置文件
systemctl restart nginx
  • 修改tomcat的域名
#主机1和主机2都需要修改域名
vim /app/tomcat/conf/server.xml
      <Host name="blog.oldqiang.com"  appBase="/html"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="zrlog_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
      
#全部主机重启tomcat
/app/tomcat/bin/shutdown.sh
/app/tomcat/bin/startup.sh
netstat -lntup | grep 8080

注:HOSTS修改域名

10.0.0.5 blog.oldqiang.com zrlog.oldedu.com

实验效果:

tomcat使用redis实现session共享

参考简书地址:https://www.jianshu.com/p/d3df0d708f2e

地址规划:

主机名 系统版本 ip地址 软件版本
lb01 Centos7.6 10.0.0.5 Nginx1.16 redis
web01 Centos7.6 10.0.0.7 tomcat-8.5
web02 Centos7.6 10.0.0.8 tomcat-8.5

安装Redis

注:我们是在10.0.0.5主机上访问web网站,索引将session共享存储在这里

#下载redis
yum install redis -y

#更改监听地址,监听所有
vim  /etc/redis.conf
61 bind  0.0.0.0

#开启redis
systemctl restart redis
systemctl enable redis
#查看端口
netstat -lntup | grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      8141/redis-server 0 

安装tomcat的session共享插件

链接:https://pan.baidu.com/s/1HAbpjElTbZeZRMWLoOd_zw 提取码:g7zr

项目地址:https://github.com/redisson/redisson/tree/master/redisson-tomcat

注:以下内容web主机都要添加

  • jar包存放地址

cd /app/tomcat/lib/
ll
-rw-r--r-- 1 root root 17787578 Mar 24 19:36 redisson-all-3.11.6.jar
-rw-r--r-- 1 root root    26316 Mar 24 19:36 redisson-tomcat-8-3.11.6.jar
  • context.xml添加的内容
vim /app/tomcat/conf/context.xml
#内容加入<Context>下面的内容</Context>
<Manager className="org.redisson.tomcat.RedissonSessionManager"
  configPath="${catalina.base}/conf/redisson.conf" 
  readMode="REDIS" updateMode="DEFAULT"/>  

内容含义:想读取redisson这个函数,就去redisson.conf这个配置文件

  • redisson.conf配置文件内容
vim  /app/tomcat/conf/redisson.conf
{
   "singleServerConfig":{
      "idleConnectionTimeout":10000,
      "connectTimeout":10000,
      "timeout":3000,
      "retryAttempts":3,
      "retryInterval":1500,
      "password":null,
      "subscriptionsPerConnection":5,
      "clientName":null,
      #redis连接地址
      "address": "redis://10.0.0.5:6379",
      "subscriptionConnectionMinimumIdleSize":1,
      "subscriptionConnectionPoolSize":50,
      "connectionMinimumIdleSize":32,
      "connectionPoolSize":64,
      "database":0,
      "dnsMonitoringInterval":5000
   },
   "threads":0,
   "nettyThreads":0,
   "codec":{
      "class":"org.redisson.codec.FstCodec"
   },
   "transportMode":"NIO"
}
  • 重启tomcat
/app/tomcat/bin/shutdown.sh
/app/tomcat/bin/startup.sh
netstat -lntup | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      20719/java        

验证session

访问:http://10.0.0.7:8080/examples/servlets/servlet/SessionExample

redis内容查看

#这里是访问两个web网站session的结果
127.0.0.1:6379> keys *
1) "redisson:tomcat_session:9D84E8A0F4D6C042181FA1C062B522EF"
2) "redisson:tomcat_session:CE1C7093A4CE583B5E4D471C136280C5"

 

posted @ 2020-03-24 13:08  kerwin-  阅读(361)  评论(0编辑  收藏  举报