redis是一個key-value存儲系統。和Memcached相似,它支持存儲的value類型相對更多,包括
string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。javascript
一、Redis不單單支持簡單的k/v類型的數據,同時還提供list,set,zset,hash等數據結構的存儲。
二、Redis支持數據的備份,即master-slave模式的數據備份。
三、Redis支持數據的持久化,能夠將內存中的數據保持在磁盤中,重啓的時候能夠再次加載進行使用。
nginx+tomcat+redis實現負載均衡、session共享
1,安裝部署nginx負載均衡服務器
安裝zlib-devel、pcre-devel等依賴包[root@www ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
注:
結合proxy和upstream模塊實現後端web負載均衡
結合nginx默認自帶的ngx_http_proxy_module模塊 和ngx_http_upstream_module模塊實現後
端服務器的健康檢查
建立nginx程序用戶[root@www ~]# useradd -s /sbin/nologin www
編譯安裝nginx[root@www ~]# tar zxf nginx-1.10.2.tar.gz
[root@www ~]# cd nginx-1.10.2/
[root@www nginx-1.10.2]# ./configure --prefix=/usr/local/nginx1.10 --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_flv_module
[root@www nginx-1.10.2]# make && make install
優化nginx程序的執行路徑[root@www nginx-1.10.2]# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/
[root@www nginx-1.10.2]# nginx -t
nginx: the configuration file /usr/local/nginx1.10/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.10/conf/nginx.conf test is successful
編寫nginx服務腳本:腳本內容以下:
複製進去便可[root@www ~]# cat /etc/init.d/nginx
css
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # chkconfig: - 85 15 # pidfile: /usr/local/nginx1.10/logs/nginx.pid # config: /usr/local/nginx1.10/conf/nginx.conf nginxd=/usr/local/nginx1.10/sbin/nginx nginx_config=/usr/local/nginx1.10/conf/nginx.conf nginx_pid=/usr/local/nginx1.10/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Start nginx daemons functions. start() { if [ -f $nginx_pid ] ; then echo "nginx already running...." exit 1 fi echo -n "Starting $prog: " $nginxd -c ${nginx_config} RETVAL=$? [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx } # Stop nginx daemons functions. stop() { echo -n "Stopping $prog: " $nginxd -s stop RETVAL=$? [ $RETVAL = 0 ] &&rm -f /var/lock/subsys/nginx } # reloadnginx service functions. reload() { echo -n "Reloading $prog: " $nginxd -s reload } # statusngnx service functions status() { if [ -f $nginx_pid ] ; then echo "$prog is running" else echo "$prog is stop" fi } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status ;; *) echo "Usage: $prog {start|stop|restart|reload|status}" exit 1 ;; esac
[root@www ~]# chmod +x /etc/init.d/nginx
[root@www ~]# chkconfig --add nginx
[root@www ~]# chk config nginx on
[root@www ~]# systemctl daemon-reload
配置nginx反向代理:反向代理+負載均衡+健康探測,nginx.conf文件內容:[root@www ~]# cat /usr/local/nginx1.10/conf/nginx.conf
html
user www www; worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; worker_rlimit_nofile 10240; pid logs/nginx.pid; events { use epoll; worker_connections 4096; } 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; server_tokens off; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #Compression Settings gzip on; gzip_comp_level 6; gzip_http_version 1.1; gzip_proxied any; gzip_min_length 1k; gzip_buffers 16 8k; gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; gzip_vary on; #end gzip # http_proxy Settings client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 75; proxy_send_timeout 75; proxy_read_timeout 75; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; #load balance Settings upstream backend_tomcat { server 192.168.31.83:8080 weight=1 max_fails=2 fail_timeout=10s; #要改 server 192.168.31.250:8080 weight=1 max_fails=2 fail_timeout=10s; #要改 } #virtual host Settings server { listen 80; server_name www.benet.com; charset utf-8; location / { root html; index index.jsp index.html index.htm; } location ~ \.(jsp|do)$ { proxy_pass http://backend_tomcat; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } location /nginxstatus { stub_status on; access_log off; allow 192.168.1.0/24; #要改 deny all; } } }
重啓nginx服務,使修改生效
[root@www ~]# service nginx restart
配置防火牆規測 允許 tcp/80端口出去[root@www ~]# firewall-cmd --permanent --add-port=80/tcp
success[root@www ~]# firewall-cmd --reload
successjava
2,安裝部署tomcat應用程序服務器
在tomcat-1和tomcat-2節點上安裝JDK
在安裝tomcat以前必須先安裝JDK,JDK的全稱是java development kit,是sun公司免費提
供的java語言的軟件開發工具包,其中包含java虛擬機(JVM),編寫好的java源程序通過
編譯可造成java字節碼,只要安裝了JDK,就能夠利用JVM解釋這些字節碼文件,從而保證了
java的跨平臺性。
安裝JDK,配置java環境:
將jdk-7u65-linux-x64.gz解壓[root@tomcat-1 ~]# tar zxf jdk-7u65-linux-x64.gz
將解壓的jdk1.7.065目錄移致動到/usr/local/下並重命名爲java[root@tomcat-1 ~]# mv jdk1.7.065/ /usr/local/java
在/etc/profile文件中添加內容以下:
export JAVAHOME=/usr/local/java
export PATH=$JAVAHOME/bin:$PATH
經過source命令執行profile文件,使其生效。[root@tomcat-1 ~]# source /etc/profile
[root@tomcat-1 ~]# echo $PATH
/usr/local/java/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
按照相同方法在tomcat-2也安裝JDK
分別在在tomcat-1和tomcat-2節點運行java -version命令查看java版本是否和以前安裝的
一致。[root@tomcat-1 ~]# java -version
java version "1.7.065"
Java(TM) SE Runtime Environment (build 1.7.065-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
至此java環境已經配置完成
在tomcat-1和tomcat-2節點安裝配置tomcat
解壓apache-tomcat-7.0.54.tar.gz包[root@tomcat-1 ~]# tar zxf apache-tomcat-7.0.54.tar.gz
將解壓生成的文件夾移動到/usr/local/下,並更名爲tomcat7[root@tomcat-1 ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat7
配置tomcat環境變量
/etc/profile文件內容以下:
export JAVAHOME=/usr/local/java
export CATALINAHOME=/usr/local/tomcat7
export PATH=$JAVAHOME/bin:$CATALINAHOME/bin:$PATH
經過source命令執行profile文件,使其生效。[root@tomcat-1 ~]# source /etc/profile
[root@tomcat-1 ~]# echo $PATH
/usr/local/java/bin:/usr/local/tomcat7/bin:/usr/local/java/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/
bin:/root/bin
查看tomcat的版本信息[root@tomcat-1 ~]# catalina.sh version
Using CATALINABASE: /usr/local/tomcat7
Using CATALINAHOME: /usr/local/tomcat7
Using CATALINATMPDIR: /usr/local/tomcat7/temp
Using JREHOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.54
Server built: May 19 2014 10:26:15
Server number: 7.0.54.0
OS Name: Linux
OS Version: 3.10.0-327.el7.x8664
Architecture: amd64
JVM Version: 1.7.065-b17
JVM Vendor: Oracle Corporation
啓動tomcat[root@tomcat-1 ~]# /usr/local/tomcat7/bin/startup.sh
Using CATALINABASE: /usr/local/tomcat7
Using CATALINAHOME: /usr/local/tomcat7
Using CATALINATMPDIR: /usr/local/tomcat7/temp
Using JREHOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.
Tomcat默認運行在8080端口,運行netstat命令查看8080端口監聽的信息[root@tomcat-1 ~]# netstat -anpt | grep java
tcp6 0 0 :::8009 ::: LISTEN 42330/java
tcp6 0 0 :::8080 ::: LISTEN 42330/java
防火牆規則配置:[root@tomcat-1 ~]# firewall-cmd --permanent --add-port=8080/tcp
success[root@tomcat-1 ~]# firewall-cmd --reload
success
按照相同方法在tomcat-2也安裝
打開瀏覽器分別對tomcat-1和tomcat-2訪問測試
若是想關閉tomcat則運行/usr/local/tomcat7/bin/shutdown.sh命令
好了,你們能夠看到訪成功。說明咱們的tomcat安裝完成,下面咱們來修改配置文件[root@tomcat-1 ~]# vim /usr/local/tomcat7/conf/server.xml
設置默認虛擬主機,並增長jvmRoute*
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-1">
修改默認虛擬主機,並將網站文件路徑指向/web/webapp1,在host段增長context段
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="true"/>
</Host>
增長文檔目錄與測試文件[root@tomcat-1 ~]# mkdir -p /web/webapp1
[root@tomcat-1 ~]# cd /web/webapp1/
[root@ tomcat-1 webapp1]# vi index.jsp
index.jsp內容以下:linux
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>tomcat-1</title> </head> <body> <h1><font color="red">Session serviced by tomcat</font></h1> <table aligh="center" border="1"> <tr> <td>Session ID</td> <td><%=session.getId() %></td> <% session.setAttribute("abc","abc");%> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> <html>
中止tomcat運行,檢查配置文件並啓動tomcat[root@tomcat-1 ~]# shutdown.sh
[root@tomcat-1 ~]# netstat -anpt | grep java
[root@tomcat-1 ~]# catalina.sh configtest
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Nov 16, 2016 1:04:05 AM org.apache.catalina.core.AprLifecycleListenerinit
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production
environments was not found on the java.library.path:
/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Nov 16, 2016 1:04:05 AM org.apache.coyote.AbstractProtocolinit
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 16, 2016 1:04:05 AM org.apache.coyote.AbstractProtocolinit
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 16, 2016 1:04:05 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 534 ms
[root@tomcat-1 ~]# startup.sh
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.[root@tomcat-1 ~]# netstat -anpt | grep java
tcp6 0 0 :::8009 ::: LISTEN 8180/java
tcp6 0 0 :::8080 ::: LISTEN 8180/java
Tomcat-2節點與tomcat-1節點配置基本相似,只是jvmRoute不一樣,另外爲了區分由哪一個節點
提供訪問,測試頁標題也不一樣(生產環境兩個tomcat服務器提供的網頁內容是相同的)。其
他的配置都相同。
用瀏覽器訪問nginx主機,驗證負載均衡
nginx
未完!!!