redis 集群时jedis该怎么配置

redis 集群时jedis该怎么配置
最新回答
一枝沈荼

2023-08-10 12:23:43

简单说一下,除了一些公司自主开发的集群外。常用的一般有三种:

  1. 使用redis-trib.rb,这个是安装redis时就自带的一种集群,采用了服务端分片的方式。Jedis使用JedisCluster类来访问。

  2. 使用Jedis带的客户端分片ShardedJedisPool类。

  3. 使用代理进行分片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");

}

如果要了解其它的,请留言给我。

銱儿鎯筜

2024-01-11 22:02:17

补助狗谠死物辽必
收起 1条折叠回答