当前位置: 移动技术网 > IT编程>开发语言>Java > Spring集成jedis的配置与使用简单实例

Spring集成jedis的配置与使用简单实例

2019年07月19日  | 移动技术网IT编程  | 我要评论

jedis是redis的java客户端,spring将redis连接池作为一个bean配置。

redis连接池分为两种,一种是“redis.clients.jedis.shardedjedispool”,这是基于hash算法的一种分布式集群redis客户端连接池。

另一种是“redis.clients.jedis.jedispool”,这是单机环境适用的redis连接池。

maven导入相关包:

  <!-- redis依赖包 -->
  <dependency>
   <groupid>redis.clients</groupid>
   <artifactid>jedis</artifactid>
   <version>2.9.0</version>
  </dependency>

shardedjedispool是redis集群客户端的对象池,可以通过他来操作shardedjedis,下面是shardedjedispool的xml配置,spring-jedis.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 引入jedis的properties配置文件 -->
  <!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
  <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />
  <!--shardedjedispool的相关配置-->
  <bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">
    <!--新版是maxtotal,旧版是maxactive-->
    <property name="maxtotal">
      <value>${redis.pool.maxactive}</value>
    </property>
    <property name="maxidle">
      <value>${redis.pool.maxidle}</value>
    </property>
    <property name="testonborrow" value="true"/>
    <property name="testonreturn" value="true"/>
  </bean>
  <bean id="shardedjedispool" class="redis.clients.jedis.shardedjedispool" scope="singleton">
    <constructor-arg index="0" ref="jedispoolconfig" />
    <constructor-arg index="1">
      <list>
        <bean class="redis.clients.jedis.jedisshardinfo">
          <constructor-arg name="host" value="${redis.uri}" />
        </bean>
      </list>
    </constructor-arg>
  </bean>
</beans>

下面是单机环境下redis连接池的配置:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 引入jedis的properties配置文件 -->
  <!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
  <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />
  <!--jedis连接池的相关配置-->
  <bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">
    <!--新版是maxtotal,旧版是maxactive-->
    <property name="maxtotal">
      <value>${redis.pool.maxactive}</value>
    </property>
    <property name="maxidle">
      <value>${redis.pool.maxidle}</value>
    </property>
    <property name="testonborrow" value="true"/>
    <property name="testonreturn" value="true"/>
  </bean>
  <bean id="jedispool" class="redis.clients.jedis.jedispool">
    <constructor-arg name="poolconfig" ref="jedispoolconfig" />
    <constructor-arg name="host" value="${redis.host}" />
    <constructor-arg name="port" value="${redis.port}" type="int" />
    <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
    <constructor-arg name="password" value="${redis.password}" />
    <constructor-arg name="database" value="${redis.database}" type="int" />
  </bean>
</beans>

对应的classpath:properties/redis.properties.xml为:

#最大分配的对象数
redis.pool.maxactive=200
#最大能够保持idel状态的对象数
redis.pool.maxidle=50
redis.pool.minidle=10
redis.pool.maxwaitmillis=20000
#当池内没有返回对象时,最大等待时间
redis.pool.maxwait=300
#格式:redis://:[密码]@[服务器地址]:[端口]/[db index]
redis.uri = redis://:12345@127.0.0.1:6379/0
redis.host = 127.0.0.1
redis.port = 6379
redis.timeout=30000
redis.password = 12345
redis.database = 0

二者操作代码类似,都是先注入连接池,然后通过连接池获得jedis实例,通过实例对象操作redis。

shardedjedis操作:

  @autowired
  private shardedjedispool shardedjedispool;//注入shardedjedispool
  @requestmapping(value = "/demo_set",method = requestmethod.get)
  @responsebody
  public string demo_set(){
    //获取shardedjedis对象
    shardedjedis shardjedis = shardedjedispool.getresource();
    //存入键值对
    shardjedis.set("key1","hello jedis");
    //回收shardedjedis实例
    shardjedis.close();
    return "set";
  }
  @requestmapping(value = "/demo_get",method = requestmethod.get)
  @responsebody
  public string demo_get(){
    shardedjedis shardedjedis = shardedjedispool.getresource();
    //根据键值获得数据
    string result = shardedjedis.get("key1");
    shardedjedis.close();
    return result;
  }

jedis操作:

  @autowired
  private jedispool jedispool;//注入jedispool
  @requestmapping(value = "/demo_set",method = requestmethod.get)
  @responsebody
  public string demo_set(){
    //获取shardedjedis对象
    jedis jedis = jedispool.getresource();
    //存入键值对
    jedis.set("key2","hello jedis one");
    //回收shardedjedis实例
    jedis.close();
    return "set";
  }
  @requestmapping(value = "/demo_get",method = requestmethod.get)
  @responsebody
  public string demo_get(){
    jedis jedis = jedispool.getresource();
    //根据键值获得数据
    string result = jedis.get("key2");
    jedis.close();
    return result;
  }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网