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; } }