负载均衡之Nginx

  • Nginx配置
环境限制,win10下测试下Nginx的负载均衡,配置比较简单,最简配置如下:
worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;

    keepalive_timeout  65;

    upstream cluster_test{
	server 127.0.0.1:6080;
	server 127.0.0.1:7080;
	#ip_hash;
        #upstream_hash;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm	index.jsp;
	    proxy_pass	http://cluster_test;
	    proxy_set_header	Host	$host;
	    proxy_set_header	X-Real-IP	$remote_addr;
	    proxy_set_header	X-Forwarded-For	$proxy_add_x_forwarded_for;
        }
    }
}
ip_hash:实现Session Sticky ,针对同一个C类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个
upstream_hash:为了解决ip_hash的一些问题,可以使用upstream_hash这个第三方模块,这个模块多数情况下是用作url_hash
nginx_upstream_jvm_route:Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 功能


  • Nginx+tomcat+jeesite集群
Nginx中ip_hash、upstream_hash、nginx_upstream_jvm_route可以达到session共享的目的,ip_hash已验证,有兴趣的可以试一下。本文使用外接缓存达到session共享的目的,jeesite默认提供了Ehcache、redis的解决方案,选用Ehcache的RMI进行cache同步实现session共享。选择该方案,只是为了图省事,建议采用redis。

在实际测试时,每次切换节点shiro自定义的sessionid都会变好,花费了我很多时间,快奔溃了,各种查资料、看shiro源码。也猜测过可能是cache同步的问题,简单做了下测试,发现有进行同步。又去查其它的问题,走了很多弯路,最后发现还是cache的问题。jeesite下默认的ehcache-rmi.xml配置是需要进行调整的,activeSessionCache监听工厂的参数replicateAsynchronously=true、replicateUpdatesViaCopy=false需要调整,调整replicateAsynchronously=false、replicateUpdatesViaCopy=true,或者replicateUpdatesViaCopy=true、asynchronousReplicationIntervalMillis=200。原因在于cache节点间异步复制有时间间隔,切换节点时可能复制未完成。因此,应根据cache实际应用场景采用同步复制或者异步复制,异步复制时应注意间隔时间。

tomcat-redis-session-manager、memcached-session-manager也可达到session共享的目的

使用负载均衡之后的Session处理思路
引用

Apache 与 Nginx 比较
引用

web服务器nginx和apache的对比分析
引用

为什么Nginx的性能要比Apache高很多?
引用

猜你喜欢

转载自sheungxin.iteye.com/blog/2349030