Nginx+Tomca+Redis实现负载均衡资源分离session共享

Posted zendwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+Tomca+Redis实现负载均衡资源分离session共享相关的知识,希望对你有一定的参考价值。

目标实现:nginx作为负载均衡后端多Tomcat实例,通过Redis实现Session共享。

操作系统环境:CentOS 6.8

下载相关软件 百度云 密码:29ic

安装rz/sz命令:便于文件传输

 

安装Maven


安装Redis

 

安装JDK

sudo rpm -ivh jdk-7u80-linux-x64.rpm //解压安装

安装Nginx

安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
下载源码包
解压缩
tar -zxvf nginx-1.10.2.tar.gz
Nginx安装
进入nginx目录之后执行./configure
也可以指定安装目录 增加参数 --prefix=/usr/nginx
如果不指定路径 可以在安装后通过whereis nginx查询
默认安装在/usr/sbin/nginx
执行make
执行 make install
 
编辑sudo vim /usr/local/nginx/conf/nginx.conf,增加 include vhost/*.conf,保存退出
mkdir /usr/local/nginx/vhost

具体配置参见wangjiangnet.conf

 

安装Git

yum install git

安装Tomcat

mv apache-tomcat-7.0.73 tomcat8080
mv apache-tomcat-7.0.73 tomcat8060

cd /root/tomcat/tomcat8060/bin
vi catalina.sh
CATALINA_HOME=/root/tomcat/tomcat8060


cd /root/tomcat/tomcat8080/bin
vi catalina.sh
CATALINA_HOME=/root/tomcat/tomcat8080

cd /root/tomcat/tomcat8060/conf
vi server.xml #修改Server 、Connector(HTTP/1.1) 、Connector(AJP/1.3) 端口

 

<Server port="8065" shutdown="SHUTDOWN">

    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8060" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"  URIEncoding="UTF-8" />


    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8069" protocol="AJP/1.3" redirectPort="8443" />

  

 

编译tomcat-redis-session-manager项目【也可以直接从百度云下载】

1.git clone [email protected]:jcoleman/tomcat-redis-session-manager.git

2. cd tomcat-redis-session-manager && vi pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ufind.session</groupId>
    <artifactId>tomcat-redis-session</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>7.0.27</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.执行mvn clean 和mvn install 将编译好的代码打包为:tomcat-redis-session-1.0-SNAPSHOT.jar,将tomcat-redis-session-1.0-SNAPSHOT.jar、jedis-2.7.2.jar、commons-pool2-2.0.jar 三个jar包分别放在tomcat8080和tomcat8060实例下的lib目录下。

4.修改配置/root/tomcat/tomcat8060/conf/context.xml、/root/tomcat/tomcat8080/conf/context.xml 配置redis session 共享

cd /root/tomcat/tomcat8060/conf
vi context.xml

 注意 Redis 一定要配置密码的!

<?xml version=‘1.0‘ encoding=‘utf-8‘?>

<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- tomcat-redis-session共享配置 --> 
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> 
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
host="127.0.0.1" 
port="6379" 
database="0"
password="abcd1234" 
maxInactiveInterval="60" /> 
</Context>

  

 

问题:

1.git clone [email protected]:jcoleman/tomcat-redis-session-manager.git 不成功

[[email protected] ~]# git clone [email protected]:jcoleman/tomcat-redis-session-manager.git
Initialized empty Git repository in /root/tomcat-redis-session-manager/.git/
The authenticity of host ‘github.com (192.30.255.112)‘ can‘t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,192.30.255.112‘ (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

生成新的SSH key 参照 https://help.github.com/articles/connecting-to-github-with-ssh/

 

以上是关于Nginx+Tomca+Redis实现负载均衡资源分离session共享的主要内容,如果未能解决你的问题,请参考以下文章

nginx-tomcat负载均衡redis-session共享,静态资源分离

使用Redis存储Nginx+Tomcat负载均衡集群的Session

使用Redis存储Nginx+Tomcat负载均衡集群的Session

nginx-tomcat负载均衡redis-session共享,静态资源分离

nginx-tomcat负载均衡redis-session共享,静态资源分离

利用nginx实现Redis的负载均衡,应该怎么配置?