主要描述Nginx + tomcat + Redis的配合使用。本次是在windows环境下配置的,故此简单描述nginx和redis安装,建议大家使用Linux环境。
Linux下安装nginx和redis,如下:
https://blog.csdn.net/weixin_44153121/article/details/88182242
https://blog.csdn.net/weixin_44153121/article/details/89389225
端口规划
redis
localhost:6379
nginx
localhost:8080
tomcat
localhost:8081
localhost:8082
localhost:8083`
下载安装
本文主要描述Nginx + tomcat + Redis的配合使用,故此不详述安装。
安装 Redis
Redis官网没有windows版本,前往github下载,版本已不更新。
下载地址:https://github.com/MSOpenTech/redis/releases
Redis解压
启动服务器 D:\software\others\redis>redis-server.exe redis.windows.conf
启动客户端 D:\software\others\redis>redis-cli.exe
安装Nginxhttp://nginx.org/en/download.html
解压到目录 D:\software\others\nginx-1.14.2
调整配置文件nginx.conf:修改默认端口为8080
运行nginx.exe
浏览器中打开:http://localhost:8080/ 成功运行界面如下:
Tomcat集群配置
在tomcat中配置 Redisson 插件管理session的共享
Redissonhttps://github.com/redisson/redisson/tree/master/redisson-tomcat
Shared Redisson instance
RedissonSessionManager is created per Web Application and thus creates own Redisson instance. For multiple applications, which use the same Redis setup, amount of Redisson instances could be reduced using JNDI registry:
1.Add shared redisson instance produced by JndiRedissonFactory into tomcat/conf/server.xml in GlobalNamingResources tag area:
<GlobalNamingResources>
<Resource name="bean/redisson"
auth="Container"
factory="org.redisson.JndiRedissonFactory"
configPath="${catalina.base}/conf/redisson.yaml"
closeMethod="shutdown"/>
</GlobalNamingResources>
conf/redisson.yaml
singleServerConfig:
address: "redis://127.0.0.1:6379"
2.Add JndiRedissonSessionManager with resource link to redisson instance into tomcat/conf/context.xml
<ResourceLink name="bean/redisson" global="bean/redisson"
type="org.redisson.api.RedissonClient" />
<Manager className="org.redisson.tomcat.JndiRedissonSessionManager"
readMode="REDIS" jndiName="bean/redisson" />
3. Copy two jars into TOMCAT_BASE/lib directory:
redisson-all-3.10.5.jar
redisson-tomcat-9-3.10.5.jar
4、修改conf/server.xml文件中的端口分别为8081/8082/8083;
如果全部部署在一台机器上,其他端口也需要处理,比如:端口前分别加1/2/3
5、复制成3份
配置Nginx
#设定实际的服务器列表
upstream localhost {
server localhost:8081 weight=1;
server localhost:8082 weight=2;
server localhost:8083 weight=3;
}
#反向代理的路径(和upstream绑定),location 后面设置映射的路径
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
}
测试效果
在每个tomcat下的showCxbb.jsp中,增加session id的输出,同时写上对应的端口,以便前台测试。
浏览器访问
为什么第一个访问的网页是8083端口,因为nginx权重。
查看redis中的session