`
sxdtzhaoxinguo
  • 浏览: 228336 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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 rewrite
	rewrite ^/(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 rewrite
	rewrite ^/(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,官方推荐*2
    proxy_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-9
    gzip_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




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics