在網上大多數是關於tomcat7與redis結合實現session會話共享,徹底不符合如今的實際要求,因爲在工做中須要完成過tomcat8負載均衡的項目,結合項目總結把實現的過程展示出來。廢話很少說了,直接上乾貨,爲了保護客戶的權益,在此所有之內網IP來代替。本文先把Nginx代理功能與Tomcat8+Redis4實現session會話共享展現出來,關於數據庫、Redis主從等方面下個博文會詳細寫出來的。javascript
1、 項目簡單需求以下:css
一、 此項目是一個電商的網絡商城的網站,網站是用jdk-1.8編寫的html
二、 實現tomcat8負載均衡的功能,一臺Tomcat服務器掛掉不影響客戶的正常訪問java
三、 商城有登陸功能須要實現session會話共享linux
四、 數據庫用mariadb+galera來實現高可用nginx
五、 用proxysql來實現數據庫的讀寫分離c++
六、 Nginx代理經過Keepalived實現高可用web
說明:使用Nginx做爲Tomcat的負載平衡器,Tomcat的會話Session數據存儲在Redis,可以實現零宕機的7x24效果。由於將會話存儲在Redis中,所以Nginx就沒必要配置成stick粘貼某個Tomcat方式,這樣才能真正實現後臺多個Tomcat負載平衡。redis
2、 項目實施中的改進sql
一、 第一次項目實施研討會中準備使用nginx基於訪問ip的hash策略
二、 第二次項目實施研討會中準備使用是tomcat8的cluster會話保持功能
三、 第三次項目實施研討會中準備使用的是基於memcache會話保持功能
四、 最後肯定用Tomcat8+Redis4實現session會話共享
說明:關於memcache與redis的區別請你們自行上網查詢,在此不浪費你們時間了
3、 網絡架構簡單介紹以下
主機名稱 |
操做系統 |
IP地址分配 |
Nginx1.12 |
CentOS7.5 |
192.168.101.71 |
Tomcat-1 |
192.168.101.72 |
|
Tomcat-2 |
192.168.101.73 |
|
Redis4.06 |
192.168.101.74 |
4、 Nginx負載均衡功能實現
一、 centos7操做系統優化,以上是實際生產環境使用
1)vim /etc/security/limits.conf
最後添加
* soft nofile 102400
* hard nofile 102400
* soft nproc 102400
* hard nproc 102400
2) vim /etc/security/limits.d/20-nproc.conf
#加大普通用戶限制 也能夠改成unlimited
* soft nproc 102400
* hard nproc 102400
root soft nproc unlimited
3) vim /etc/sysctl.conf加入內容以下:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
net.netfilter.nf_conntrack_max = 655350
net.netfilter.nf_conntrack_tcp_timeout_established = 1200
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
vm.overcommit_memory=1
4) 最後 sysctl -p 生效
5)修改主機名以區分各機器功能
2、nginx安裝配置
1) 上傳nginx-1.12.2.tar.gz到/usr/local/src
2) yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
3) useradd -s /sbin/nologin www
4) cd /usr/local/src
5) tar -zxvf nginx-1.12.2.tar.gz -C /usr/local/
6) cd /usr/local/nginx-1.12.2/
7) ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_flv_module
8) make && make install
9) ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
10) vim /etc/init.d/nginx 內容以下:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# chkconfig: - 85 15
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Start nginx daemons functions.
start() {
if [ -f $nginx_pid ] ; then
echo "nginx already running...."
exit 1
fi
echo -n "Starting $prog: "
$nginxd -c ${nginx_config}
RETVAL=$?
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
}
# Stop nginx daemons functions.
stop() {
echo -n "Stopping $prog: "
$nginxd -s stop
RETVAL=$?
[ $RETVAL = 0 ] &&rm -f /var/lock/subsys/nginx
}
# reloadnginx service functions.
reload() {
echo -n "Reloading $prog: "
$nginxd -s reload
}
# statusngnx service functions
status() {
if [ -f $nginx_pid ] ; then
echo "$prog is running"
else
echo "$prog is stop"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $prog {start|stop|restart|reload|status}"
exit 1
;;
esac
11) chmod +x /etc/init.d/nginx
12) chkconfig --add nginx && chkconfig nginx on
13) systemctl daemon-reload
14) mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
15) vim /usr/local/nginx/conf/nginx.conf 內容以下:
user www www;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
worker_rlimit_nofile 102400;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 102400;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
server_tokens off;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#Compression Settings
gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
#end gzip
# http_proxy Settings
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#load balance Settings
upstream backend_tomcat {
server 192.168.101.72:8080 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.101.73:8080 weight=1 max_fails=2 fail_timeout=10s;
}
#virtual host Settings
server {
listen 80;
server_name www.benet.com;
charset utf-8;
location / {
root html;
index index.jsp index.html index.htm;
}
location ~* \.(jsp|do)$ {
proxy_pass http://backend_tomcat;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.101.0/24;
deny all;
}
}
}
16) service nginx restart 若是第一失敗就再執行一次,
17) Nginx代理服務器配置是4核8G主機
18) ss -tunlp 能夠查看到80端口已經對外提供服務了
3、安裝、配置 tomcat
jdk版本:jdk1.8.0_131
tomcat版本: apache-tomcat-9.0.2
1)所需軟件:(tomcat8.5和tomcat9 均可以,此處以tomcat9爲例)
apache-tomcat-8.5.20.tar.gz 或 apache-tomcat-9.0.2.tar.gz
———————————————————————————-
jdk-8u131-linux-x64.tar.gz
———————————————————————————-
commons-pool2-2.4.2.jar
jedis-2.9.0.jar
tomcat85-session-redis-1.0.jar
———————————————————————————-
下載地址:
連接:https://pan.baidu.com/s/1c1P6Nzq 密碼:13f5
參考配置:
context.xml
2)解壓tomcat、jdk到指定的目錄
# mkdir /app
# tar -xvf apache-tomcat-9.0.2.tar.gz -C /app/
# tar -xvf jdk-8u131-linux-x64.tar.gz -C /app/
3)指定jdk或者修改jdk環境變量:
3.1)指定JDK版本以便tomcat與jdk聯接
vim /app/apache-tomcat-9.0.2/bin/catalina.sh
添加以下內容:
export JAVA_HOME=/app/jdk1.8.0_131
export JRE_HOME=/app/jdk1.8.0_131/jre
位置以下圖:
3.2)修改java環境變量
(a)
在/etc/profile.d/目錄下,建立一個.sh結尾的文件,
好比:
vim /etc/profile.d/jdk180.sh
添加以下內容:
export JAVA_HOME=/app/jdk1.8.0_131
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
(b)
執行該腳本文件,是JAVA環境變量生效
source /etc/profile.d/jdk180.sh
3.3)拷貝Tomcat須要的3個jar文件
將下邊的三個文件拷貝至:${TOMCAT_HOME}/lib目錄下
commons-pool2-2.4.2.jar、jedis-2.9.0.jar、tomcat85-session-redis-1.0.jar
$ cp ./*.jar /app/apache-tomcat-9.0.2/lib/
3.4)修改context.xml文件
$ vim /app/apache-tomcat-9.0.2/conf/context.xml
添加以下內容:參考下載文件中的context.xml文件
————————————————– 分割線 ————————————————–
<Valve className=」com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve」 />
<Manager className=」com.orangefunction.tomcat.redissessions.RedisSessionManager」
host=」192.168.101.74″
port=」6379″
database=」0″
maxInactiveInterval=」60″ />
因在word文檔編輯模式,此處的內容複製到context.xml文件後須要在英文輸入法上把引號從新替換下
————————————————– 分割線 ————————————————–
3.5)建立測試文件:
cd /app/apache-tomcat-9.0.2/webapps/
mkdir test
vim test/index.jsp
以tomcat-1機器爲例index.jsp內容以下:tomcat-2機器則直接替換相應的名稱就能夠了(把tomcat-1換成tomcat-2)
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>tomcat-1</title>
</head>
<body>
<h1><font color="red">Session serviced by tomcat-1</font></h1>
<table aligh="center" border="1">
<tr>
<td>Session ID</td>
<td><%=session.getId() %></td>
<% session.setAttribute("abc","abc");%>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
<html>
3.6)啓動tomcat
/app/apache-tomcat-9.0.2/bin/startup.sh
4、安裝、配置 Redis(此處只是redis的簡單安裝配置)
4.1、下載、解壓、編譯:
yum -y install wget && wget http://download.redis.io/releases/redis-4.0.6.tar.gz
tar -zxvf redis-4.0.6.tar.gz -C /app/
cd /app/redis-4.0.6/
make && make install
默認配置文件位於:
/app/redis-4.0.6/redis.conf
二進制文件是編譯完成後在src目錄下,經過下面的命令啓動Redis服務:
#ll src/redis-server
注意:這裏直接執行Redis-server 啓動的Redis服務,是在前臺直接運行的(效果如上圖),也就是說,執行完該命令後,若是Lunix關閉當前會話,則Redis服務也隨即關閉。正常狀況下,啓動Redis服務須要從後臺啓動,而且指定啓動配置文件。
4.2、修改配置文件,讓redis以守護進程的形式後臺運行
A、vim redis.conf,將daemonize屬性改成yes(代表須要在後臺運行)
daemonize yes
B、默認是隻監聽在本機的本地IP地址:127.0.0.1的6379端口,
注意:#####################################
redis4.0.6 若是註釋掉默認的綁定地址 #bind 127.0.0.1,
有時會致使客戶端沒法連接redis-server
###########################################
添加要監聽的本地IP地址:
bind 127.0.0.1 192.168.101.74
能夠根據須要,修改監聽端口
4.3、啓動、中止 redis
啓動:
格式:redis-server 配置文件
src/redis-server redis.conf
關閉指定redis
src/redis-cli -p 6379 shutdown
中止:
# src/redis-cli -p 端口 shutdown
src/redis-cli -p 6379 shutdown
或者
pkill redis-server
4.4啓動Redis:
格式:redis-server 配置文件
cd /app/redis-4.0.6/ && src/redis-server redis.conf
5、訪問測試頁:
刷新頁面,分別訪問到不一樣的tomcat主機,但獲取的session ID相同,
說明:用Redis,實現tomcat session共享 測試成功
http://192.168.101.71/test 不斷刷新能夠看到session ID號是相同
參考如下文檔連接:
http://www.javashuo.com/article/p-zqqvxctd-mw.html
http://www.enunix.com/