q789321 发表于 2015-11-18 15:10:14

Nginx1.7.4+Tomcat7+memcached集群环境搭建

  这几天研究了一下Nginx和tomcat的集群环境搭建,同时支持HTTP请求和HTTPS请求,研究了一下,整理一下,分享给大家。还是有很多不足的地方,大家发现问题,希望多多指正。话不多说,上代码~
  因为研究的条件有限,我是在本机上搭建两个Tomcat进行模拟的。
  一、环境准备
  1.Tomcat7:http://tomcat.apache.org/download-70.cgi
  2.Nginx1.7.4:http://nginx.org/en/download.html
  3.Memcached:http://www.newasp.net/soft/63735.html 实在没有找到别的太好的下载地址~
  4.必备的JAR包:
  


[*]memcached-session-manager-1.6.5.jar、memcached-session-manager-tc7-1.6.5(tomcat6 请选tc6)、javolution-5.4.3.1.jar、msm-javolution-serializer-1.6.5.jar、spymemcached-2.10.3.jar

这些JAR都可以在:http://code.google.com/p/memcached-session-manager/downloads/list 这里下载到,网速慢,FQ吧~

[*]couchbase-client-1.4.4.jar

下载地址:http://packages.couchbase.com/clients/java/1.4.4/Couchbase-Java-Client-1.4.4.zip 这个JAR在这个zip文件中


二、Tomcat配置

[*]解压并复制Tomcat,分别为apache-tomcat-7.0.55-01和apache-tomcat-7.0.55-02
[*]修改server.xml文件,同时添加HTTPS支持和Tomcat双开的配置

apache-tomcat-7.0.55-01配置
<?xml version='1.0' encoding='utf-8'?>
<span style=&quot;color:#ff0000;&quot;><Server port=&quot;8005&quot; shutdown=&quot;SHUTDOWN&quot;></span>
<Listener className=&quot;org.apache.catalina.core.AprLifecycleListener&quot; SSLEngine=&quot;on&quot; />
<Listener className=&quot;org.apache.catalina.core.JasperListener&quot; />
<Listener className=&quot;org.apache.catalina.core.JreMemoryLeakPreventionListener&quot; />
<Listener className=&quot;org.apache.catalina.mbeans.GlobalResourcesLifecycleListener&quot; />
<Listener className=&quot;org.apache.catalina.core.ThreadLocalLeakPreventionListener&quot; />
<GlobalNamingResources>
<Resource name=&quot;UserDatabase&quot; auth=&quot;Container&quot;
type=&quot;org.apache.catalina.UserDatabase&quot;
description=&quot;User database that can be updated and saved&quot;
factory=&quot;org.apache.catalina.users.MemoryUserDatabaseFactory&quot;
pathname=&quot;conf/tomcat-users.xml&quot; />
</GlobalNamingResources>
<Service name=&quot;Catalina&quot;>
<span style=&quot;color:#ff0000;&quot;><Connector port=&quot;8080&quot; protocol=&quot;HTTP/1.1&quot;
connectionTimeout=&quot;20000&quot;
redirectPort=&quot;8443&quot; /></span>
<span style=&quot;color:#ff0000;&quot;> <Connector port=&quot;8443&quot; protocol=&quot;HTTP/1.1&quot; SSLEnabled=&quot;true&quot; SSLCertificateFile=&quot;E:/tomcat.crt&quot; SSLCertificateKeyFile=&quot;E:/tomcat.key&quot;
maxThreads=&quot;150&quot; scheme=&quot;https&quot; secure=&quot;true&quot;
clientAuth=&quot;false&quot; keystoreFile=&quot;E:/tomcat.keystore&quot; keystorePass=&quot;123456&quot; sslProtocol=&quot;TLS&quot; />
<Connector port=&quot;8009&quot; protocol=&quot;AJP/1.3&quot; redirectPort=&quot;8443&quot; /></span>
<Engine name=&quot;Catalina&quot; jvmRoute=&quot;<span style=&quot;color:#ff0000;&quot;>tomcat1</span>&quot; defaultHost=&quot;localhost&quot;>
<Realm className=&quot;org.apache.catalina.realm.LockOutRealm&quot;>
<Realm className=&quot;org.apache.catalina.realm.UserDatabaseRealm&quot;
resourceName=&quot;UserDatabase&quot;/>
</Realm>
<Host name=&quot;localhost&quot;appBase=&quot;webapps&quot;
unpackWARs=&quot;true&quot; autoDeploy=&quot;true&quot;>
<Valve className=&quot;org.apache.catalina.valves.AccessLogValve&quot; directory=&quot;logs&quot;
prefix=&quot;localhost_access_log.&quot; suffix=&quot;.txt&quot;
pattern=&quot;%h %l %u %t &quot;%r&quot; %s %b&quot; />
</Host>
</Engine>
</Service>
</Server>


apache-tomcat-7.0.55-02配置
<?xml version='1.0' encoding='utf-8'?>
<span style=&quot;color:#ff0000;&quot;><Server port=&quot;8006&quot; shutdown=&quot;SHUTDOWN&quot;></span>
<Listener className=&quot;org.apache.catalina.core.AprLifecycleListener&quot; SSLEngine=&quot;on&quot; />
<Listener className=&quot;org.apache.catalina.core.JasperListener&quot; />
<Listener className=&quot;org.apache.catalina.core.JreMemoryLeakPreventionListener&quot; />
<Listener className=&quot;org.apache.catalina.mbeans.GlobalResourcesLifecycleListener&quot; />
<Listener className=&quot;org.apache.catalina.core.ThreadLocalLeakPreventionListener&quot; />
<GlobalNamingResources>
<Resource name=&quot;UserDatabase&quot; auth=&quot;Container&quot;
type=&quot;org.apache.catalina.UserDatabase&quot;
description=&quot;User database that can be updated and saved&quot;
factory=&quot;org.apache.catalina.users.MemoryUserDatabaseFactory&quot;
pathname=&quot;conf/tomcat-users.xml&quot; />
</GlobalNamingResources>
<Service name=&quot;Catalina&quot;>
<span style=&quot;color:#ff0000;&quot;> <Connector port=&quot;</span><span style=&quot;color:#ff0000;background-color: rgb(255, 153, 0);&quot;>11080</span><span style=&quot;color:#ff0000;&quot;>&quot; protocol=&quot;HTTP/1.1&quot;
connectionTimeout=&quot;20000&quot;
redirectPort=&quot;<span style=&quot;background-color: rgb(255, 153, 0);&quot;>11443</span>&quot; />
<Connector port=&quot;<span style=&quot;background-color: rgb(255, 153, 0);&quot;>11443</span>&quot; protocol=&quot;HTTP/1.1&quot; SSLEnabled=&quot;true&quot; SSLCertificateFile=&quot;E:/tomcat.crt&quot; SSLCertificateKeyFile=&quot;E:/tomcat.key&quot;
maxThreads=&quot;150&quot; scheme=&quot;https&quot; secure=&quot;true&quot;
clientAuth=&quot;false&quot; keystoreFile=&quot;E:/tomcat.keystore&quot; keystorePass=&quot;123456&quot; sslProtocol=&quot;TLS&quot; />
<Connector port=&quot;<span style=&quot;background-color: rgb(255, 153, 0);&quot;>11009</span>&quot; protocol=&quot;AJP/1.3&quot; redirectPort=&quot;11443&quot; /></span>
<Engine name=&quot;Catalina&quot; jvmRoute=&quot;<span style=&quot;color:#ff0000;background-color: rgb(255, 153, 0);&quot;>tomcat2</span>&quot; defaultHost=&quot;localhost&quot;>
<Realm className=&quot;org.apache.catalina.realm.LockOutRealm&quot;>
<Realm className=&quot;org.apache.catalina.realm.UserDatabaseRealm&quot;
resourceName=&quot;UserDatabase&quot;/>
</Realm>
<Host name=&quot;localhost&quot;appBase=&quot;webapps&quot;
unpackWARs=&quot;true&quot; autoDeploy=&quot;true&quot;>
<Valve className=&quot;org.apache.catalina.valves.AccessLogValve&quot; directory=&quot;logs&quot;
prefix=&quot;localhost_access_log.&quot; suffix=&quot;.txt&quot;
pattern=&quot;%h %l %u %t &quot;%r&quot; %s %b&quot; />
</Host>
</Engine>
</Service>
</Server>


三、Memcached配置


[*]将Memcached的安装包解压到目录下,文件夹重命名为X:/memcached
[*]以管理员身份进入X:/memcached,执行命令memcached.exe –d install,现在memcached已经作为系统服务可以随机启动,默认端口:11211
[*]下面介绍几个常用的Memcached命令:

查看memcached的服务是否已经启动:tasklist /fi &quot;imagename eq memcached.exe&quot;
memcached参数使用介绍

-p 监听的端口

-l 连接的IP地址, 默认是本机

-d start 启动memcached服务

-d restart 重起memcached服务

-d stop|shutdown 关闭正在运行的memcached服务

-d install 安装memcached服务

-d uninstall 卸载memcached服务

-u 以的身份运行 (仅在以root运行的时候有效)

-m 最大内存使用,单位MB。默认64MB

-M 内存耗尽时返回错误,而不是删除项

-c 最大同时连接数,默认是1024

-f 块大小增长因子,默认是1.25

-n 最小分配空间,key&#43;value&#43;flags默认是48

-h 显示帮助


4.Memcached的Tomcat配置【处理Session同步的问题】
首先,将之前下载的JAR包全部拷贝到两个Tomcat的lib下。
接着,配置两个Tomcat的context.xml的文件:在Context标签内加入如下内容
<Manager className=&quot;de.javakaffee.web.msm.MemcachedBackupSessionManager&quot;
memcachedNodes=&quot;n1:localhost:11211&quot;<!-- Memcached只有一个节点,现在这么写就OK -->
requestUriIgnorePattern=&quot;.*/.(png|gif|jpg|css|js)$&quot;
sessionBackupAsync=&quot;false&quot;
sessionBackupTimeout=&quot;100&quot;
transcoderFactoryClass=&quot;de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory&quot;
copyCollectionsForSerialization=&quot;false&quot;/>

四、Nginx的配置
1.将下载的Nginx压缩包解压到X:/Nginx目录下
2.启动Nginx,以管理员身份进入X:/Nginx目录,执行命令:startnginx.exe,屏幕会一闪而过
3.接着输入命令:tasklist /fi &quot;imagename eq nginx.exe&quot;,出现下面的内容就算是启动成功:

Nginx常用命令:
停止Nginx:nginx.exe -s quit

重启Nginx:nginx.exe -s reload

查看Nginx版本:nginx.exe -v
4.修改conf/nginx.conf文件:
在http{}中,添加如下内容:
#设定HTTP负载均衡的服务器列表
upstream <span style=&quot;color:#3333ff;&quot;>tomcat_server</span>{
server localhost:11080 weight=1;#第一台机器
server localhost:8080 weight=1;#第二台机器
}server {
      listen       80;
      server_namelocalhost;

      #charset koi8-r;

      #access_loglogs/host.access.logmain;

      location / {
            root   html;
            indexindex.html index.htm;
<span style=&quot;white-space:pre&quot;></span>    proxy_pass http://<span style=&quot;color:#3333ff;&quot;>tomcat_server</span>;
      }


#设定HTTPS负载均衡的服务器列表
upstream tomcat_ssl_server{
server localhost:11443 weight=1;#第一台机器
server localhost:8443 weight=1;#第二台机器
}server {
      listen       443 ssl;
      server_namelocalhost;

      ssl_certificate      E:\\tomcat.crt;
      ssl_certificate_keyE:\\tomcat.key;

<span style=&quot;white-space:pre&quot;></span>#The cache and other modules which require shared memory support do not work
<span style=&quot;white-space:pre&quot;></span>#on Windows Vista and later versions due to address space layout randomization being enabled in these Windows versions.
      #ssl_session_cache    shared:SSL:1m;
      ssl_session_timeout5m;

      ssl_ciphersHIGH:!aNULL:!MD5;
      ssl_prefer_server_cipherson;

      location / {
            root   html;
            indexindex.html index.htm;
<span style=&quot;white-space:pre&quot;></span>proxy_pass https://tomcat_ssl_server;
      }
    }


proxy_pass表示代理主机,upstream tomcat_server{……}配置中配置了代理主机处理请求的两个服务。

上述配置表示:Nginx监听到http://locahost:80 https://localhost:443的请求之后,转发到代理tomcat_server中的两个服务中的一个,有它们来处理请求。weight表示被分配到权重,weight值越大,处理请求的机会越多。


五、测试
在这里我们主要测试的是,Tomcat的Session同步问题,编写JSP页面,获取到Session的ID
<%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;pageEncoding=&quot;UTF-8&quot;%>
<html>
<body>
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println(&quot;apache-tomcat-7.0.55-02&quot;);
%>
<%=(String)session.getAttribute(&quot;user&quot;)%>
</body>
</html>HTTP请求:


HTTPS请求:

  
  

版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Nginx1.7.4+Tomcat7+memcached集群环境搭建