簡單說壹下,除了壹些公司自主開發的集群外。常用的壹般有三種:
使用redis-trib.rb,這個是安裝redis時就自帶的壹種集群,采用了服務端分片的方式。Jedis使用JedisCluster類來訪問。
使用Jedis帶的客戶端分片ShardedJedisPool類。
使用代理進行分片twemproxy,連接代理可以使用Jedis類(單鏈接)和JedisPool類(多鏈接)。
下面提供壹個JedisCluster的例子:
JedisCluster cluster;
public void init() {
// 加載redis配置文件
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException("[redis.properties] is not found!");
}
// 創建jedis池配置實例
JedisPoolConfig config = new JedisPoolConfig();
// 設置池配置項值
config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive").trim()));
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle").trim()));
config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait").trim()));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow").trim()));
config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn").trim()));
Set<HostAndPort> hps = new HashSet<HostAndPort>();
hps.add(new HostAndPort("192.168.242.133", 4001));
hps.add(new HostAndPort("192.168.242.133", 4002));
hps.add(new HostAndPort("192.168.242.133", 4003));
hps.add(new HostAndPort("192.168.242.133", 4004));
cluster = new JedisCluster(hps, 2000, 5);
}
public void test() {
// 這裏就可以使用cluster進行各種redis的操作了(與Jedis類的接口類似)
cluster.set("key", "value");
}
如果要了解其它的,請留言給我。