环境准备
1、准备一台 nginx 服务器 ip192.168.1.133 端口 81
安装过程:
- #首先安装依赖:yum - y install gcc - c++yum - y install pcre pcre - devel yum - y install zlib zlib - devel yum - y install openssl openssl—devel#注意:安装nginx必须使用root用户安装#创建一个nginx目录mkdir / usr / local / src / nginx#进入到nginx目录cd / usr / local / src / nginx#下载或上传安装包wget http: //nginx.org/download/nginx.tar.gz 或 rz上传
- #解压安装包tar - xvf nginx.tar.gz#进入到解压后的目录cd nginx#下面才开始正式安装#把nginx安装到指定用户的目录mkdir - p / ucenter / soft / nginx#安装配置prefix为安装目录user为用户group为组. / configure--prefix = /ucenter/soft / nginx--user = ucenter--group = ucenter#编译make#安装make install#在linux系统中由于非root用户不能占用80端口,所以需要使普通用户以root身份启动nginx。cd / ucenter / soft / nginx / sbin#把soft文件下所有的文件所属者修改为ucener - R表示递归chown ucenter: ucenter. / soft / -R#修改. / nginx的所属为root chown root nginx#让普通用户可以使用80端口,可以使用root权限启用nginx chmod u + s nginx#修改配置文件在修改配置文件之前,要备份该文件cd conf / #要注意nginx的工作进程,一般根据cpu的核数去修改vim nginx.conf#关闭防火墙,打开80端口service iptables stop#启动nginx. / nginx#重启nginx. / nginx - s reload#关闭nginx. / nginx - s stop
准备一台 tomcat 服务器,先准备 java 环境,安装 jdk 步骤省略
然后分别安装 3 个 tomcat 服务器 ip 地址:192.168.1.143,tomcat1 8080 端口,tomcat2 8081 端口,tomcat3 8082 端口。
apache-tomcat-7.0.64/conf/server.xml 配置文件修改这三个地方,这样端口就不会冲突
- <Server port="8005" shutdown="SHUTDOWN">
- <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"
- />
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改 tomcat ROOT 目录下 index.jsp,分别增加每个 tomcat 的标识,以及在页面上显示 session ID
- <%-- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with this
- work for additional information regarding copyright ownership. The ASF
- licenses this file to You under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance with the License. You
- may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed
- under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
- OR CONDITIONS OF ANY KIND, either express or implied. See the License for
- the specific language governing permissions and limitations under the License.
- --%>
- <%@ page session="true" %>
- <% java.text.SimpleDateFormat sdf=n ew java.text.SimpleDateFormat(
- "yyyy"); request.setAttribute( "year", sdf.format(new java.util.Date())); request.setAttribute(
- "tomcatUrl", "http://tomcat.apache.org/"); request.setAttribute( "tomcatDocUrl",
- "/docs/"); request.setAttribute( "tomcatExamplesUrl", "/examples/"); %>
- <%=request.getServletContext().getServerInfo() %>
- class="curved container"> class="separator" /> ${pageContext.servletContext.serverInfo}--8080
- class="curved container"> class="curved container"> If you're seeing this,
- you've successfully installed Tomcat. Congratulations! Recommended Reading:
- class="button"> class="container shadow" href="/manager/status">Server
- Status class="button"> class="container shadow" href="/manager/html">Manager
- App class="button"> class="container shadow" href="/host-manager/html">Host
- Manager class="separator" /> --> class="separator" /> class="curved container">
- Developer Quick Start class="col25"> class="container"> class="col25">
- class="container"> class="col25"> class="container"> class="col25"> class="container">
- class="separator" /> class=""> class="curved container"> Managing Tomcat
- For security, access to the is restricted. Users are defined in: $CATALINA_HOME/conf/tomcat-users.xml
- In Tomcat 7.0 access to the manager application is split between different
- users. class=""> class="curved container"> Documentation Find additional
- important configuration information in: $CATALINA_HOME/RUNNING.txt Developers
- may be interested in: class=""> class="curved container"> Getting Help
- and The following mailing lists are available: Important announcements,
- releases, security vulnerability notifications. (Low volume). User support
- and discussion User support and discussion for Development mailing list,
- including commit messages class="separator" /> class="curved container">
- class="col20"> class="container"> Other Downloads class="col20"> class="container">
- Other Documentation class="col20"> class="container"> Get Involved class="col20">
- class="container"> Miscellaneous class="col20"> class="container"> Apache
- Software Foundation class="separator" /> class="copyright">Copyright ©1999-${year}
- Apache Software Foundation. All Rights Reserved
这时候 修改 nginx 配置文件 nginx.conf,把三台 tomcat 的 ip 地址以及端口号加入进去,使用 nginx 做代理
- #user nobody;
- worker_processes 1;#error_log logs / error.log;#error_log logs / error.log notice;#error_log logs / error.log info;#pid logs / nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application / octet - stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '#'$status $body_bytes_sent "$http_referer" '#'"$http_user_agent" "$http_x_forwarded_for"';#access_log logs / access.log main;
- sendfile on;#tcp_nopush on;#keepalive_timeout 0;
- keepalive_timeout 65;#gzip on;
- upstream localhost1 {
- #ip_hash;
- server 192.168.1.143 : 8080;
- server 192.168.1.143 : 8081;
- server 192.168.1.143 : 8082;
- }
- server {
- listen 81;
- server_name localhost;#charset koi8 - r;#access_log logs / host.access.log main;
- location / {
- proxy_connect_timeout 3;
- proxy_send_timeout 30;
- proxy_read_timeout 30;
- proxy_pass http: //localhost1;
- }#error_page 404 / 404.html;#redirect server error pages to the static page / 50x.html#error_page 500 502 503 504 / 50x.html;
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http:/ / 127.0.0.1;#
- }#pass the PHP scripts to FastCGI server listening on 127.0.0.1 : 9000##location~\.php$ {#root html;#fastcgi_pass 127.0.0.1 : 9000;#fastcgi_index index.php;#fastcgi_param SCRIPT_FILENAME / scripts$fastcgi_script_name;#include fastcgi_params;#
- }#deny access to.htaccess files,
- if Apache 's document root
- # concurs with nginx's one##location~ / \.ht {#deny all;#
- }
- }#another virtual host using mix of IP - ,
- name - ,
- and port - based configuration##server {#listen 8000;#listen somename: 8080;#server_name somename alias another.alias;#location / {#root html;#index index.html index.htm;#
- }#
- }#HTTPS server##server {#listen 443;#server_name localhost;#ssl on;#ssl_certificate cert.pem;#ssl_certificate_key cert.key;#ssl_session_timeout 5m;#ssl_protocols SSLv2 SSLv3 TLSv1;#ssl_ciphers HIGH: !aNULL: !MD5;#ssl_prefer_server_ciphers on;#location / {#root html;#index index.html index.htm;#
- }#
- }
- }
这时候,分别启动三台 tomcat 以及 nginx,访问 http://192.168.1.133:81,这时候每次刷新页面,都会随机访问 8080 或者 8081 或者 8082,而且页面上出现的 session id 也都是不一样的,我们应该如何让这三台 tomcat 共享 session 呢,我们使用 redis 来做。
这时候,在已经按照三台 tomcat 的服务器 192.168.1.143 上,安装 redis,安装步骤如下:
- $ wget http: //download.redis.io/releases/redis-3.2.3.tar.gz
- $ tar xzf redis - 3.2.3.tar.gz $ cd redis - 3.2.3 $ make MALLOC = libc#启动redis src前面是安装的路径$ src / redis - server & #关闭redis src / redis - cli shutdown#使用redis放入键值对key value $ src / redis - cli 127.0.0.1 : 6379 > set foo bar OK 127.0.0.1 : 6379 > get foo "bar"$
安装完 redis 之后,在三个 tomcat 的 lib 文件夹内分别上传这五个所依赖的 jar,分别是
commons-pool2-2.0.jar
jedis-2.5.2.jar
tomcat-redis-session-manager1.2.jar
tomcat-juli.jar
tomcat-juli-adapters.jar
所有 jar 我上传到博客园里了,:
http://files.cnblogs.com/files/jiafuwei/tomcat_redis_session 共享 jar.rar
复制该链接,即可下载,如果有问题,可以及时联系我。
然后分别修个三个 tomcat 的 context.xml 文件,增加如下的配置
- <?xml version='1.0' encoding='utf-8' ?>
- <Context>
- <!-- Default set of monitored resources -->
- <WatchedResource>
- web-INF/web.xml
- </WatchedResource>
- <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"
- />
- <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
- host="127.0.0.1" port="6379" database="0" maxInactiveInterval="60" />
- </Context>
这时候,分别重启三个 tomcat 以及 nginx,查看 tomcat 日志之后,发现没有任何异常报错,说明我们成功了,接下来开始测试。
我们访问 nginx 服务器地址:http://192.168.1.133:81/
得到的是 8080 端口的 tomcat1 ,session id 为 1A0625767F27BA95EF4D5F061FE0568D
这时候按 F5 刷新页面,得到的是 8081 端口的 tomcat2 ,session id 依旧是 1A0625767F27BA95EF4D5F061FE0568D
再次刷新页面,得到的是 8082 端口的 tomcat3,session id 依旧是 1A0625767F27BA95EF4D5F061FE0568D。
这时候,说明我们搭建 tomcat + nginx 负载均衡 + redis session 同步成功啦!
nginx 帮助把我们的请求均匀的分发给三个 tomcat --》tomcat1 、tomcat2 以及 tomcat3
redis 帮助我们同步 session,这样一来,我们的服务器性能就会提高许多,任何一台 tomcat 发生故障后,对整体的服务都不会有影响了。
来源: http://www.cnblogs.com/jiafuwei/p/6122369.html