目標實現:Nginx做爲負載均衡後端多Tomcat實例,經過Redis實現Session共享。html
操做系統環境:CentOS 6.8java
SSH:SecureCRTlinux
其中nginx
Nginx服務:80端口git
Tomcat實例1:8080端口github
Tomcat實例2:8060端口web
Redis服務:6379端口redis
下載相關軟件都放在 百度雲 密碼:29ic ,下面的安裝過程當中須要的軟件 均可以從這裏直接下載。apache
安裝rz/sz命令:便於文件傳輸vim
tar zxvf lrzsz-0.12.20.tar.gz && cd lrzsz-0.12.20
./configure && make && make install
安裝JDK
安裝Maven
安裝Redis
安裝Nginx
具體配置參見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/