使用nginx + tomcat + redis 实现集群

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38361347/article/details/82629025

为什么要使用集群?集群有什么好处?

利用nginx对请求进行分流,将请求平均的分给不同的tomcat去处理,减少单个tomcat的负载量,提高tomcat的响应速度。

1 材料准备

tomcat 至少2个

redis 1个

nginx  1个  下载地址 http://nginx.org/

jar包           下载地址 https://pan.baidu.com/s/1zjSwS8HakqnWsbN1TfFy2Q 密码:p8fz 

commons-pool2-2.3.

jar jedis-2.7.3.jar 

tomcat-redis-session-manager-master-2.0.0.jar

2下载两个tomcat 并且更改端口

成功启动,说明端口修改成功

3 新建一个Test测试项目

 首页可以这样子显示,不同的tomcat 首页可以用1,2来区分,并且打印出sessionId 

启动tomcat发现 session不相同 tomcat不同

4 然后导入刚才准备好的jar包,放进tomcat/lib下面(两个tomcat都需要导入)

5 修改tomcat 的 context.xml ,加入以下xml代码

 <Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" />
   <Manager className="com.naritech.nicole.gump.RedisSessionManager"
     host="127.0.0.1"
     port="6379"
     database="0"
     maxInactiveInterval="60"
     />

tomcat  8 加入 

<Resources allowLinking="true" />

tomcat  7 加入 

<Context allowLinking="true" />

context.xml

<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
	<Resources allowLinking="true" />
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />	
    -->
 <Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" />
   <Manager className="com.naritech.nicole.gump.RedisSessionManager"
     host="127.0.0.1"
     port="6379"
     database="0"
     maxInactiveInterval="60"
     />


	</Context>

6 启动tomcat

session相同 tomcat不同

然后下面我们用nginx进行分派发送给tomcat集群

7 先启动nginx 

cmd  进入解压后的nginx 目录下 start nginx 

启动成功(说明nginx是正常的)

接下来设置配置文件nginx.conf ,用于分派给tomcat集群

8 启动redis 

9 重启nginx

打开浏览器输入访问的url   http://localhost:9090/Test/ 

多次刷新会发现在两个页面之间切换,但是session是相同的  tomcat页面1和2相互之间进行切换

10 查看redis里的session值 

以上是tomcat集群,如有错误,请指出。

猜你喜欢

转载自blog.csdn.net/weixin_38361347/article/details/82629025
今日推荐