jedis整合spring详解

来源:互联网 发布:卡拉菲珠宝骗局 知乎 编辑:程序博客网 时间:2024/05/01 05:58

      使用jedis,能很好的对redis缓存进行操作,下面是博主在学习之中获得的心得

      1.如果只在服务器上部署一个redis缓存数据库,则在applicationcontext中是这样的

<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<!-->数据库服务器地址<-->
<constructor-arg name="host" value="127.0.0.1"></constructor-arg><!-->端口号<-->
<constructor-arg name="port" value="6379"></constructor-arg>
<!-->连接池<--><constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg></bean>

 

    其中jedis是自带连接池的,所以连接池的配置可配可不配,如果不配置则使用默认的连接池,如果你认为你的配置比默认配置更优秀也可以进行配置,连接池配置如下

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><!-- 最大连接数 --><property name="maxTotal" value="30" /><!-- 最大空闲连接数 --><property name="maxIdle" value="10" /><!-- 每次释放连接的最大数目 --><property name="numTestsPerEvictionRun" value="1024" /><!-- 释放连接的扫描间隔(毫秒) --><property name="timeBetweenEvictionRunsMillis" value="30000" /><!-- 连接最小空闲时间 --><property name="minEvictableIdleTimeMillis" value="1800000" /><!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --><property name="softMinEvictableIdleTimeMillis" value="10000" /><!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --><property name="maxWaitMillis" value="1500" /><!-- 在获取连接的时候检查有效性, 默认false --><property name="testOnBorrow" value="true" /><!-- 在空闲时检查有效性, 默认false --><property name="testWhileIdle" value="true" /><!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --><property name="blockWhenExhausted" value="false" /></bean>

   与大多数连接池如c3p0没有什么区别,再通过Junit测试一下
@Testpublic void testSpringJedisSingle() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");Jedis jedis = pool.getResource();jedis.close();
绿条,测试通过
   2。如果是在不同的服务器上部署了很多redis数据库,则在applicationcontext中的配置稍有不同
<bean id="redisClient" class="redis.clients.jedis.JedisCluster"><constructor-arg name="nodes"><set><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="127.0.0.1 "></constructor-arg><constructor-arg name="port" value="7001"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="127.0.0.1"></constructor-arg><constructor-arg name="port" value="7002"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="127.0.0.1"></constructor-arg><constructor-arg name="port" value="7003"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="127.0.0.1"></constructor-arg><constructor-arg name="port" value="7004"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="127.0.0.1"></constructor-arg><constructor-arg name="port" value="7005"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="192.168.25.153"></constructor-arg><constructor-arg name="port" value="7006"></constructor-arg></bean></set></constructor-arg><constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg></bean>


测试集群是否配置正确
@Testpublic void testSpringJedisCluster() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");JedisCluster jedisCluster =  (JedisCluster) applicationContext.getBean("redisClient");jedisCluster.close();}



绿条,通过


0 0