posts - 27,comments - 2,trackbacks - 0
做个总结
linux服务器上做负载均衡
自己准备的:linux服务器(45.78.20.168),jdk1.7,nginx,redis,tomcat7两个,部署的项目;

1:jdk1.7安装,两个tomcat分别端口8080,8081;部署相同的项目;启动;
    http://45.78.20.168:8080/redis3.2/getRedis.action
    http://45.78.20.168:8081/redis3.2/getRedis.action
2:安装nginx,添加负载的配置,安装目录下找 /etc/nginx/conf.d/default.conf文件(或/etc/nginx/nginx.conf);策略设置为默认轮询;
    upstream www.nimenhaihaoma.com {
        server 45.78.20.168:8080;
        server 45.78.20.168:8081;
    }
    server{
        listen 80;
        server_name www.nimenhaihaoma.com;
        location / {
            proxy_pass http://www.nimenhaihaoma.com;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
3:配置session共享,方式很多,这里用的redis的session共享(兼容jdk版本至少1.7):
    tomcat的lib包加commons-pool2-2.0.jar,jedis-2.5.2.jar,tomcat-redis-session-manager1.2.jar;
    tomcat配置文件context.xml,在标签<Context>内添加配置:
        <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
        <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />

4:项目里面区分session的代码:
    (1):放session的接口(执行一次);
    (2):取session数据(不断刷新),看tomcat打印信息;
5:效果,http://www.nimenhaihaoma.com/redis3.2/getRedis.action (狂刷session值相同)



posted @ 2016-08-17 17:03 魏文甫 阅读(113) | 评论 (0)编辑 收藏