Windows+Nginx+Tomcat搭建负载均衡和集群环境同时实现session共享(一)

摘要:随着网站的访问量越来越多,所以就考虑给网站增加服务器了,现在比较流行的做法就是给网站做集群环境,下面我把我做的过程记录一下,方便日后查看,同时也希望可以帮助到有需要的朋友!

一:首先是环境:

1.jdk 1.6.0_45

2.tomcat 6.0.44

3.nginx 1.8.0


二:jdk,tomcat,nginx的安装:

1.jdk的安装请参考:点击打开链接

2.nginx的安装步骤

(1).首先下载nginx的windows版本,我这里下载的nginx 1.8.0版本,下载地址:点击打开链接

(2).下载完成后,直接解压到一个没有中文的路径下,我这里是解压到:D:\server\nginx-1.8.0

(3).通过dos命令进入nginx-1.8.0目录下启动nginx即可

(4)Windows下操作Nginx命令

1.启动
nginx.exe
start nginx
2.停止
nginx -s stop
nginx -s quit

stop表示立即停止nginx,不保存相关信息

quit表示正常退出nginx,并保存相关信息

3.重启
nginx -s reload
重启(因为改变了配置,需要重启)

3.tomcat的安装(直接到tomcat的官网下载解压缩版的即可)


三:把下载下载的tomcat分别复制3个,一共是个,其中三个做qdksDemo的集群环境用,另外三个做qdkyDemo的集群环境用,具体截图如下:


复制完成后,要分别修改每个tomcat的端口和session共享的配置,具体的配置如下:

1.第一处要修改的端口号

<Server port="8005" shutdown="SHUTDOWN">
2.第二处要修改的端口号

<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
3.第三处要修改的端口号

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
4.第四处要增加的session共享配置,这个可以查看tomcat的官方文档,里面有配置直接复制到server.xml中就可以,地址: http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"channelSendOptions="6"><Manager className="org.apache.catalina.ha.session.BackupManager"expireSessionsOnShutdown="false"notifyListenersOnReplication="true"mapSendOptions="6"/><!--<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="5000"selectorTimeout="100"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.MessageDispatch15Interceptor"/><Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/></Channel><Valve className="org.apache.catalina.ha.tcp.ReplicationValve"filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/><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>

这样一个tomcat的配置文件就修改完成了,其他5个tomcat的配置和这个流程一样,只是对于的端口不能一样


四:nginx的配置:

1.首先在nginx的目录下新建一个新的文件夹来存放不同项目之间的集群配置文件,如下图:


2.在manyvhost文件夹里面新建一个配置文件,分别对于两个项目的集群配置,如下图:


3.qdks.conf和qdky.conf的配置内容如下:

(1).qdks.conf

upstream  qdks  {  server   localhost:8081 weight=1;  server   localhost:8082 weight=1;server   localhost:8083 weight=1;
}
server {listen       8086;server_name 192.168.1.103;error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}root /data/projects/ycp/bill;# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"#    location ~ ^/static/assets/(.*)$#    {#alias /data/projects/payment/web/public/assets/$1;#      access_log off;#      #expires 3d;#    }location / {index  index.html index.htm  index.jsp;}location ~ .* {# proxy_pass_header Server;proxy_set_header Host $http_host;# proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Scheme $scheme;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;add_header Pragma "no-cache";proxy_pass http://qdks;}rewrite ^/admin/?$ /admin/login redirect;# for rewriterewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;#redirect to mobile wap#rewrite ^$ /m redirect;#rewrite ^/$ /mobile/user redirect;}

(2).qdky.conf

upstream  qdky  {  server localhost:7081 weight=1;  server localhost:7082 weight=1;server localhost:7083 weight=1;
}
server {listen       7086;server_name  192.168.1.103;error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}root /data/projects/ycp/bill;# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"#    location ~ ^/static/assets/(.*)$#    {#alias /data/projects/payment/web/public/assets/$1;#      access_log off;#      #expires 3d;#    }location / {index  index.html index.htm  index.jsp;}location ~ .* {# proxy_pass_header Server;proxy_set_header Host $http_host;# proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Scheme $scheme;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;add_header Pragma "no-cache";proxy_pass http://qdky;}rewrite ^/admin/?$ /admin/login redirect;# for rewriterewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;#redirect to mobile wap#rewrite ^$ /m redirect;#rewrite ^/$ /mobile/user redirect;}

这两个配置文件就是我当前环境下的配置文件,其中qdks的项目的端口号8086,下面对应三个tomcat的端口为:8081,8082,8083;qdky的项目的端口号7086,下面对应三个tomcat的端口号为:7081,7082,7083


4.最后在nginx的conf目录下的nginx.conf核心配置文件中引入上面的两个项目的集群配置,如下:

#user  nobody;
worker_processes  1;error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;pid        logs/nginx.pid;events {worker_connections  5000;
}http {include       mime.types;default_type  application/octet-stream;#隐藏nginx的版本号server_tokens off;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;#access日志存放路径和格式access_log  logs/access.log;#提升文件传输性能sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;#keepalive_timeout  65;#设置客户端能够请求的单个文件大小client_max_body_size 300m;keepalive_timeout  75;#proxy参数#跟后端服务器连接的超时时间,发起握手等候响应超时时间proxy_connect_timeout 5;#连接成功后,等候后端服务器的响应时间proxy_read_timeout 600;#后端服务器数据回传时间proxy_send_timeout 600;#代理请求缓存区proxy_buffer_size 16k;#同上,告诉nginx保存单个用的几个buffer、最大用多空间proxy_buffers 4 64k;#如果系统很忙时可以申请更大的proxy_buffers,官方推荐*2proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;#gzip  on;#开启压缩功能gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.1; gzip_comp_level 2;#压缩级别从低到高1-9gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;#qdksDemo和qdkyDemo项目的虚拟目录(用绝对路径表示)include  D:/server/nginx-1.8.0/manyvhost/qdks.conf;include  D:/server/nginx-1.8.0/manyvhost/qdky.conf;# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       80;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443;#    server_name  localhost;#    ssl                  on;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_timeout  5m;#    ssl_protocols  SSLv2 SSLv3 TLSv1;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers   on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

五:分别部署对应的项目,来测试我们上面配置的集群环境是否正常

1.下载对应的项目源代码,下载后解压到你的本地workspace下面,我的路径是:D:\workspace_qdexam\qdksDemo和D:\workspace_qdexam\qdkyDemo

qdksDemo的下载地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187453

qdkyDemo的下载地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187455


2.分别部署到对应的tomcat下面,这里我采用的是直接映射路径的方法

(1).qdksDemo的部署方法是,在对应的三个tomcat的conf\Catalina\localhost目录下新建一个配置文件qdksDemo.xml,该配置文件的内容如下:

<Context    path="/qdksDemo"   docBase="D:\workspace_qdexam\qdksDemo\WebContent"     debug="0"    privileged="true">      </Context>

(2).qdkyDemo的部署方法是,在对应的三个tomat的conf\Catalina\localhost目录下新建一个配置文件qdkyDemo.xml,该配置文件的内容如下:

<Context path="/qdkyDemo"   docBase="D:\workspace_qdexam\qdkyDemo\WebContent" debug="0" privileged="true"></Context>


3.分别启动这6个tomcat,如下图:


4.启动nginx,如下图:


5.最后通过浏览器访问这两个项目,如下图:



分别点击刷新,你会发现上面的实际访问端口会变,但是下面的Session ID是不变的,这就说明基于nginx和tocmat的集群搭建成功了!


最后我把我的tomcat和nginx都打包上传,供大家参考!

tomcat的配置压缩包下载地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187521

nginx的配置压缩包下载地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187527




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/443420.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android开发中调用Spring CXF整合发布的WebService接口为什么抛出异常错误?

摘要&#xff1a;最近在协助同事搞Android调用WebService接口&#xff0c;再测试的过程中发现老师报错&#xff0c;经过baidu&#xff0c;google&#xff0c;终于解决了&#xff0c;现在记录一下&#xff1a; 一&#xff1a;错误信息&#xff1a; 2015-10-28 18:50:39 org.apac…

Eclipse,MyEclipse 安装SVN插件

摘要&#xff1a;MyEclipse10.5安装SVN插件最简单的方式&#xff1a; 一&#xff1a;先到SVN官网下载对应版本的SVN插件包&#xff0c;我这里下载的site-1.10.10版本 下载地址&#xff1a;http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID2240 二&#xf…

MyEclipse 10.5 安装SVN插件

摘要&#xff1a;MyEclipse10.5安装SVN插件最简单的方式&#xff1a; 一&#xff1a;先到SVN官网下载对应版本的SVN插件包&#xff0c;我这里下载的site-1.10.10版本 下载地址&#xff1a;http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID2240 二&#xf…

PHP获取服务器端的相关信息

摘要&#xff1a;PHP获取服务端端的相关信息 一&#xff1a;代码&#xff1a; <!DOCTYPE html> <html> <head><title>第一个PHP程序(获取服务器信息)</title><meta http-equiv"content-type" content"text/html"; charse…

log4j 标准配置模板:

摘要&#xff1a;log4j在开发中是少不了的&#xff0c;下面贴上我的log4j的配置&#xff0c;该配置文件实现的结果是&#xff0c;每天会生成一个日子文件到指定的目录下&#xff0c;这样方便我们随时查看日志信息&#xff01; log4j.propertieslog4j.rootLoggerINFO,CONSOLE,A …

Sql Server 2005 分页

摘要&#xff1a;Sql Server 分页查询sql! Sql Server 2005分页查询数据&#xff0c;适用于手机端和服务器端交互 一&#xff1a;Dao代码&#xff1a; /*** 分页查询研讯分类下的新闻列表*/Overridepublic List<TblNews> getListByYunXunNewId(int cId, int start, int…

Spring+Hibernate+SpringMVC+MySql实现配置多个数据源!

摘要&#xff1a;在日常的工作中&#xff0c;很多时候我们进行项目开发的时候&#xff0c;一个项目有可能不止用到一个数据源&#xff0c;为了提高数据库的水平伸缩性&#xff0c;需要对多个数据库实例进行管理&#xff0c;需要配置多数据源! 一&#xff1a;代码截图&#xff…

Java之Base64实现文件和字符串之间的转换

摘要&#xff1a;Java通过Base64加密解密实现文件和字符串之间的转换&#xff01; Base64.java package com.qdexam.util;import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputS…

app中使用用户名/邮箱/手机号登录的思路分析

摘要&#xff1a;app中使用用户名/邮箱/手机号登录的思路分析 1.客户端 客户端需要传递2个参数 account 代表&#xff1a;用户名/邮箱/手机号 password 代表&#xff1a;密码这里问题来了&#xff0c;那么如何判断识别用户输入的是用户名还是邮箱&#xff0c;还是手机号就需要…

java实现ListObject转List实体类,java实现Object转对象,java实现Object转实体类

摘要&#xff1a;在java开发中&#xff0c;我们常常会遇到Object转对象的情况&#xff0c;最近我就遇到了这个问题&#xff0c;现在记录一下&#xff0c;方便日后自己查看复习&#xff01; 一&#xff1a;查询Object类型的集合对象的方法如下&#xff1a; List topicList top…

MySql实现分页查询的SQL,mysql实现分页查询的sql语句

摘要&#xff1a;MySql数据库实现分页查询的SQL语句写法&#xff01; 一&#xff1a;分页需求&#xff1a; 客户端通过传递start(页码)&#xff0c;limit(每页显示的条数)两个参数去分页查询数据库表中的数据&#xff0c;那我们知道MySql数据库提供了分页的函数limit m,n&…

CentOS下安装Memcached,Linux下安装Memcached,centos下安装memcached,linux下安装memcached...

摘要&#xff1a;最近在看《大型分布式网站架构设计与实践》这本书&#xff0c;把其中的学习过程记录一下&#xff0c;以便日后复习。 一&#xff1a;Memcached简单介绍和描述&#xff1a; Memcached 是一个高性能的分布式内存对象缓存系统&#xff0c;用于动态Web应用以减轻数…

Spring,ehcache整合报错

摘要&#xff1a;在做Spring整合ehcache配置的时候出现了下面的错误&#xff0c;提示如下&#xff1a;java.lang.ClassNotFoundException: org.springframework.cache.ehcache.EhCacheManagerFactoryBean&#xff0c;经过检查发现是因为org.springframework.cache.ehcache.EhCa…

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool/BasePoolableObjectFactory

摘要&#xff1a;异常信息解决过程记录 一&#xff1a;异常信息&#xff1a; Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool/BasePoolableObjectFactoryat java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(Cla…

一个微信公众号接入另一个微信公众号的内容

摘要&#xff1a;近日来了这么一个需求&#xff0c;就是客户想在他们的微信公众号里面直接接入我们微信公众号的文章&#xff0c;于是我便开始baidu&#xff0c;google&#xff0c;最后发现了一个新的搜索引擎&#xff1a;“搜狗微信搜索”&#xff0c;该搜索引擎是最近才上线的…

SQL Server将数据库中的表复制到另一个数据库

在使用SqlServer的过程中&#xff0c;我们可能需要将表从一个数据库复制到另一个数据库中&#xff0c;今天&#xff0c;小编为大家介绍这种操作的具体方法及步骤。 一&#xff1a;复制表结构&#xff1a; 1.首先&#xff0c;打开并连接Sql Server&#xff0c;在源数据库Sourc…

SQL Server cast() 批量更新列内容

摘要&#xff1a;有这样一个需求&#xff0c;需要把数据表中的列批量更新一下&#xff0c;给后面的数字都增加20&#xff0c;如下图&#xff1a; 正确的sql如下&#xff1a; update tbl_papers_test set name 执业护士考试儿科护理学cast((cast(substring(name,12,datalength(…

SQL Server 批量更新字段值为ROW_NUMBER()+列名称

摘要&#xff1a;有这样一个需求&#xff0c;需要把数据表中的列名称的数据按行号重新排序并更新一下&#xff0c;这里用到了ROW_NUMBER()函数。 一&#xff1a;需求如下图&#xff1a; 二&#xff1a;通过执行如下sql语句实现了上面的需求&#xff0c;如下&#xff1a; update…

Maven+Tomcat的热部署方案

摘要&#xff1a;在日常的开发中&#xff0c;热部署是个非常重要的事&#xff0c;下面就介绍下Maven&#xff0c;Tomcat如何实现热部署! 第一步&#xff1a;配置Tomcat的登陆的用户名与密码 E:\apache-tomcat-7.0.68\conf\tomcat-users.xml 从第35行开始配置 <role rolenam…

readonly与disabled属性在css中区别

论readonly与disabled在css中区别 之前在项目中遇到的一个问题&#xff0c;现在想把它记录下来。 当项目需求上需要在文本框中禁用输入某个值的时候&#xff0c;我第一个反应是用了disabled&#xff0c;因为之前一直接触的都是disabled。但当我需要提交FROM表单的时候&#xf…