您的位置:首页 > 数据库 > Redis

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

2018-01-10 17:37 686 查看
目标实现:Nginx作为负载均衡后端多Tomcat实例,通过Redis实现Session共享。

操作系统环境:CentOS 6.8

SSH:SecureCRT

其中

Nginx服务:80端口

Tomcat实例1:8080端口

Tomcat实例2:8060端口

Redis服务:6379端口

下载相关软件都放在 百度云 密码:29ic ,下面的安装过程中需要的软件 都可以从这里直接下载。

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

tar zxvf lrzsz-0.12.20.tar.gz && cd lrzsz-0.12.20

./configure && make && make install

安装过程默认把lsz和lrz安装到了/usr/local/bin/目录下,现在我们并不能直接使用,下面创建软链接,并命名为rz/sz
cd /usr/bin
ln -s /usr/local/bin/lrz rz
ln -s /usr/local/bin/lsz sz

上传文件命令:rz
下载文件命令:sz filename

打开SecureCRT软件 -> Options -> session options -> X/Y/Zmodem 下可以设置上传和下载的目录

安装JDK


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

jdk配置环境变量
vi /etc/profile
在末尾新增
export JAVA_HOME=/usr/java/jdk1.7.0_80
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$JAVA_HOME/bin:$PATH

source /etc/profile

安装Maven

tar -zxvf apache-maven-3.0.5-bin.tar.gz

vi /etc/profile #环境配置
export MAVEN_HOME=/root/maven/apache-maven-3.0.5
export PATH=$JAVA_HOME/bin:$PATH:$MAVEN_HOME/bin

source /etc/profile

[b]安装Redis
[/b]

参见 PHP开发中Redis安装(CentOS6.5)

安装Nginx

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel #安装依赖
tar -zxvf nginx-1.10.2.tar.gz #解压缩
cd nginx-1.10.2 #进入nginx目录
./configure
# 也可以指定安装目录 增加参数 --prefix=/usr/nginx
# 如果不指定路径 可以在安装后通过whereis nginx查询
# 默认安装在/usr/sbin/nginx
make
make install
mkdir /usr/local/nginx/vhost

sudo vim /usr/local/nginx/conf/nginx.conf #编辑增加 include vhost/*.conf 保存退出

具体配置参见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 git@github.com: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>


5.复制百度云中tomcat8060目录内的index.html、login.jsp 到服务器 tomcat8060目录下/webapps/ROOT/目录下
复制百度云中tomcat8080目录内的index.html、login.jsp 到服务器 tomcat8080目录下/webapps/ROOT/目录下

6.分别启动两个tomcat实例
cd .tomcat8060/bin && ./catalina.sh start
cd .tomcat8080/bin && ./catalina.sh start

最终效果

1.第一次访问转向 端口是8060 的tomcat 实例



2.第二次访问转向 端口是8080 的tomcat 实例



3.点击上图中的login按钮,可以看到如下图,访问的其实是tomcat8060实例。

这其实就已经做到多tomcat实例的Session都是通过我们指定的Redis服务共享。



问题

1.git clone git@github.com:jcoleman/tomcat-redis-session-manager.git 不成功

[root@iZ38n4tck31thgZ ~]# git clone git@github.com: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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: