spring与jedis整合配置
1.引入jedis的jar包
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
2.在spring配置文件中添加配置
<!-- POOL配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="50" />
<property name="maxIdle" value="10" />
<property name="maxWaitMillis" value="1000" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- jedis shard信息配置 -->
<bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="192.168.1.101" />
<constructor-arg index="1" value="6379" type="int" />
</bean>
<!-- jedis shard pool配置 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedisShardInfo" />
</list>
</constructor-arg>
</bean>
经过以上两个步骤,即可完成配置
3. 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/spring/applicationContext.xml", "/spring/redis.xml"})
public class RedisTest {
@Autowired
private ShardedJedisPool shardedJedisPool;
@Test
public void testSet() {
ShardedJedis jedis = shardedJedisPool.getResource();
jedis.set("name", "wwss");
}
@Test
public void testGet() {
ShardedJedis jedis = shardedJedisPool.getResource();
String name = jedis.get("name");
System.out.println(name);
}
}