Redis客户端和spring

1. pom配置  

     <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>

2.spring配置

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal"  value="10" />
        <property name="maxIdle" value="10" />
        <property name="minIdle" value="1" />
    </bean>




    <bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
        <constructor-arg index="0" value="10.16.3.2" />
        <constructor-arg index="1" value="6379" type="int"/>
        <property name="password"  value="123456"/>
    </bean>




    <bean id="jedisPool" class="redis.clients.jedis.ShardedJedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <ref bean="jedisShardInfo" />
            </list>
        </constructor-arg>
    </bean>

3.java test

ShardedJedisPool jedisPool = (ShardedJedisPool)AppContextUtil.getContext().getBean("jedisPool");

ShardedJedis jedis = jedisPool.getResource();
// 执行set指令
String result = jedis.set("key-string", "Hello, Redis!");
System.out.println( String.format("set指令执行结果:%s", result) );


// 执行get指令
String value = jedis.get("key-string");
System.out.println( String.format("get指令执行结果:%s", value) );



你可能感兴趣的:(Redis客户端和spring)