This article mainly introduces the installation and configuration of Redis, Nginx and Tomcat have written a very detailed document, please refer to: http://francis905.blog.51cto.com/3048056/1716740
Lab Environment: 3 virtual machines (pc1:nginx1.6.3,pc2:tomcat7+redis2.6.13,pc3:tomcat7+redis2.6.13)
Experiment Objective: To realize session sharing through Redis
[Note] Brown is an extension of other materials that must be done
Cd/usr/local/src
Tar zxvf redis-2.6.13.tar.gz
CD redis-2.6.13
Make Prefix=/usr/local/redis Install
If make fails, the GCC is not installed in your system. Yum install-y gcc
After successful installation,/usr/loca/redis/bin will have these 5 files
[Email protected] redis-2.6.13]# ls/usr/local/redis/bin/
Redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server
Redis-server:redis Server Daemon Startup program
Redis-cli:redis command-line operation tool. Of course, you can also use Telnet to operate on its plain text protocol.
Redis-benchmark:redis Performance testing tools to test the read and write performance of Redis in your system and in your configuration
REDIS-CHECK-AOF: Data Repair
Redis-check-dump: Check the Export tool
Here, if you do not start the script can be started directly:/usr/loca/redis/bin/redis-server
Cp/usr/local/src/redis-2.6.13/utils/redis_init_script/etc/init.d/redis//Copy startup script
[Email protected] redis-2.6.13]# chkconfig--add Redis
Redis service does not support Chkconfig
Vim/etc/init.d/redis
..................................................................................
#!/bin/sh
# chkconfig:2345 80 90//Resolution service does not support chkconfig issues
# simple Redis INIT.D script conceived to work on Linux systems
# as it does use of the/proc filesystem.
redisport=6379
Exec=/usr/local/redis/bin/redis-server//modify Path
CLIEXEC=/USR/LOCAL/REDIS/BIN/REDIS-CLI//modify Path
Pidfile=/var/run/redis_${redisport}.pid
conf= "/etc/redis/${redisport}.conf"
Case "$" in
Start
If [-F $PIDFILE]
Then
echo "$PIDFILE exists, process is already running or crashed"
Else
echo "Starting Redis server ..."
$EXEC $CONF &//Join & perform in the background
Fi
;;
Stop
if [!-F $PIDFILE]
Then
echo "$PIDFILE does not exist, process was not running"
Else
pid=$ (Cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC-P $REDISPORT shutdown
While [-x/proc/${pid}]
Do
echo "Waiting for Redis to shutdown ..."
Sleep 1
Done
echo "Redis stopped"
Fi
;;
*)
echo "Please use Start or stop as first argument"
;;
Esac
..................................................................................
Copy the Redis configuration file to/etc/redis/${redisport}. conf
Mkdir/etc/redis
cp/usr/local/src/redis-2.6.13/redis.conf/etc/redis/6379. conf
Parse the configuration file that needs to define Redis yourself
mkdir/usr/local/redis/etc/
mkdir/usr/local/redis/var/
Vim/usr/local/redis/etc/redis.conf
.............................................................
Daemonize Yes
Pidfile/usr/local/redis/var/redis.pid
Port 6379
Timeout 300
LogLevel Debug
Logfile/usr/local/redis/var/redis.log
Databases 16
Save 900 1
Save 300 10
Save 60 10000
Rdbcompression Yes
Dbfilename Dump.rdb
dir/usr/local/redis/var/
AppendOnly No
Appendfsync always
.............................................................
Specify the configuration file when starting/usr/local/redis/bin/redis-server/usr/loca/redis/etc/redis.conf
conf the meaning of the main configuration parameters:
Daemonize: Whether to run daemon mode later
Pidfile:pid File Location
Port: Port number for listening
Timeout: Request time-out
Loglevel:log Information level
Logfile:log File Location
Databases: number of open databases
Save *: How often the snapshot is saved, the first * indicates how long, and the third * indicates how many times the write operation is performed. Snapshots are automatically saved when a certain number of writes are performed within a certain amount of time. You can set multiple conditions.
Rdbcompression: Whether to use compression
Dbfilename: Data Snapshot file name (only file name, excluding directory)
Dir: Save directory for Data snapshot (this is the directory)
AppendOnly: If the appendonlylog is turned on, each write will record a log, which will improve the data anti-risk ability, but affect the efficiency.
Appendfsync:appendonlylog How to sync to disk (three options, each write is forced to call Fsync, Fsync per second, do not call Fsync wait for the system to synchronize itself)
Chkconfig--add Redis
Chkconfig Redis on
Service Redis Start
Add the directory of the Redis command to the system parameter path
Vim/etc/profile
Export path= "$PATH:/usr/local/redis/bin"
Source/etc/profile
Start Redis
/usr/local/redis/bin/redis-server/usr/local/redis/etc/redis.conf
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/77/47/wKiom1ZmXBWwiTJFAACA86Po9G8491.png "title=" Start.png "alt=" Wkiom1zmxbwwitjfaaca86po9g8491.png "/>
Turn off Redis
REDIS-CLI shutdown
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/77/45/wKioL1ZmXJODDhFvAAA-RktSIYc539.png "title=" Shutdown.png "alt=" Wkiol1zmxjoddhfvaaa-rktsiyc539.png "/>
Call the REDIS-CLI command test
[Email protected] redis-2.6.13]# REDIS-CLI
Redis 127.0.0.1:6379> Auth nihao123! Added validation to users
Ok
Redis 127.0.0.1:6379> Ping
PONG
Redis 127.0.0.1:6379> set foo bar
Ok
Redis 127.0.0.1:6379> get foo
"Bar"
Redis 127.0.0.1:6379> Keys "*"//See the session Information experiment results can be used for this test
2 ways to add user authentication to Redis
1. Modify the configuration file redis.conf
Vim/etc/redis/6379.conf
260 Requirepass nihao123! 260 refers to the line number
Service Redis Stop;start
2, through the Config command can set the password and get the current user password
1. Redis 127.0.0.1:6379[1]> Config set requirepass 123456
2. OK
3. Redis 127.0.0.1:6379[1]> config get requirepass
4.1) "Requirepass"
5.2) "123456"
[Note:] After setting the authentication password, the Redis service shutdown requires password verification before it can be closed, so the command is modified to:
Redis-cli–a 123456 shutdown
PS: Redis does not support TOMCAT8 until 2015-05-12, for details see official website: Https://github.com/jcoleman/tomcat-redis-session-manager
Configure the Context.xml file under the Conf directory for Tomcat:
1> Single Point Reids configuration
<!--
Jedis Save Session
-
<valve classname= "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<manager classname= "Com.orangefunction.tomcat.redissessions.RedisSessionManager"
host= "localhost"//fill in the IP you want to designate as the Redis service
Port= "6379"
database= "0"
Maxinactiveinterval= "/>"
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/77/47/wKiom1ZmW42hrVaSAACFO6ozKLs226.png "title=" 1.png " alt= "Wkiom1zmw42hrvasaacfo6ozkls226.png"/>
2> Sentinel Cluster Configuration:
<!--Sentinel Configuration--
<valve classname= "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<manager classname= "Com.orangefunction.tomcat.redissessions.RedisSessionManager"
Maxinactiveinterval= "60"
Sentinelmaster= "MyMaster"
Sentinels= "127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382"
/>
#集群配置没验证 follow-up documentation will do this.
[Note]: The session persistence setting is turned on by default from Tomcat6, the local session persistence can be turned off during the test, and in the Context.xml file under Tomcat's Conf directory, uncomment the following configuration:
<!--uncomment this to disable session persistence across Tomcat restarts-
<!--
<manager pathname= ""/>
-
Redis Exception Resolution: Jedis.exceptions.JedisDataException:ERR Client sent AUTH, but no password is set
Redis sets the access password through the property Requirepass, but when the client sends a AUTH request to the server, the service side returns the exception: ERR Client sent AUTH, but no password is set Comment out the Redis configuration file Requirepass.
Tomcat to easily see test results, write a new Java code
vim/usr/local/tomcat7/webapps/root/index.jsp
.........................................................................
<%@ page language= "java"%>
<body>
<table align= "centre" border= "1" >
<tr>
<td>session id</td>
<td><%= Session.getid ()%></td>
</tr>
<tr>
<td>created on</td>
<td><%= session.getcreationtime ()%></td>
</tr>
</table>
</body>
Sessionid:<%=session.getid ()%>
<br>
Sessionip:<%=request.getservername ()%>
<br>
Sessionport:<%=request.getserverport ()%>
<%
Out.println ("This tomcat server is 192.168.3.12");
%>
.........................................................................
#两台tomcat都是如此,, in order to differentiate the last IP address to modify their own
Configure Nginx Reverse proxy, refer to the URL mentioned at the beginning
Start Redis, Tomcat, Nginx
You can see the effect:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/77/47/wKiom1ZmXZaiH_vRAABA-9DylW0087.png "style=" float: none; "title=" 12.png "alt=" Wkiom1zmxzaih_vraaba-9dylw0087.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/77/47/wKiom1ZmXZfTC-0AAABCQmEdMzo848.png "style=" float: none; "title=" 13.png "alt=" Wkiom1zmxzftc-0aaabcqmedmzo848.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/77/45/wKioL1ZmXqHyT_EvAACikUoEV1s071.png "title=" 11111. PNG "alt=" Wkiol1zmxqhyt_evaacikuoev1s071.png "/>
This article is from the "Rookie Growth Road" blog, please be sure to keep this source http://francis905.blog.51cto.com/3048056/1720693
Nginx+tomcat7+redis for session sharing (single-point Redis)