Tomcat集群部署(nginx)

1.nginx + tomcat集群部署

1、下载Tomcat

1)两个Tomcat修改不同的端口号

(1)端口号

tomcat1 :端口号8086

tomcat2:端口号8089

 

2、下载nginx并安装

1)下载:https://nginx.org/en/download.html 

2)安装包直接解压

3)启动等相关命令

nginx端口默认80。启动后查看是否存在nginx.exe的进程。若启动失败,可能是端口冲突,可再nginx.conf中修改端口号

cmd命令进入安装文件;

1、启动:

C:\server\nginx-1.0.2>start nginx

或

C:\server\nginx-1.0.2>nginx.exe

注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。

2、停止:

C:\server\nginx-1.0.2>nginx.exe -s stop

或

C:\server\nginx-1.0.2>nginx.exe -s quit

注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。

3、重新载入Nginx:

C:\server\nginx-1.0.2>nginx.exe -s reload

当配置信息修改,需要重新载入这些配置时使用此命令。

4、重新打开日志文件:

C:\server\nginx-1.0.2>nginx.exe -s reopen

5、查看Nginx版本:

C:\server\nginx-1.0.2>nginx -v



3、配置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  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream server_test{
  
		server 127.0.0.1:8086;
		server 127.0.0.1:8089;
		ip_hash;
    }

    server {
        listen       8088;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
	    proxy_pass http://server_test;
	    proxy_set_header Host  $host:$server_port;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_Forwarded_for;
            proxy_redirect off;

	    proxy_connect_timeout       1;
	    proxy_read_timeout          30;
	    proxy_send_timeout          30;

	   client_max_body_size 50m; 
	   client_body_buffer_size 256k; 
	   proxy_buffer_size 256k; 
	   proxy_buffers 4 256k; 
	   proxy_busy_buffers_size 256k; 
	   proxy_temp_file_write_size 256k; 
	   //proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; 
	    proxy_max_temp_file_size 128m; 
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

4、tomcat下放测试项目

 

Tomcat(Tomcat1跟Tomcat2都加)下添加测试项目

tomcat/webapps/路径下新建test文件夹,文件夹下添加index.jsp,代码如下

<%@ page contentType="text/html; charset=utf-8" %>  
<%@ page import="java.util.*" %>  
<html><head><title>Cluster Test</title></head>  
<body>
SessionID:<%=session.getId()%><BR>SessionIP:<%=request.getServerName()%>
<BR>SessionPort:<%=request.getServerPort()%>
<BR>这里是服务器1
<BR>tomcat端口是8086
</body>
</html>

5、启动Tomcat1.Tomcat2.nginx

访问nginx  http://127.0.0.1:8088/test/index.jsp即可。

刷新页面可以看到,轮流访问该Tomcat。

2.session共享

方式1:IP绑定 ip_hash 
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决集群部署环境下session共享的问题。

但是如果访问的Tomcat挂掉,访问该Tomcat的用户就会无法使用系统

nginx.conf文件下添加 ip_hash 
upstream server_test{
  
		server 127.0.0.1:8086;
		server 127.0.0.1:8089;
		ip_hash;
    }

方式2:Tomcat自带的cluster(并发量大不适用)

server.xml文件中<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 节点下添加以下内容。

Tomcat5的话直接将注释掉的节点放开就行,不要用下面的代码。

 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 这是原来的,现注释掉-->
      
	  <!--开始-->
	  
	  <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="4008"
				  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.MessageDispatch15Interceptor"/>
		</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.JvmRouteSessionIDBinderListener" />
		<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />
</Cluster>

需要在项目中建一个WEB-IN文件夹,里面新建一个web.xml文件,打开web.xml,并在web-app节点下添加<distributable/>节点。

<web-app>
 
<distributable/>
 
</web-app>

 方式3:memcache

1)下载memcache(以及相关jar包下载)

memcache以及jar包以及配置:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

memcache下载:https://www.runoob.com/memcached/window-install-memcached.html

jar包下载:https://mvnrepository.com/

2)解压安装

window下memcache安装

3)启动

1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached

2. 在终端(也即cmd命令界面)下输入‘c:\memcached\memcached.exe -d install’安装

3. 再输入:‘c:\memcached\memcached.exe -d start’启动。NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。

4)配置

Tomcat conf/context.xml文件内添加<Context>标签下添加一下内容

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"   
				memcachedNodes="n1:127.0.0.1:11211"   
				requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"   
				sessionBackupAsync="false"   
				sessionBackupTimeout="100"   
				transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"   
				copyCollectionsForSerialization="false" 
				sticky="false"/>     

3.redis(这个方式我测试出现了问题,没有深入研究)

参考文档https://www.cnblogs.com/notDog/p/5318686.html
1)下载

2)安装

3)jar包

下载地址:https://github.com/jcoleman/tomcat-redis-session-manager/downloads

下载地址:https://mvnrepository.com/

4)配置

打开Context.xml,添加

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> 
<ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
    host="localhost" 
    port="6379" 
    database="0"
    maxInactiveInterval="60" />

Tomcat集群Cluster配置
nginx+tomcat集群搭建

nginx参考链接:

Nginx+upstream针对后端服务器容错的配置说明

nginx反向代理后,jsp页面request.getServerPort()获取得端口号总是80解决方案

nginx一次请求,转发到多个地址,引起iphash的session不能共享问题​​​​​​​

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值