#文件准备 ##nginx: [nginx-1.10.3.tar.gz](http://nginx.org/download/nginx-1.10.3.tar.gz) ##tomcat8: [apache-tomcat-8.0.43.zip](http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.0.43/bin/apache-tomcat-8.0.43.zip) ##redis: [redis-3.2.8.tar.gz](http://download.redis.io/releases/redis-3.2.8.tar.gz) ##ruby(当使用redis cluster时需要): [ruby-2.4.1.tar.gz](https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz) ##TomcatRedisSessionManager(From [GitHub](https://github.com/ran-jit/TomcatClusterRedisSessionManager)): [TomcatRedisSessionManager-1.1.1.zip](https://github.com/ran-jit/TomcatClusterRedisSessionManager/releases/download/1.1.1/TomcatRedisSessionManager-1.1.1.zip) ##jdk :[jdk-8u131-linux-x64.tar.gz](http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz)
#环境安装 >**PS:建议将所有文件下载后上传至/usr/local/src ;若无其他说明,本教程所有操作均以/usr/local/src作为起手目录**
##redis
``` tar -zxvf redis-3.2.8.tar.gz cd redis-3.2.8 make ``` >建议安装后, 将redis移动到/usr/local/redis3.2或相似的好记的目录,方便后续操作 安装后查看是否安装成功
``` cd src ./redis-server ../redis.conf ``` 具体参考: [redis安装|菜鸟教程](http://www.runoob.com/redis/redis-install.html) ##nginx ``` yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz tar zxvf pcre-8.35.tar.gz cd pcre-8.35 ./configure make && make install tar -zxvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./confignre --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make && make install ```
安装完成后,进入sbin目录, 输入
`nginx -s start ` 默认端口80,尝试访问 >nginx源码安装较为复杂,参考网上完整安装教程: [Nginx安装配置 | 菜鸟教程](http://www.runoob.com/linux/nginx-install-setup.html)
##tomcat
```
tar -zxvf apache-tomcat-8.0.43.zip cd apache-tomcat-8.0.43 cd bin ./startup.sh ```
解压即安装,启动后默认端口为8080, 自行测试server是否正常开启,若失败,尝试以下命令 ```
systemctl stop firewalld systemctl stop iptabled
```
#nginx+tomcat集群搭建
##复制一份tomcat8
```
mv apache-tomcat-8.0.43 /usr/local/tomcat8/8081 cp /usr/local/tomcat8/8081 /usr/local/tomcat8/8082
```
##修改tomcat的启动及关闭端口
###更改配置文件 ####tomcat/conf/server.xml 更改关闭端口,例如8005->8015
```
<Server port="8005" shutdown="SHUTDOWN">
.....
```
更改启动端口,例如8080->8081
```
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
```
##配置nginx
###nginx.conf
```
#user nobody; worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice; #error_log logs/error.log info;
#pid
logs/nginx.pid; events { worker_connections 1024; } 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; sendfile on;
#tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on; upstream tomcatcluster{ server 127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=1; } server { listen 80; server_name localhost; charset utf-8;
#access_log logs/host.access.log main; location / { proxy_pass http://tomcatcluster; proxy_redirect default; }
#error_page 404 /404.html; # redirect server error pages to the static page /50x.html
# error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
```
其中核心是 >upstream tomcatcluster{ server 127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=1; } 定义一个upstream 标识为tomcatcluster; server:定义一个服务器 ip:port:指定URL weight:权重, 越大 >location / { proxy_pass http://tomcatcluster; proxy_redirect default; } http:// 后接upstream指定的标识 ,如 tomcatcluster
##启动nginx
进入nginx目录, 比如我的/usr/local/nginx
```
cd /usr/local/nignx
cd sbin
./nginx -s start
```
启动前可使用` nignx -t` 检验配置文件是否错误.
浏览器访问80端口, 若无意外, 则显示tomcat的欢迎界面,于是为tomcat添加项目
##修改tomcat/webapps/ROOT
```
cd /usr/local/tomcat8/8081
cd webapps/ROOT
rm -rf *
vi index.jsp
```
输入以下内容:
```
<html>
<body>
<h1>This is tomcat 1 , sessionId: <%=session.getId()%></h1>
<% session.setAttribute("aa","guddqs"); session.setAttribute("bb","bbgudqs"); %>
</body>
</html>
```
类似的修改8082, 要在jsp上区分出是哪个tomcat服务
修改后,多次刷新页面, 轮次出现2个tomcat的页面. 至此nginx+tomcat负载均衡搭建完毕
#redis共享session for tomcat
>PS: 本教程采用tomcat8, 并使用国外某大佬github项目, 对于tomcat7需要另一个redis-session-manager.jar
##准备文件
```
unzip TomcatRedisSessionManager-1.1.1.zip
cd TomcatRedisSessionManager-1.1.1
ls -l
total 740
-rw-r--r--. 1 root root 61829 Mar 19 2016 commons-logging-1.2.jar
-rw-r--r--. 1 root root 111892 Mar 19 2016 commons-pool2-2.4.1.jar
-rw-r--r--. 1 root root 533252 Feb 21 2016 jedis-2.8.0.jar
-rw-r--r--. 1 root root 1850 Nov 26 00:30 ReadMe.txt
-rw-r--r--. 1 root root 324 Nov 25 23:56 RedisDataCache.properties
-rw-r--r--. 1 root root 28807 Nov 26 00:49 TomcatRedisSessionManager-1.1.1.jar
```
其中4个jar包需要复制到tomcat的lib目录下
```
cp commons-logging-1.2.jar /usr/local/tomcat/8081/lib
cp commons-pool2-2.4.1.jar /usr/local/tomcat/8081/lib
.......
```
RedisDataCache.properties需要复制到tomcat的conf下
```
cp RedisDataCache.properties /usr/local/tomcat/8081/conf
```
##修改配置文件
###context.xml
进入tomcat的conf目录, 修改 context.xml文件
再`<Context>` 节点下加入
```
<Valve className="com.r.tomcat.session.management.RequestSessionHandlerValve" />
<Manager className="com.r.tomcat.session.management.RequestSessionManager" />
```
###RedisDataCache.properties
然后修改之前复制到conf下的RedisDataCache.properties文件,切记不可修改文件名
```
vi RedisDataCache.properties
```
单redis配置如下:
```
redis.hosts=127.0.0.1:6379
# Redis Password
redis.password=
# set true to enable redis cluster mode
redis.cluster.enabled=false
# Redis database (default 0)
#redis.database=0
# Redis connection timeout (default 2000)
#redis.timeout=2000
```
redis cluster模式配置文件如下:
```
redis.hosts=127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002,127.0.0.1:3003,127.0.0.1:3004,127.0.0.1:3005
# Redis Password
redis.password=
# set true to enable redis cluster mode
redis.cluster.enabled=true
# Redis database (default 0)
#redis.database=0
# Redis connection timeout (default 2000)
#redis.timeout=2000
```
>PS: redis.password留空则等于无密码
类似的修改8082下的tomcat配置文件, 复制jar包, RedisDataCache.properties
最后重启2个tomcat, 访问80端口, 刷新页面, 观察页面显示是否达到 2个不同的tomcat服务单具有相同的session id
#小结
>使用nginx反向代理到2个tomcat服务器, 仅需修改tomcat配置不同的端口,以及为nginx.conf添加upstream配置并将反向代理指向upstream即可
>而添加tomcat的session共享则利用了tomcat的提供的外部session存储机制接口,而实现则使用了redis作为储存源.从而实现了session共享.