Nginx负载均衡 + tomcat集群 + Redis缓存

29 篇文章 2 订阅
10 篇文章 0 订阅

主要描述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
windowsRedis
Redis解压
启动服务器 D:\software\others\redis>redis-server.exe redis.windows.conf
redissever
启动客户端 D:\software\others\redis>redis-cli.exe
cli
安装Nginxhttp://nginx.org/en/download.html
解压到目录 D:\software\others\nginx-1.14.2
调整配置文件nginx.conf:修改默认端口为8080
运行nginx.exe
浏览器中打开:http://localhost:8080/ 成功运行界面如下:
nginxhome

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份
tomcatcluster

配置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;
        }	

proxy

测试效果

在每个tomcat下的showCxbb.jsp中,增加session id的输出,同时写上对应的端口,以便前台测试。

浏览器访问
8083
8082
8081
为什么第一个访问的网页是8083端口,因为nginx权重。

查看redis中的session
session

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值