# sharded-jedis-sentinel-pool
**Repository Path**: migrant/sharded-jedis-sentinel-pool
## Basic Information
- **Project Name**: sharded-jedis-sentinel-pool
- **Description**: No description available
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2016-09-01
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
Sharded jedis sentinel Pool
===========================
### Redis HA by using Redis Sentinel
* Requires Jedis2.2.2 or later;
* You can read [the blog](http://warm-breeze.iteye.com/blog/2020413) to understand and use it.
Redis主从复制和主从切换
http://blog.csdn.net/zhu_tianwei/article/details/44980985
配置主从复制
建立从文件夹,譬如 /usr/local/slaves/下建立 6380 6381 两个文件夹(两个从服务器)
复制redis.conf到刚建立的两个文件夹中
修改redis.conf 中的
port 6380
slaveof 127.0.0.1 6379 ------ip表示主服务器的ip 端口表示主服务器端口
保存退出,另一个从服务器做同样修改,如果端口不是6380,修改断开即可,此处修改为6381
启动主服务器 redis-server redis.conf
进入从服务器文件夹,启动从服务器
查看主服务器信息:redis-cli -p 6379 info Replication,可以看到有两个从服务器
redis-cli -p 6379 进去主服务器,存储数据 set key val
进入从服务器,redis-cli -p 6380 查看数据 get key 查看是否能取出数据
配置主从切换
新建文件 sentinel.conf
编辑文件
####master sentinel.conf
##sentinel实例之间的通讯端口
port 26379
####sentinel需要监控的master信息: .
####应该小于集群中slave的个数,只有当至少个sentinel实例提交"master失效" 才会认为master为ODWON("客观"失效) .
sentinel monitor mymaster 127.0.0.1 6381 1
sentinel down-after-milliseconds mymaster 1000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
其他两个文件只需要修改端口即可
启动redis-sentinel sentinel.conf &
通过java读写主从服务器
需要添加Spring配置和其他两个jar包
UTF-8
3.2.9.RELEASE
org.apache.commons
commons-pool2
2.4.2
org.springframework.data
spring-data-redis
1.6.0.RELEASE
org.springframework
spring-context
${springVersion}
org.springframework
spring-tx
${springVersion}
org.springframework
spring-context-support
${springVersion}
cglib
cglib-nodep
3.1
org.apache.commons
commons-lang3
3.1
com.alibaba
fastjson
1.2.5
org.aspectj
aspectjweaver
1.8.2
junit
junit
4.8
test
net.sf.ehcache
ehcache
2.7.5
org.slf4j
slf4j-api
1.6.6
redis.clients
jedis
2.4.2
commons-pool
commons-pool
1.6
commons-logging
commons-logging
1.1.1
org.slf4j
slf4j-log4j12
1.7.10
test
Spring配置
java测试
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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.RedisTemplate;
/**
* Created by vincent on 15-10-13.
*/
public class CommonTest {
private ApplicationContext context ;
private RedisTemplate redisTemplate;
final String key ="key7";
@Before
public void init(){
context = new ClassPathXmlApplicationContext("applicationContext.xml");
redisTemplate= context.getBean("redisTemplate",RedisTemplate.class);
}
@Test
public void test1(){
redisTemplate.execute(new RedisCallback() {
@Override
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
redisConnection.set(key.getBytes(),(System.currentTimeMillis()+"").getBytes());
return 1L;
}
});
}
@Test
public void test2(){
Object execute = redisTemplate.execute(new RedisCallback() {
@Override
public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.get(key.getBytes());
}
});
System.out.println(new String((byte[])execute));
}
}