23

Following is Jedis documentation directly copied from jedis github page:

List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo("localhost", 6379);
si.setPassword("foobared");
shards.add(si);
si = new JedisShardInfo("localhost", 6380);
si.setPassword("foobared");
shards.add(si);

Then, there are two ways of using ShardedJedis. Direct connections or by using ShardedJedisPool. For reliable operation, the latter has to be used in a multithreaded environment.

2.a) Direct connection:

ShardedJedis jedis = new ShardedJedis(shards);
jedis.set("a", "foo");
jedis.disconnect;

2.b) Pooled connection:

ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource();
jedis.set("a", "foo");
.... // do your work here
pool.returnResource(jedis);
.... // a few moments later
ShardedJedis jedis2 = pool.getResource();
jedis.set("z", "bar");
pool.returnResource(jedis);
pool.destroy();

Above example shows how to use ShardedJedis.

In my current setup, I am using RedisTemplate and JedisConnectionFactory.

My question is

How do I use ShardedJedis with RedisTemplate?

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • This is old, but at that time, the reply says it doesn't support shards. http://forum.spring.io/forum/spring-projects/data/nosql/104599-how-to-connect-to-multiple-redis-instances-using-redistemplate – Alper Akture Sep 01 '15 at 21:04

2 Answers2

1

I think it doesn't directly support your case. RedisTemplate offers a high-level abstraction for Redis interactions. While RedisConnection offers low level methods that accept and return binary values (byte arrays).

See: Working with Objects through RedisTemplate

Nebras
  • 636
  • 1
  • 7
  • 16
0

I think u can config jedis in spring.xml with below code: and of course you should create a JedisClient.java as an interface

<!-- redis cluster -->
<!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
    <constructor-arg>
        <set>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis01.real.cluster.client.host}"/>
                <constructor-arg name="port" value="${redis01.real.cluster.client.port}"/>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis02.real.cluster.client.host}"/>
                <constructor-arg name="port" value="${redis02.real.cluster.client.port}"/>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis03.real.cluster.client.host}"/>
                <constructor-arg name="port" value="${redis03.real.cluster.client.port}"/>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis04.real.cluster.client.host}"/>
                <constructor-arg name="port" value="${redis04.real.cluster.client.port}"/>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis05.real.cluster.client.host}"/>
                <constructor-arg name="port" value="${redis05.real.cluster.client.port}"/>
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis06.real.cluster.client.host}"/>
                <constructor-arg name="port" value="${redis06.real.cluster.client.port}"/>
            </bean>
        </set>
    </constructor-arg>
</bean>
<bean id="jedisClientCluster" class="com.dingli.rest.component.impl.JedisClientCluster"/> -->