模拟tomcat集群,使用nginx反向代理,redis实现数据共享

总不能还没努力就像生活妥协吧。----摘自知乎

模拟tomcat集群

模拟tomcat集群,在这里也就添加两个tomcat了,多了原理也是一样的。
把tomcat复制了两份,想要同时启动两个tomcat,就要先把他们的端口号改成不一样的,不然端口占用。

要改端口的地方有三个,都在tomcat底下的conf底下的server.xml文件中:
注意:改的都是配置中的port属性,其他属性不动

  • 第一个
<Server port="8005" shutdown="SHUTDOWN">
  • 第二个
 <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
  • 第三个
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

改完了之后,就可以尝试启动了。分别在两个cmd中将位置切换到tomcat底下的bin目录中,输入startup.bat回车,就可以启动tomcat。此时在浏览器中打开相应端口号的页面了。
至此,模拟tomcat集群就完了,就这么简单。

使用nginx反向代理

  • 首先下载nginx
  • 修改配置文件
    在nginx中conf下的nginx.conf文件进行配置,这里保存一份自己的配置.
    注意:里面的8081和8082都是上面配置tomcat时修改的,对应的。

#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 test {
		server localhost:8081;
		server localhost:8082;
	}

	
	    
	#服务器集群配置
    upstream yuxi.com{#服务器集群名称

	server	127.0.0.1:8081 weight=1;#服务器配置,weight是权重,权重越大,分配的概率越大。

	server	127.0.0.1:8082 weight=2;

    }
	

	#当前的nginx配置
    server {
        listen       80;
        server_name localhost;#当前服务的域名.如果是localhost:8080的请求,则交给yuxi.com的集群来处理。

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
			
			proxy_pass http://yuxi.com;#与服务器集群的名称一致
			proxy_redirect default;
        }

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

}

配置完成后,就可以查看你的IP地址,然后在浏览器中进行访问,就会按照你配置的权重概率的进行页面展示。
至此,nginx也配置完了

使用redis实现session共享

  • 第一步,先分别往tomcat中的lib下添加三个jar包
    在这里插入图片描述

  • 第二步,修改tomcat中conf底下context.xml文件
    在该文件的最后一行(要在标签内)添加如下信息:

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
	<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
  • 第三步,将tomcat---->webapps---->root---->index.jsp页面中的session改为true
<%@ page session="true" %>
  • 第四步,将装好的redis启动

至此,在浏览器中访问nginx的时候,他代理给两个tomcat服务器,这两个tomcat就用的是同一个session,实现了数据共享。

这个只是最为简单的一个做法,没有深入的进行一些操作,以后再说吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值