您的位置:首页 > 运维架构 > Tomcat

Apache+Tomcat+Terracotta+Ehcache负载均衡和分布式会话,缓存框架搭建

2015-11-28 23:28 591 查看
一.Apache2.4

1.下载安装

使用的版本是2.4版本。

登录apache 官网下载apache http server。默认没有linux版本,但官网提供了下载windows版本的地址。根据引导一步步安装即可。有使用手册。不懂可仔细阅读。

2.配置

主要涉及到3个文件,httpd.conf,mod_jk.conf,worker.properties。后两个文件默认是没有的需要自己建。都在conf目录下。

新建mod_jk.conf,内容如下:

#加载mod_jk Module

 

LoadModule jk_module modules/mod_jk.so

 

#指定 workers.properties文件路径

 

JkWorkersFile conf/worker.properties

 

#指定哪些请求交给tomcat处理,"loadbalancer"为在workers.propertise里指定的负载分配控制器名

 

JkMount /*.* loadbalancer

 

在httpd.conf文件最后添加如下代码:

 

#添加整合tomcat配置

include "C:\Apache24\conf\mod_jk.conf"

 

新建worker.properties文件,内容如下:

 

worker.list=loadbalancer,tomcat2,tomcat1

 

# 定义第一个集群节点

worker.tomcat1.port=10109

worker.tomcat1.host=192.168.1.101

worker.tomcat1.type=ajp13

worker.tomcat1.lbfactor=1

worker.tomcat1.socket_keepalive=1

worker.tomcat1.socket_timeout=300 

 

# 定义第二个集群节点

worker.tomcat2.port=10209

worker.tomcat2.host=192.168.1.102

worker.tomcat2.type=ajp13

worker.tomcat2.lbfactor=1

worker.tomcat2.socket_keepalive=1

worker.tomcat2.socket_timeout=300

 

 

# 负载均衡行为

worker.loadbalancer.type=lb

worker.loadbalancer.balance_workers=tomcat2,tomcat1

worker.loadbalancer.sticky_session=0

 

    Apache在启动之后会在C:\Apache24\logs下生成mod_jk.log日志信息。Tomcat不能正常工作可以在这里查看日志。

二、Terracotta3.7.7

1.安装之后在bin下建立tc_config.xml配置文件内容如下:

 

<?xml version="1.0" encoding="UTF-8" ?>  

 <tc:tc-config xmlns:tc="http://www.terracotta.org/config">  

   <servers>  

     <!-- Sets where the Terracotta server can be found. Replace the value of host with the server's IP address. -->   

     <server host="192.168.1.100" name="Server1">  

       <data>%(user.home)/terracotta/server-data</data>  

       <logs>%(user.home)/terracotta/server-logs</logs>  

     </server>  

     <!-- If using a standby Terracotta server, also referred to as an ACTIVE-PASSIVE configuration, add the second server here. -->   

       

   <server host="192.168.1.131" name="Server2">   

     <data>%(user.home)/terracotta/server-data</data>  

     <logs>%(user.home)/terracotta/server-logs</logs>  

   </server>   

   <!-- If using more than one server, add an <ha>   

   section. -->  

    

   <ha>   

   <mode>networked-active-passive</mode>  

   <networked-active-passive>  

     <election-time>5</election-time>  

   </networked-active-passive>  

 </ha>  

  

  </servers>  

 <!-- Sets where the generated client logs are saved on clients. Note that the exact location of Terracotta logs on client machines may vary based on the value of user.home and the local disk layout. -->   

   <clients>  

          <logs>%(user.home)/terracotta/client-logs</logs>  

   </clients>  

 </tc:tc-config>

 

其中,%(user.home)/terracotta是terracotta的安装目录。

 

启动terracotta使用bin目录下start_tc_server.bat,dev_console.bat可查看terracotta集群工作情况。

 

三、apache整合tomcat7

目的:负载均衡

Tomcat配置文件server.xml有三处需要修改。

1.ajp端口要与apache worker.properties中的一致

2.http1.1端口每个tomcat不同

3.有一个engine标签,添加jvmRoute=”worker.name”,其中worker.name就是apache worker.properties文件中的worker名称,tomcat1,tomcat2等等。

 

四、terracotta整合tomcat7

目的:分布式会话

1.将terracotta common目录中的terracotta-toolkit-1.6-runtime-5.7.0.jar和sessions目录中的terracotta-session-1.3.7.jar拷贝到tomcat lib目录下

2.修改tomcat 的context.xml 添加如下内容:

<Valve className="org.terracotta.session.TerracottaTomcat70xSessionValve" tcConfigUrl="terracotta.server1.ip:9510,terracotta.server2.ip:9510"/>

如上所示,集群中有几个terracotta就添加几个地址,逗号分割。

 

五、terracotta整个ehcache

目的:分布式缓存

在项目的src目录下新建ehcache.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>  

  

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

         xsi:noNamespaceSchemaLocation="ehcache.xsd"  

         updateCheck="true" monitoring="off"  

         dynamicConfig="true">  

  

    

    <cacheManagerEventListenerFactory class="" properties=""/>  

  

    <terracottaConfig url="192.168.1.101:9510,192.168.1.102:9510"/> <!--terracottaؾϱǷƤ׃,Ĭɏ׋ࠚΪ9510,נٶؾϱǷԃ,ؖٴ  -->  

  

    <defaultCache  

           maxEntriesLocalHeap="0"  

           eternal="false"  

           overflowToDisk="false"  

           timeToIdleSeconds="30"  

           timeToLiveSeconds="60">  

           <terracotta clustered="true" /> <!--使用集群-->  

    </defaultCache>  

      

    <cache name="demoCache"   

            maxBytesLocalHeap="200M"  

            eternal="false"  

            overflowToDisk="false"  

            timeToIdleSeconds="30"  

            timeToLiveSeconds="60"  

            memoryStoreEvictionPolicy="LFU"  

            maxElementsOnDisk="100000"  

            >  

    <terracotta clustered="true" />   

    </cache>  

  

</ehcache> 

 

如上图所示:

//配置terracotta服务器集群

<terracottaConfig url="192.168.1.101:9510,192.168.1.102:9510"/> 

//使用集群缓存

<terracotta clustered="true" /> <!--使用集群-->  

注意: 所有需要缓存的对象都需要实现serializable接口。默认的缓存方式就是序列化。具体暂时没有深究。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: