使用Nginx实现Tomcat负载均衡,并使用Redis实现session共享
本文简单介绍如何使用Nginx来实现Tomcat的负载均衡,使用Nginx来作为静态资源服务器,使用Redis来实现session的共享。搭建环境如下:
操作系统: RedHat Enterprise Linux 7 64位
jdk: jdk-8u92-linux-x64
nginx: nginx-1.10.3
tomcat:apache-tomcat-8.5.31
redis: redis-3.2.9
部署图如下:
Redis的安装配置(单节点)
1、将安装包上传到/opt/study/redis目录下
2、解压安装包
cd /opt/study/redis
tar -zxvf redis-3.2.9.tar.gz
3、编译安装
cd /opt/study/redis/redis-3.2.9
make
make PREFIX=/opt/software/redis install
4、编辑配置文件
将安装包的redis.conf配置文件复制到 /opt/software/redis/bin 目录下,并修改以下关键配置信息:
#绑定本机IP地址
bind 192.168.134.130
#监听端口
port 7001
#是否作为守护进程运行
daemonize yes
pidfile /opt/software/redis/bin/redis.pid
logfile /opt/software/redis/bin/redis.log
5、启动Redis
cd /opt/software/redis/bin
./redis-server ./redis.conf
Tomcat的安装部署
1、将安装包apache-tomcat-8.5.31.tar.gz上传到/opt/software/tomcat目录下
2、解压安装包
cd /opt/software/tomcat
tar -zxvf apache-tomcat-8.5.31.tar.gz
3、将测试用的web应用部署到tomcat的webapps目录下。测试程序自行开发
4、启动tomcat
cd /opt/software/tomcat/apache-tomcat-8.5.31/bin
./startup.sh
springboot项目下使用spring-session实现session共享
1、在pom.xml文件中添加依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
2、创建配置类RedisSessionConfig.java
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class RedisSessionConfig {
}
3、在application.properties属性文件添加配置项
spring.redis.host=192.168.134.130 spring.redis.port=7001 spring.redis.timeout=30 spring.session.store-type=Redis spring.session.redis.flushMode=IMMEDIATE
使用tomcat-cluster-redis-session-manager插件实现Tomcat的session共享
1、从 https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wiki 下载插件的压缩包
2、将压缩包中的以下jar文件上传到tomcat的lib目录下,不同版本的插件对应不同的jar文件版本,本范例使用tomcat-cluster-redis-session-manager-2.0.zip版本的压缩包。
commons-logging-1.2.jar
commons-pool2-2.4.2.jar
jedis-2.9.0.jar
tomcat-cluster-redis-session-manager-2.0.jar
3、将redis-data-cache.properties复制到TOMCAT的conf目录下,并修改里面的参数值
redis.hosts=192.168.134.130:7001
4、编辑TOMCAT的conf/context.xml文件,在最后的</Context>前加入以下配置:
<Valve className="tomcat.request.session.redis.SessionHandlerValve" /> <Manager className="tomcat.request.session.redis.SessionManager" />
Nginx的安装配置
1、将安装包上传到/opt/study目录下
2、解压安装包
cd /opt/study
tar zxvf nginx-1.10.3.tar.gz
3、编译安装
cd /opt/study/nginx-1.10.3
./configure --prefix=/opt/software/nginx
make
make install
4、编辑/opt/software/nginx/conf/nginx.conf配置文件
#user nobody; worker_processes 1; 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; sendfile on; keepalive_timeout 65; #gzip on; upstream tomcat_server{ server 192.168.134.128:8080 weight=1; server 192.168.134.129:8080 weight=1; } server { listen 80; server_name 192.168.134.127; #处理静态资源 location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /opt/software/nginx/html; expires 30d; } #访问tomcat下的demo应用 location ~* /demo/ { proxy_pass http://tomcat_server; proxy_redirect default; } location / { root html; index index.html index.htm; } #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; } } }
5、启动nginx
cd /opt/software/nginx/sbin
./nginx
Nginx实现静态资源服务器
可以将静态网页、图片、css样式文件、js脚本文件等静态资源文件部署到Nginx服务器中,当客户端请求这些静态资源时,Nginx将直接返回这些静态资源,无需经过后台的应用服务器。
假设demo/index.jsp文件的内容如下:
<%@ page session="true" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="static/css/common.css" />
<title>demo page</title>
</head>
<body>
<img src="static/images/personal.png">
<script type="text/javascript" src="static/js/common.js"></script>
</body>
</html>
则静态资源文件在Nginx下的目录结构如下:
/opt/software/nginx/html/demo/static
css
common.css
images
personal.png
js
common.js
Nginx的配置文件需要添加以下配置信息:
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /opt/software/nginx/html;
expires 30d;
}