前端代理服务器nginx:192.168.223.136

tomcat服务器:采用的一台多实例192.168.223.146:8081,192.168.223.146:8082(如何构建多实例tomcat,请参考前面的文章)

首先查看下tomcat的webapps目录架构:

[root@wadeson tomcat-instance]# pwd
/usr/local/tomcat-instance
[root@wadeson tomcat-instance]# ll
总用量 24
-rwxr-xr-x. 1 root root 165 8月 9 15:31 start-tomcat1.sh
-rwxr-xr-x. 1 root root 165 8月 9 16:18 start-tomcat2.sh
-rwxr-xr-x. 1 root root 165 8月 9 15:38 stop-tomcat1.sh
-rwxr-xr-x. 1 root root 165 8月 9 16:18 stop-tomcat2.sh
drwxr-xr-x. 7 root root 4096 8月 9 15:45 tomcat1
drwxr-xr-x. 7 root root 4096 8月 9 16:12 tomcat2
[root@wadeson tomcat-instance]# cd tomcat1/webapps/
[root@wadeson webapps]# tree .
.
├── myapp
│   └── test.jsp
└── ROOT
  ├── classes
  ├── index.jsp
  ├── lib
  ├── META-INF
  └── WEB-INF

由于安装的tomcat是8.5版本的,所以查看下官方文档:https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

构建tomcat集群只需要如下几步:

1、添加集群组件信息(在host或者engine组件下添加进去就行)

  <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
</Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

以上是直接摘自官网,需要稍微做下修改address="auto",address="228.0.0.4"
2、需要在各自的应用程序目录下面为web.xml添加一行<distributable/>元素
  • Make sure your web.xml has the <distributable/> element

现在直接贴自己的配置tomcat1:

<Host name="localhost" appBase="/usr/local/tomcat-instance/tomcat1/webapps"  unpackWARs="true" autoDeploy="true">

<Context path="" docBase="ROOT" reloadable="true"/>
  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/usr/local/tomcat-instance/tomcat1/logs"
  prefix="localhost_access_log" suffix=".txt"
  pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">

<Manager className="org.apache.catalina.ha.session.DeltaManager"
  expireSessionsOnShutdown="false"
  notifyListenersOnReplication="true"/>

  <Channel className="org.apache.catalina.tribes.group.GroupChannel">
  <Membership className="org.apache.catalina.tribes.membership.McastService"
    address="228.0.0.4"
    port="45564"
    frequency="500"
    dropTime="3000"/>
  <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
    address="192.168.223.146"                 由于是一台主机上的两个tomcat实例,所以这里的cluster添加的内容都一样
    port="4000"
    autoBind="100"
    selectorTimeout="5000"
    maxThreads="6"/>

  <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
  <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
  </Sender>
  <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
  <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
  </Channel>

  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
  filter=""/>
  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

  <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
    tempDir="/tmp/war-temp/"
    deployDir="/tmp/war-deploy/"
    watchDir="/tmp/war-listen/"
    watchEnabled="false"/>

  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
</Host>

然后在应用程序ROOT下面的WEB-INF的下面添加自己的web.xml:

[root@wadeson tomcat-instance]# ll tomcat1/webapps/ROOT/WEB-INF/web.xml
-rw-------. 1 root root 168271 8月 10 16:50 tomcat1/webapps/ROOT/WEB-INF/web.xml

  <distributable/>         这就是需要添加的一行内容
</web-app>

于是配置tomcat1实例完成,tomcat2的配置和tomcat1一致(由于是同一主机的不同实例)

于是进行访问:

可以看见session的id值两者相同,只是后面的tomcat不一致,于是session共享的功能就达成了,然后配置nginx进行代理转发:

upstream backserver {
  server 192.168.223.146:8081 weight=1;
  server 192.168.223.146:8082 weight=1;
}

server {
  listen 80;
  server_name 192.168.223.136;

  location / {
    root html;
    index index.html index.htm;
    proxy_pass http://backserver/;
  }

测试效果:

基于tomcat集群做session共享的更多相关文章

  1. Dubbo入门到精通学习笔记(十八):使用Redis3.0集群实现Tomcat集群的Session共享

    文章目录 1.单节点访问http://192.168.1.61:8082/pay-web-boss/: 2.增加多一个消费者节点:192.168.1.62,以同样的方式部署pay-web-boss工程 ...

  2. Tomcat集群的session共享

    配置Tomcat的session共享可以有三种解决方案: 第一种是以负载均衡服务器本身提供的session共享策略,每种服务器的配置是不一样的并且nginx本身是没有的. 第二种是利用web容器本身的 ...

  3. Tomcat 集群 + Redis Session 共享出现 Session 瞬间失效问题

    写在前面的话 写这篇博客出于公司最近要迁移到新的云上面且对之前的资源,架构做一个升级. 本来是一个不大的项目,旧环境旧一个 TOMCAT 跑起来,不过出于高可用考虑,新环境决定使用 TOMCAT 集群 ...

  4. Nginx+Tomcat+Memcached实现tomcat集群和session共享

    一.Nginx安装 详见前文:http://www.cnblogs.com/yixiwenwen/p/3574097.html 二.memcached安装和启动 详见前文:http://www.cnb ...

  5. 关于 tomcat 集群中 session 共享的三种方法

    前两种均需要使用 memcached 或redis 存储 session ,最后一种使用 terracotta 服务器共享. 建议使用 redis,不仅仅因为它可以将缓存的内容持久化,还因为它支持的单 ...

  6. tomcat集群及session共享

    一般来说,java web app主要用作两个领域: 1.api.api一般是无状态的,所以无需考虑session共享的问题 2.传统web应用和网站,如crm,oa,erp,b2c,bbs等.尤其b ...

  7. 通过memcached来实现对tomcat集群中Session的共享策略

    近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 上述功能已经实现,有需要了解的朋友可以看我另外一篇博文. Linux下Apache与Tomca ...

  8. Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享

    Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 ============================= 蕃薯耀 2017年11月27日 http: ...

  9. Redis存储Tomcat集群的Session

    Redis存储Tomcat集群的Session 如何 做到把新开发的代码推送到到生产系统中部署,生产系统要能够零宕机.对使用用户零影响. 设想 是使用集群来搞定,通过通知负载均衡Nginx,取下集群中 ...

随机推荐

  1. [SharePoint 2010] Visual Studio 2010內撰寫視覺化WebPart超簡單

    新一代的Visual Studio 2010對於SharePoint 2010的專案撰寫,有非常另人讚賞的改進. 以往寫一個WebPart要搞好多雜七雜八的步驟,也要硬寫HTML輸出,當然有人說可以寫 ...

  2. LeetCode-Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. Zabbix监控介绍及安装配置

    什么是zabbix zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵 ...

  4. Servlet------>jsp输出JavaBean

    JavaBean是遵循特殊写法的java类 它通常具有如下特点: 1.这个java类必须具有一个无参的构造函数 2.属性必须私有化 3.私有化必须通过public类暴露给其他程序,而且方法的命名必须遵 ...

  5. js-jquery-003-条形码-二维码【QR码】

    一.基本使用 插件地址:https://github.com/jeromeetienne/jquery-qrcode 1.首先在页面中加入jquery库文件和qrcode插件. <script ...

  6. Linux下修改.bash_profile 文件改变PATH变量的值

    Linux中含有两个重要的文件 /etc/profile和$HOME/.bash_profile 每当系统登陆时都要读取这两个文件,用来初始化系统所用到的变量,其中/etc/profile是超级用户所 ...

  7. abap 中modify 的使用

    1.modify table itab from wa Transporting f1 f2 ... 表示表itab中符合工作区wa 中关键字的一条数据的 f1 f2字段会被wa中对应的字段值更新. ...

  8. mysql监控优化(一)连接数和缓存

    一.mysql的连接数 MYSQL数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,连接数少的话,在大并发下连接数会不够用,会有很多线程在等待其他连接释放 ...

  9. SDUT3146:Integer division 2(整数划分区间dp)

    题目:传送门 题目描述 This is a very simple problem, just like previous one. You are given a postive integer n ...

  10. 使用Spring注解获取配置文件信息

    需要加载的配置文件内容(resource.properties): #FTP相关配置 #FTP的IP地址 FTP_ADDRESS=192.168.1.121 FTP_PORT=21 FTP_USERN ...