负载均衡Java服务器端编程

nginx+tomcat+redis负载均衡及session共享

2017-03-31  本文已影响176人  半天想不出来

在网上google好多文章,好多文章不是缺这就是缺那,最终还是顺利搞定,在之前一个项目上测试成功。亲自动手尝试过,对这套架构才能更加了解,并遇到一些小坑,特以此文记录。

一、安装nginx

创建安装目录:mkdir /usr/local/soft && cd /usr/local/soft
命令下载Nginx压缩包:wget http://nginx.org/download/nginx-1.10.3.tar.gz
解压压缩包:tar zxvf nginx-1.10.3.tar.gz
进入安装目录: cd /usr/local/nginx-1.10.3
尝试安装:./configure

在最后提示:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

Nginx共依赖以下三个包:
1.gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ )
2.rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ )
3.ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/ )

依赖包安装顺序依次为:openssl、zlib、pcre, 最后安装Nginx包
1)下载并安装openssl

wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
解压压缩包:tar zxvf openssl-1.0.1t.tar.gz
安装:cd openssl-1.0.1t && ./config && make && make install

2)下载并安装zlib

wget http://zlib.net/zlib-1.2.11.tar.gz
解压:tar zxvf zlib-1.2.8.tar.gz 
安装:cd zlib-1.2.8/ && ./configure && make && make install

3)下载并安装PCRE

wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
解压:tar zxvf pcre-8.37.tar.gz
安装:cd pcre-8.37/ && ./configure && make && make install

4)再次尝试安装Nginx

安装命令:cd nginx-1.10.3/ && ./configure && make && make install

5)测试开启Nginx

尝试启动:cd /usr/local/nginx/sbin/ && ./nginx
搜索该文件:whereis libpcre.so.1
提示:libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so /usr/local/lib/libpcre.so.1
 或 libpcre.so: /usr/local/lib/libpcre.so.1 /usr/local/lib/libpcre.so
执行:ln -s /usr/local/lib/libpcre.so.1 /lib64
 或:ln -s /usr/local/lib/libpcre.so.1 /lib
再次尝试启动:./nginx

打开浏览器,显示如下则成功。

welcome.png

二、安装redis

1)安装

下载:wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar xzf redis-3.2.1.tar.gz
cd redis-3.2.1
make && make install

完成后启动redis服务

redis-server

2)修改配置文件
redis.conf配置文件修改,先复制一份。

cp redis.conf /usr/local/redis.conf

redis.conf文件

protected-mode yes修改为no
daemonize no 修改为yes
#屏蔽bind信息
#bind 127.0.0.1

3)测试
启动指定配置文件

redis-server /usr/local/redis.conf

客户端访问

$ redis-cli   
127.0.0.1:6379> exit

4)开放端口
配置好后,需要开放redis端口6379

iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

保存规则

service iptables save

重启防火墙

service iptables restart

三、配置tomcat基于redis的session共享

1)配置第三方类库,支持session共享
准备的包
commons-pool-1.6.jar,(http://commons.apache.org/proper/commons-pool/download_pool.cgi
tomcat-redis-session-manager-1.2-tomcat-7,(https://github.com/jcoleman/tomcat-redis-session-manager/downloads
jedis-2.1.0.jar(http://central.maven.org/maven2/redis/clients/jedis/2.1.0/jedis-2.1.0.jar

将这个三个包放入tomcat的/lib中

修改context.xml
新增redis session管理第三方支持

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" /> 
<Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="192.168.110.87" port="6379" database="0" maxInactiveInterval="60" />
context.xml.png

2)配置跨域访问
准备包:
java-property-utils-1.10.jar
cors-filter-2.5.jar

修改web.xml,新增如下配置。(web.xml可为具体项目的web.xml。也可以为tomcat/conf/web.xml,二者的作用范围不同)

<filter>  
  <filter-name>CORS</filter-name>  
  <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>  
  <init-param>  
      <param-name>cors.allowOrigin</param-name>  
      <param-value>*</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.supportedMethods</param-name>  
      <param-value>GET, POST, HEAD, PUT, DELETE</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.supportedHeaders</param-name>  
      <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified,app_key</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.exposedHeaders</param-name>  
      <param-value>Set-Cookie</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.supportsCredentials</param-name>  
      <param-value>true</param-value>  
  </init-param>  
  </filter>  
  <filter-mapping>  
      <filter-name>CORS</filter-name>  
      <url-pattern>/*</url-pattern>  
  </filter-mapping>

前端需要修改

var utils = angular.module('Utils', []);    
    utils.config(['$httpProvider', config]);    
    function config($httpProvider) {    
            $httpProvider.defaults.withCredentials = true;    
    }  

四、配置nginx负载均衡

部署图:

部署图.png

修改nginx配置文件
在http{ }内新增

http {
    include       mime.types;
    default_type  application/octet-stream;
    #Tomcat
    upstream gs {
     #server 192.168.110.238:9080 max_fails=1 fail_timeout=10s;
     server 192.168.110.250:9080 max_fails=1 fail_timeout=10s;
     server 192.168.110.151:8080 max_fails=1 fail_timeout=10s;
    }  

    location ~ \.(do|jsp)$ {#/do,jsp结尾都转发到tomcat集群
        proxy_pass http://gs;
        proxy_redirect off;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For
        $proxy_add_x_forwarded_for;
    }

    location /webapp{
        root  /html;#/html为前端工程根目录
        index index.html index.htm;
    }

    location /arcgis {/arcgis
        root /html;
    }
}

重启nginx:

/usr/local/nginx/sbin/nginx -s reload

五、注意

1、Tomcat里面web.xml的session-timeout不能设为-1,不然session保存到redis会马上失效,不能实现session共享。
2、在tomcat/conf中修改了配置文件,但是还是用eclipse启动的tomcat,会出现修改配置文件被冲掉的情况,可以
a、不在eclipse中启动
b、在eclipse,server工程中修改配置文件

3、存放到session里面的对象,必须进行序列化,实现Serializable接口,不然保存不到redis中去。

4、后续需要优化的是nginx,redis注册服务,和nginx的Index页面配置等。

上一篇下一篇

猜你喜欢

热点阅读