1.首先介紹redis在linux系統下的安裝方法。
wget http://download.redis.io/releases/redis-2.8.3.tar.gz獲取redis安裝文件
tar xzf redis-2.8.3.tar.gz解壓
cd redis-2.8.3
make
進入目錄然後安裝。
然後將
redis-benchmark redis-cli redis.conf redis-sentinel redis-server
這幾個文件拷貝到/usr/redis目錄下。
然後就可以啓動redis了。
redis啓動,首先進入redis
然後執行以下命令:
./redis-server ./redis.conf
測試:
root@localhost redis]# ./redis-cli
127.0.0.1:6379> get bar
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth lgx
OK
127.0.0.1:6379> get bar
"foo"
127.0.0.1:6379> quit
[root@localhost redis]#
關閉redis:
./redis-cli shutdown
2.下面介紹redis如何配置哨兵模式
我們可以用redis開啓不同的端口來模擬
一個哨兵,一個主redis,兩個從redis
sentinel_63791.conf 配置:
port 63791
daemonize yes
logfile "/usr/redis/sentinel_63791.log"
#master
sentinel monitor myaster 192.168.183.130
6379 1
sentinel down-after-milliseconds myaster 5000
sentinel failover-timeout mymaster 18000
sentinel auth-pass myaster yingjun
sentinel parallel-syncs mymaster 1
redis_master_6379.conf 配置:
在原配置文件中作如下修改:
port 6379
daemonize yes
requirepass lgx
masterauth lgx
redis_slave_6380.conf 配置:
在原配置文件中作如下修改:
port 6380
daemonize yes
requirepass lgx
slaveof 192.168.183.130 6379
masterauth lgx
redis_slave_6381.conf 配置:
在原配置文件中作如下修改:
port 6381
daemonize yes
requirepass lgx
slaveof 192.168.183.130 6379
masterauth lgx
按如下順序依次啓動服務:
./redis-server ./redis_master_6379.conf
./redis-server ./redis_slave_6381.conf
./redis-server ./redis_slave_6382.conf
./redis-sentinel ./sentinel_63791.conf
然後就可以測試了
下面是測試主redis停止了之後,主從切換之後的狀況
可以看出主redis變成了從redis
從redis變成了主redis。
原理圖:
3。代碼測試
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:product.properties</value> </list> </property> </bean> <!-- Redis 連接池配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}"/> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="minIdle" value="${redis.minIdle}"/> <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/> <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> <property name="testOnReturn" value="${redis.testOnReturn}"/> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/> <property name="softMinEvictableIdleTimeMillis" value="${redis.softMinEvictableIdleTimeMillis}"/> </bean> <!-- spring data redis JedisConnectionFactory--> <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="${redis.clustername}"></property> </bean> </property> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg index="0" value="${redis.host}"/> <constructor-arg index="1" value="${redis.port}"/> </bean> </set> </property> </bean> <!-- Redis 數據源配置 --> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="redisSentinelConfiguration"/> <property name="poolConfig" ref="poolConfig" /> <property name="password" value="${redis.pass}"></property> </bean> <!-- Redis 調用客戶端配置 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="redisConnectionFactory" /> </bean> </beans>
package cart; import com.alibaba.fastjson.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; /** * Created by Maggie on 2017/6/14. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-redis.xml") public class RedisTest { @Autowired private StringRedisTemplate redisTemplate; @Test public void insertTest(){ Map p=new HashMap(); p.put("name","li"); p.put("age","24"); final String value = toJSONString(p); boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { byte [] redisKey = redisTemplate.getStringSerializer().serialize("person.liguoxiong"); byte [] redisValue = redisTemplate.getStringSerializer().serialize(value); connection.set(redisKey,redisValue); return true; } }); System.out.println(result); } @Test public void getTest(){ final String key ="person.liguoxiong"; String resultStr = (String)redisTemplate.execute(new RedisCallback<Object>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { byte[] redisKey = redisTemplate.getStringSerializer().serialize(key); if (connection.exists(redisKey)) { byte[] value = connection.get(redisKey); return redisTemplate.getStringSerializer().deserialize(value); } return null; } }); System.out.println(resultStr); } public String toJSONString(Object obj){ final String value ; if(obj instanceof String) value = obj + ""; }else{ value = JSONObject.toJSONString(obj); } return value; } }