规划结构图
nginx接收用户请求反向代理至后端应用服务器
应用服务器或者session服务器其中一台宕机,不影响客户端会话的信息
OS:CentOS release 6.7 (Final)
Memcached设置
两台memcached1 2 各进行配置
1.安装memcached
[root@Memcached1 ~]# yum install memcached
[root@Memcachd2 ~]# yum install memcached
2.启动服务
[root@Memcached1 ~]# service memcachedstart
Starting memcached: [ OK ]
[root@Memcachd2 ~]# service memcached start
Starting memcached: [ OK ]
3.测试
[root@Memcached1 ~]# ss -tln State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::11211 :::* LISTEN 0 128 *:11211 *:*
]# telnet 192.168.1.180 11211 Trying 192.168.1.180... Connected to 192.168.1.180. Escape character is '^]'. quit Connection closed by foreign host. |
[root@Memcachd2 ~]# ss –tln State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::11211 :::* LISTEN 0 128 *:11211 *:*
]# telnet 192.168.1.190 11211 Trying 192.168.1.190... Connected to 192.168.1.190. Escape character is '^]'. quit Connection closed by foreign host. |
Tomcat设置
1.TomcatA B 安装
参照tomcat基本安装
2.上传jar文件
分别把依赖包上传到 Tomcat A B
]# pwd
/usr/local/tomcat/lib
3.TomcatA 设置
3.1创建目录
[root@TomcatA lib]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib} mkdir: created directory `/usr/local/tomcat/webapps/test' mkdir: created directory `/usr/local/tomcat/webapps/test/WEB-INF' mkdir: created directory `/usr/local/tomcat/webapps/test/WEB-INF/classes' mkdir: created directory `/usr/local/tomcat/webapps/test/WEB-INF/lib' 修改权限 [root@TomcatA lib]# chown -R tomcat /usr/local/tomcat/webapps/test/
|
3.2 创建主页内容
]# vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" %>
<% session.setAttribute("zrd.com","zrd.com"); %>
|
3.3 修改server.xml
高亮部分为 添加的内容其他不要修改
[root@TomcatA conf]# vim /usr/local/tomcat/conf/server.xml
type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> connectionTimeout="20000" redirectPort="8443" /> resourceName="UserDatabase"/> unpackWARs="true" autoDeploy="true"> prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> memcachedNodes=" memcached1:192.168.1.180:11211,memcached2:192.168.1.190:11211 " failoverNodes=" memcached1 " requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" /> |
4.TomcatB 设置
4.1创建目录
[root@TomcatB classes]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib} mkdir: created directory `/usr/local/tomcat/webapps/test' mkdir: created directory `/usr/local/tomcat/webapps/test/WEB-INF' mkdir: created directory `/usr/local/tomcat/webapps/test/WEB-INF/classes' mkdir: created directory `/usr/local/tomcat/webapps/test/WEB-INF/lib' |
4.2创建主页内容
#]vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" %>
<% session.setAttribute("magedu.com","magedu.com"); %>
|
4.3 修改server.xml
添加高亮部分内容,其他不用改动
[root@TomcatB ~]# vim /usr/local/tomcat/conf/server.xml
type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> connectionTimeout="20000" redirectPort="8443" /> resourceName="UserDatabase"/> unpackWARs="true" autoDeploy="true"> prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> memcachedNodes=" memcached1:192.168.1.180:11211,memcached2:192.168.1.190:11211" failoverNodes=" memcached1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" /> |
proxy设置
使用nginx作为反向代理服务器
1.安装
需要配置epel源(http://mirrors.sohu.com/fedora-epel/6/x86_64/ )
yum install nginx
2.修改配置文件
高亮部分为添加的内容,其他不动
[root@zrd0 nginx]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx; worker_processes 1;
error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info;
pid /run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
# Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf;
index index.html index.htm; upstream tomcats { # ip_hash; server 192.168.1.150:8080; server 192.168.1.160:8080; } server { listen 80; server_name localhost; root /usr/share/nginx/html;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://tomcats/; } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { }
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.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; # root html;
# location / { # } #}
# HTTPS server # #server { # listen 443; # server_name localhost; # root html;
# 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 / { # } #}
} |
测试
启动服务
memcached
[root@Memcached1 ~]# service memcached start Starting memcached: [ OK ]
[root@Memcachd2 ~]# service memcached start Starting memcached: [ OK ] |
Tomcat
[root@TomcatA conf]# service tomcat start
[root@TomcatB conf]# service tomcat start |
nginx
[root@zrd0 nginx]# service nginx start |
轮询测试
关闭Memcached2,故障转移是否成功
[root@Memcachd2 ~]# service memcached stop
Stopping memcached: [ OK ]
故障转移到Memcached1成功
配置文件
TomcatA B
nginx