标签:
1>redis安装
mkdir -p /usr/local/webserver/redis //安装目录
cd /usr/local/webserver/redis
wget http://download.redis.io/redis-stable.tar.gz //最新稳定版
tar xzf redis-stable.tar.gz
cd redis-stable
make //报错的话 install gcc
make install //redis可执行文件redis-cli,redis-server等会被复制到/usr/local/bin,命令行直接输入即可执行
2>主配置文件redis_master.conf做如下修改:
daemonize yes //使redis能以守护进程模式运行
pidfile /var/run/redis_6379.pid //设置redis PID 文件位置
port 6379
dir /var/redis/6379 //设置持久化文件存放位置
3>启动master
[root@redis-sentinel redis-stable]# redis-server redis_master.conf
[root@redis-sentinel redis-stable]# ps -ef|grep redis
root 5504 1 0 12:41 ? 00:00:00 redis-server 127.0.0.1:6379
root 5508 1361 0 12:41 pts/0 00:00:00 grep redis
4>使用客户端连接测试
[root@redis-sentinel redis-stable]# redis-cli -p 6379
127.0.0.1:6379> set ww 1
OK
127.0.0.1:6379> get ww
"1"
1>从配置文件redis_slave_6380.conf,redis_slave_6381.conf做如下修改:
daemonize yes //使redis能以守护进程模式运行
pidfile /var/run/redis_6380.pid //设置redis PID 文件位置
port 6380
dir /var/redis/6380 //设置持久化文件存放位置
slaveof 127.0.0.1 6379 //设置属于哪个master
daemonize yes //使redis能以守护进程模式运行
pidfile /var/run/redis_6381.pid //设置redis PID 文件位置
port 6381
dir /var/redis/6381 //设置持久化文件存放位置
slaveof 127.0.0.1 6379 //设置属于哪个master
2>启动slave并测试数据是否同步
[root@redis-sentinel redis-stable]# redis-server redis_slave_6380.conf
[root@redis-sentinel redis-stable]# ps -ef|grep redis
root 5504 1 0 12:41 ? 00:00:02 redis-server 127.0.0.1:6379
root 5555 1 0 14:47 ? 00:00:00 redis-server 127.0.0.1:6380
root 5561 1361 0 14:48 pts/0 00:00:00 grep redis
[root@redis-sentinel redis-stable]# redis-cli -p 6380
127.0.0.1:6380> get ww
"1" //数据已经同步过来
[root@redis-sentinel redis-stable]# redis-server redis_slave_6381.conf
[root@redis-sentinel redis-stable]# ps -ef|grep redis
root 5504 1 0 12:41 ? 00:00:02 redis-server 127.0.0.1:6379
root 5555 1 0 14:47 ? 00:00:00 redis-server 127.0.0.1:6380
root 5557 1 0 14:48 ? 00:00:00 redis-server 127.0.0.1:6381
root 5561 1361 0 14:48 pts/0 00:00:00 grep redis
[root@redis-sentinel redis-stable]# redis-cli -p 6381
127.0.0.1:6380> get ww
"1" //数据已经同步过来
////sentinel_26379.conf
port 26379
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel myid 1c805b9134364d69986f9042c22e95debf1932c9
//sentinel_26380.conf
port 26380
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel myid ac1ef015411583d4b9f3d81cee830060b2f29862
//sentinel_26381.conf
port 26381
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel myid 0aca3a57038e2907c8a07be2b3c0d15171e44da5
[root@redis-sentinel redis-stable]# redis-sentinel sentinel_26379.conf
[root@redis-sentinel redis-stable]# redis-sentinel sentinel_26380.conf
[root@redis-sentinel redis-stable]# redis-sentinel sentinel_26381.conf
[root@redis-sentinel redis-stable]# ps -ef|grep redis
root 5504 1 0 12:41 ? 00:00:03 redis-server 127.0.0.1:6379
root 5555 1 0 14:47 ? 00:00:00 redis-server 127.0.0.1:6380
root 5606 1 0 15:23 ? 00:00:00 redis-server 127.0.0.1:6381
root 5770 1361 0 15:54 pts/0 00:00:00 redis-sentinel *:26379 [sentinel]
root 5773 5429 0 15:54 pts/1 00:00:00 redis-sentinel *:26380 [sentinel]
root 5776 5710 0 15:55 pts/2 00:00:00 redis-sentinel *:26381 [sentinel]
root 5781 5743 0 15:57 pts/3 00:00:00 grep redis
[root@redis-sentinel redis-stable]# redis-cli -p 26379
127.0.0.1:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "runid"
8) "83491622b984fa6bbb4cd7761c77b23491569b1b"
9) "flags"
10) "master"
...
127.0.0.1:26379> SENTINEL slaves mymaster
1) 1) "name"
2) "127.0.0.1:6380"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6380"
7) "runid"
8) "e532cc9e510bf93a6c3dd5d978be8364c91c616f"
9) "flags"
10) "slave"
...
2) 1) "name"
2) "127.0.0.1:6381"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6381"
7) "runid"
8) "f7cee9c9f6e5a346d38f2a09dfc9eb4846118ae5"
9) "flags"
10) "slave"
...
127.0.0.1:26379> SENTINEL get-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6379"
[root@redis-sentinel redis-stable]# kill -9 5504
[root@redis-sentinel redis-stable]# ps -ef|grep redis
root 5555 1 0 14:47 ? 00:00:00 redis-server 127.0.0.1:6380
root 5606 1 0 15:23 ? 00:00:00 redis-server 127.0.0.1:6381
root 5770 1361 0 15:54 pts/0 00:00:00 redis-sentinel *:26379 [sentinel]
root 5773 5429 0 15:54 pts/1 00:00:00 redis-sentinel *:26380 [sentinel]
root 5776 5710 0 15:55 pts/2 00:00:00 redis-sentinel *:26381 [sentinel]
root 5789 5743 0 15:59 pts/3 00:00:00 grep redis
//主库出现故障时自动将从库转换为主库
[root@redis-sentinel redis-stable]# redis-cli -p 26379
127.0.0.1:26379> SENTINEL get-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6380"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans">
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="1000"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="1"/>
<property name="maxWaitMillis" value="30000"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
</bean>
<bean id="cacheService" class="sentinel.CacheServiceImpl" destroy-method="destroy">
<property name="jedisSentinlePool">
<bean class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg index="0" value="mymaster" />
<constructor-arg index="1">
<set>
<value>192.168.13.128:26379</value>
<value>192.168.13.128:26380</value>
<value>192.168.13.128:26381</value>
</set>
</constructor-arg>
<constructor-arg index="2" ref="jedisPoolConfig" />
</bean>
</property>
</bean>
</beans>
public class CacheServiceImpl implements CacheService {
private static Logger logger = LoggerFactory.getLogger(CacheServiceImpl.class);
private JedisSentinelPool jedisSentinlePool;
public void set(String key, String value) {
JedisSentinelPool pool = getJedisSentinelPool();
Jedis jedis = pool.getResource();
try {
jedis.set(key, value);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
}finally {
if(pool!=null){
pool.returnResource(jedis);
}
}
}
}
public interface CacheService {
public void set(String key, String value);
}
@Controller
public class GoodsController extends BaseNoLoginController {
@Autowired
private CacheService cacheService;
...
}
标签:
原文地址:http://www.cnblogs.com/wangweiNB/p/5620845.html