有两台服务器(tomcat),nginx配置了这两台服务器,现在需要将用户访问的网址进行登录验证,同一个用户不同的请求都可以进到首页,而不是因为不同的服务器,导致同一用户的请求退回到登陆页。
效果如图:


实现方式

1 基于Redis的Session共享,使用Spring Session

这一步可以查看我这篇文章:
如何保持session一致性

2 两个服务器8080和8081如何实现?

两个tomcat
两个tomcat如何同时启动?
修改其中一个配置文件server.xml,将里面三个端口号都 +1 ,如:

配置代码如下:

<?xml version="1.0" encoding="UTF-8"?><Server port="8006" shutdown="SHUTDOWN"><Listener className="org.apache.catalina.startup.VersionLoggerListener" /><!-- Security listener. Documentation at /docs/config/listeners.html<Listener className="org.apache.catalina.security.SecurityListener" />--><!--APR library loader. Documentation at /docs/apr.html --><Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /><!-- Prevent memory leaks due to use of particular java/javax APIs--><Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /><Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /><Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /><!-- Global JNDI resourcesDocumentation at /docs/jndi-resources-howto.html--><GlobalNamingResources><!-- Editable user database that can also be used byUserDatabaseRealm to authenticate users--><Resource name="UserDatabase" auth="Container"type="org.apache.catalina.UserDatabase"description="User database that can be updated and saved"factory="org.apache.catalina.users.MemoryUserDatabaseFactory"pathname="conf/tomcat-users.xml" /></GlobalNamingResources><!-- A "Service" is a collection of one or more "Connectors" that sharea single "Container" Note:  A "Service" is not itself a "Container",so you may not define subcomponents such as "Valves" at this level.Documentation at /docs/config/service.html--><Service name="Catalina"><Connector port="8081" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" /><!-- Define an AJP 1.3 Connector on port 8009 --><Connector port="8011" protocol="AJP/1.3" redirectPort="8443" /><!-- An Engine represents the entry point (within Catalina) that processesevery request.  The Engine implementation for Tomcat stand aloneanalyzes the HTTP headers included with the request, and passes themon to the appropriate Host (virtual host).Documentation at /docs/config/engine.html --><!-- You should set jvmRoute to support load-balancing via AJP ie :<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">--><Engine name="Catalina" defaultHost="localhost"><!--For clustering, please take a look at documentation at:/docs/cluster-howto.html  (simple how to)/docs/config/cluster.html (reference documentation) --><!--<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>--><!-- Use the LockOutRealm to prevent attempts to guess user passwordsvia a brute-force attack --><Realm className="org.apache.catalina.realm.LockOutRealm"><!-- This Realm uses the UserDatabase configured in the global JNDIresources under the key "UserDatabase".  Any editsthat are performed against this UserDatabase are immediatelyavailable for use by the Realm.  --><Realm className="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/></Realm><Host name="localhost"  appBase="webapps"unpackWARs="true" autoDeploy="true"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" /></Host></Engine></Service>
</Server>

3 如何使用nginx实现两台服务器的负载均衡

修改nginx配置

配置代码如下:


#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 lagouServer{server 127.0.0.1:8080;server 127.0.0.1:8081;}server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;proxy_pass http://lagouServer;}location /favicon.ico {}#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;#    }#}}

踩抗注意:
tomcat运行的项目要打成war包,将war包放到tomcat的webapps文件夹下即可!

如何用tomcat模拟集群,要求负载均衡(Nginx),并保持session一致性!相关推荐

  1. 结合Apache和Tomcat实现集群和负载均衡

    http://fableking.iteye.com/blog/360870 TomcatApacheJSP应用服务器Web  本文基本参考自 轻松实现Apache,Tomcat集群和负载均衡,经由实 ...

  2. Tomcat服务器集群与负载均衡实现

    一.前言 在单一的服务器上执行WEB应用程序有一些重大的问题,当网站成功建成并开始接受大量请求时,单一服务器终究无法满足需要处理的负荷量,所以就有点显得有点力不从心了.另外一个常见的问题是会产生单点故 ...

  3. Nginx+Tomcat集群与负载均衡

    Nginx+Tomcat集群与负载均衡 架构描述 前端一台nginx服务器做负载均衡器,后端放N台tomcat组成集群处理服务,通过nginx转发到后面(注:没做动静分离,静态动态全部都转给tomca ...

  4. Nginx实现tomcat集群进行负载均衡

    一.背景 随着业务量和用户数量的激增,单一的tomcat部署应用已经无法满足性能需求,而且对于每次发布项目期间服务不可用的问题也凸显,既然出现了这个问题,那么我们本文就借助nginx来完美的解决这个问 ...

  5. 实践中整理出tomcat集群和负载均衡

    实践中整理出tomcat集群和负载均衡 来源:http://macrochen.blogdriver.com/macrochen/1207263.html (一)环境说明 (1)服务器有4台,一台安装 ...

  6. 搭建服务器集群——Windows7系统中nginx与IIS服务器搭建集群实现负载均衡

    转载:https://www.cnblogs.com/xiongze520/p/10308720.html 分布式,集群,云计算机.大数据.负载均衡.高并发······当耳边响起这些词时,做为一个菜鸟 ...

  7. 使用LVS+TUN搭建集群实现负载均衡

    使用LVS+TUN搭建集群实现负载均衡 TUN模式的概述与工作原理 TUN模式服务概述:      IP Tunneling(IP隧道) --可以在不同地域,不同网段      Director分配请 ...

  8. 学习:双机热备、集群、负载均衡、SQL故障转移群集简单理解(转)

    双机热备.集群.负载均衡.SQL故障转移群集简单理解平常,大家常提到几个技术名词:双机热备.集群.负载均衡.SQL故障转移群集.这里,就我的理解,和大家简单探讨下,有不足或错误之处还请各位指出! 这些 ...

  9. FastDFS蛋疼的集群和负载均衡(十五)之lvs四层+Nginx七层负载均衡

    ###Interesting things lvs+nginx的拓扑图 准备环境,基于上一篇[Diary Report 2018-1-3]关于FastDFS蛋疼的集群和负载均衡(十三)之LVS-DR环 ...

  10. haproxy对mysql集群进行负载均衡

    haproxy对mysql集群进行负载均衡: 参考网址:https://www.cnblogs.com/jave1ove/p/5980053.html 注: mysql为集群模式,我使用的为mysql ...

最新文章

  1. js和html以及css的区别,html、css、js中的区别与关系
  2. Java基础学习_01 概述及环境配置
  3. 【学术相关】10篇顶会paper,入选微软学者,上海交大吴齐天的科研思考!
  4. U盘安装BackTrack5,提示Could not find kernel image: linux
  5. mysql事务隔离级别详解_高性能MySQL-详解事务与隔离级别
  6. 输入url到页面返回的过程
  7. 特征提取与检测(五) : LBP特征
  8. 【迅雷VIP体验】免费获得迅雷会员,享受高速下载通道
  9. rgba通道转rgb_image – 将RGBA颜色转换为RGB
  10. 电脑常见故障排查思路
  11. dw自动生成html,如何用Dreamweaver快速创建HTML代码
  12. ios html fixed,ios下position:fixed失效的问题解决
  13. 什么软件适合团队协作?团队协作工具排行
  14. 【漏洞复现】海洋CMS6.28远程代码执行
  15. 多线程-day-09CAS原理
  16. Django入门 | 官方文档带你快速入门
  17. 论程序员如何玩阴阳师
  18. LabVIEW编更改研华数字板卡中DIO方向 例程与相关资料
  19. 什么是商家转账到零钱
  20. csharp基础练习题:符号计数【难度:1级】--景越C#经典编程题库,不同难度C#练习题,适合自学C#的新手进阶训练

热门文章

  1. 东北人讲java_东北人,请你讲东北话(内附东北话速成)
  2. 房屋登记官考核模拟题(6)
  3. 泰勒公式(泰勒展开式,泰勒中值定理)使用基本技巧
  4. 沐阳从0到1零基础学习安卓逆向
  5. python mysql插入数据报错:TypeError: %d format: a number is required, not str
  6. 页面交换文件pagefile.sys-虚拟内存页面文件
  7. 离散时间信号,连续时间信号,模拟信号,数字信号区别
  8. Dapr for dotnet | 并发计算模型 - Virtual Actors
  9. (超详细)手把手带你爬取南邮毛概马原思修题库
  10. springboot+redis主从复制、哨兵、读写分离