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

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;
  }

总结

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

(0)

相关推荐

  • SpringBoot集成shiro,MyRealm中无法@Autowired注入Service的问题

    网上说了很多诸如是Spring加载顺序,shiroFilter在Spring自动装配bean之前的问题,其实也有可能忽略如下低级错误. 在ShiroConfiguration中要使用@Bean在ApplicationContext注入MyRealm,不能直接new对象. 道理和Controller中调用Service一样,都要是SpringBean,不能自己new. 错误方式: @Bean(name = "securityManager") public SecurityManager

  • Spring各版本新特性的介绍

    Spring各个版本新特性 Spring3.1新特性 1.添加了引入环境profile功能 2.添加了@enable注解,使用特定功能 3.添加了对声明式缓存的支持,能够使用简单的注解声明缓存边界和规则 4.添加的用于构造器注入的c命名空间,类似与Spring2的p命名空间,用于对应属性注入 5.开始支持Servlet3.0,包括基于java配置中生命Servlet和Filter,不再只仅仅借助web.xml 6.改善对于JPA的支持,让JPA能在Spring中完整配置JPA,不必再使用pers

  • SpringBoot深入理解之内置web容器及配置的总结

    前言 在学会基本运用SpringBoot同时,想必搭过SSH.SSM等开发框架的小伙伴都有疑惑,SpringBoot在spring的基础上做了些什么,使得使用SpringBoot搭建开发框架能如此简单,便捷,快速.本系列文章记录网罗博客.分析源码.结合微薄经验后的总结,以便日后翻阅自省. 正文 使用SpringBoot时,首先引人注意的便是其启动方式,我们熟知的web项目都是需要部署到服务容器上,例如tomcat.weblogic.widefly(以前叫JBoss),然后启动web容器真正运行我

  • SpringBoot整个启动过程的分析

    前言 前一篇分析了SpringBoot如何启动以及内置web容器,这篇我们一起看一下SpringBoot的整个启动过程,废话不多说,正文开始. 正文 一.SpringBoot的启动类是**application,以注解@SpringBootApplication注明. @SpringBootApplication public class CmsApplication { public static void main(String[] args) { SpringApplication.run

  • mysql+spring+mybatis实现数据库读写分离的代码配置

    场景:一个读数据源一个读写数据源. 原理:借助spring的[org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource]这个抽象类实现,看名字可以了解到是一个路由数据源的东西,这个类中有一个方法 /** * Determine the current lookup key. This will typically be * implemented to check a thread-bound transaction

  • Spring配置shiro时自定义Realm中属性无法使用注解注入的解决办法

    先来看问题 纠结了几个小时终于找到了问题所在,因为shiro的realm属于Filter,简单说就是初始化realm时,spring还未加载相关业务Bean,那么解决办法就是将springmvc的配置文件加载提前. 解决办法 打开web.xml文件 OK,问题解决! 总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持.如果你想了解更多相关内容请查看下面相关链接

  • Spring线程池ThreadPoolExecutor配置并且得到任务执行的结果

    用ThreadPoolExecutor的时候,又想知道被执行的任务的执行情况,这时就可以用FutureTask. ThreadPoolTask package com.paul.threadPool; import java.io.Serializable; import java.util.concurrent.Callable; public class ThreadPoolTask implements Callable<String>, Serializable { private s

  • Spring Boot中使用Spring-data-jpa的配置方法详解

    为了解决这些大量枯燥的数据操作语句,我们第一个想到的是使用ORM框架,比如:hibernate.通过整合Hibernate之后,我们以操作Java实体的方式最终将数据改变映射到数据库表中. 为了解决抽象各个Java实体基本的"增删改查"操作,我们通常会以泛型的方式封装一个模板Dao来进行抽象简化,但是这样依然不是很方便,我们需要针对每个实体编写一个继承自泛型模板Dao的接口,再编写该接口的实现.虽然一些基础的数据访问已经可以得到很好的复用,但是在代码结构上针对每个实体都会有一堆Dao的

  • Spring框架十一种常见异常的解决方法汇总

    在程序员生涯当中,提到最多的应该就是SSH三大框架了.作为第一大框架的Spring框架,我们经常使用. 然而在使用过程中,遇到过很多的常见异常,我在这里总结一下,大家共勉. 一.找不到配置文件的异常 org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/herman/ss/controller];

  • Spring加载配置和读取多个Properties文件的讲解

    一个系统中通常会存在如下一些以Properties形式存在的配置文件 1.数据库配置文件demo-db.properties: database.url=jdbc:mysql://localhost/smaple database.driver=com.mysql.jdbc.Driver database.user=root database.password=123 2.消息服务配置文件demo-mq.properties: #congfig of ActiveMQ mq.java.namin

随机推荐