?文章來源地址http://www.zghlxwxcb.cn/news/detail-605600.html
目錄
一、服務(wù)器配置
二、安裝nginx
三、安裝配置Tomcat:
?四、配置session
Session服務(wù)器之Redis
Redis與Memcached的區(qū)別
安裝部署redis
一、服務(wù)器配置
IP地址 |
主機名 |
軟件包列表 |
192.168.100.131 |
huyang1 |
nginx |
192.168.100.133 |
huyang3 |
JDK Tomcat ? |
192.168.100.135 |
huyang5 |
JDK Tomcat |
二、安裝nginx
配置主機名:
[root@localhost ~]# hostname nginx
[root@localhost ~]# bash
安裝nginx軟件包并修改:
[root@nginx ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@nginx ~]# useradd -s /sbin/nologin -M nginx
[root@nginx ~]# tar xf nginx-1.15.9.tar.gz -C /usr/src/
[root@nginx ~]# cd /usr/src/nginx-1.15.9/
[root@nginx ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
--prefix=/usr/local/nginx #指定安裝目錄
--user=nginx --group=nginx #指定運行的用戶和組
--with-file-aio #啟用文件修改支持
--with-http_stub_status_module #啟用狀態(tài)統(tǒng)計
--with-http_ssl_module #啟用ssl模塊
--with-http_flv_module #啟用flv模塊,提供尋求內(nèi)存使用基于時間的偏移量文件
--with-http_gzip_static_module #啟用gzip靜態(tài)壓縮
[root@nginx ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
34 upstream tomcat_pool {
35 server 192.168.100.133:8080 weight=1 max_fails=1 fail_timeout=10s;
36 server 192.168.100.135:8080 weight=1 max_fails=1 fail_timeout=10s;
37 }
38
39 server {
40 listen 80;
41 server_name localhost;
42 charset utf-8;
43
44 location / {
45 root html;
46 index index.html index.htm;
47 proxy_pass http://tomcat_pool;
48 }
[root@nginx ~]# nginx
三、安裝配置Tomcat:
配置主機名:
[root@localhost ~]# hostname node1 另外一臺機器配置為node2
[root@localhost ~]# bash
安裝配置Tomcat
解壓apache-tomcat-7.0.54.tar.gz 包
[root@tomcat1 ~]# tar xf apache-tomcat-7.0.54.tar.gz
解壓后生成apache-tomcat-7.0.54文件夾,將該文件夾移動到/usr/local下,并改名為tomcat
[root@tomcat1 ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat
啟動Tomcat
[root@tomcat1 ~]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
Tomcat 默認運行在8080端口
[root@tomcat1 ~]# netstat -lnpt | grep java
tcp 0 0 :::8080 :::* LISTEN 3318/java
瀏覽器訪問測試 192.168.100.133:8080 和 192.168.100.135:8080
均呈現(xiàn)下列圖形!
?四、配置session
建立session.jsp的測試頁面
[root@node1 ~]# vim /usr/local/tomcat/webapps/ROOT/session.jsp
Session ID:<%= session.getId() %><BR>
SessionPort:<%= request.getServerPort() %>
<% out.println("This tomcat server 192.168.100.133");%>
[root@node2 ~]# vim /usr/local/tomcat/webapps/ROOT/session.jsp
Session ID:<%= session.getId() %><BR>
SessionPort:<%= request.getServerPort() %>
<% out.println("This tomcat server 192.168.100.135");%>
Session綁定:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
34 upstream tomcat_pool {
35 ip_hash;
36 server 192.168.100.133:8080 weight=1 max_fails=1 fail_timeout=10s;
37 server 192.168.100.135:8080 weight=1 max_fails=1 fail_timeout=10s;
38 }
[root@nginx ~]# killall -HUP nginx
?瀏覽器訪問測試 192.168.100.131/session.jsp
?刷新,id和IP地址都會變化
Session服務(wù)器之Redis
Redis與Memcached的區(qū)別
- 內(nèi)存利用率:使用簡單的key-value(鍵值對)存儲的話,Memcached的內(nèi)存利用率更高,而如果Redis采用hash結(jié)構(gòu)來做key-value存儲,由于其組合式的壓縮,其內(nèi)存利用率會高于Memcached。
- CPU性能對比:由于Redis只使用單核,而Memcached可以使用多核,所以平均每一個核上Redis在存儲小數(shù)據(jù)時比Memcached性能更高。而在100k以上的數(shù)據(jù)中Memcached性能要高于Redis,雖然Redis最近也在存儲大數(shù)據(jù)的性能上進行優(yōu)化,但是比起Memcached還是稍有遜色。
- Redis支持數(shù)據(jù)的持久化,可以將內(nèi)存中的數(shù)據(jù)保持在磁盤中,重啟的時候可以再次加載進行使用。
- Redis支持數(shù)據(jù)的備份,即master-slave模式的數(shù)據(jù)備份。
- Redis不僅僅支持簡單的key-Value類型的數(shù)據(jù),同時還提供list,set,zset,hash等數(shù)據(jù)結(jié)構(gòu)的存儲。
安裝部署redis
[root@huyang5 ~]# tar xf redis-6.2.12.tar.gz -C /usr/src/
[root@huyang5 ~]# cd /usr/src/redis-6.2.12/
[root@huyang5 redis-6.2.12]# make
[root@huyang5 redis-6.2.12]# mkdir -p /usr/local/redis/{bin,etc,var}
[root@huyang5 redis-6.2.12]# cd src/
[root@huyang5 src]# cp redis-benchmark ?redis-check-aof redis-cli redis-server ?/usr/local/redis/bin/
[root@huyang5 redis-6.2.12]# cp ../redis.conf ?/usr/local/redis/etc
[root@huyang5 redis-6.2.12]# cp ../sentinel.conf ?/usr/local/redis/etc
[root@huyang5 redis-6.2.12]# vim /usr/local/redis/etc/redis.conf //修改配置文件
[root@huyang5 redis-6.2.12]# killall -9 redis-server
啟動redis:
[root@huyang5 redis-6.2.12# /usr/local/redis/bin/redis-server ?&& ??/usr/local/redis/etc/redis.conf
[root@huyang5 redis-6.2.12]# netstat -anpt |grep redis
監(jiān)控redis共享session:
[root@huyang5 redis-6.2.12]# /usr/local/redis/bin/redis-cli -p 6379 ?monitor
[root@huyang5 redis-6.2.12]# cp tomcat-redis-session-manage-tomcat7.jar tomcat-juli.jar commons-logging-1.1.3.jar commons-pool2-2.2.jar jedis-2.5.2.jar /usr/local/tomcat/lib
修改context.xml文件以支持調(diào)用redis
[root@huyang5/3 redis-6.2.12]# vim /usr/local/tomcat/conf/context.xml
[root@huyang5 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@huyang5 ~]# /usr/local/tomcat/bin/startup.sh
[root@huyang5 redis-session]# netstat -anpt | grep :6379
[root@huyang3 ~]# netstat -anpt | grep :6379
瀏覽器訪問測試 http://192.168.100.131/session.jsp?
刷新一次:這樣就鎖定在了135的session id
同理也可以反過來鎖定133的session id
文章來源:http://www.zghlxwxcb.cn/news/detail-605600.html
?
到了這里,關(guān)于生產(chǎn)環(huán)境Session解決方案、Session服務(wù)器之Redis的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!