Jedis是Redis的Java客户端,Spring将Jedis连接池作为一个Bean来配置。如果在Spring Data的官网上可以发现,Spring Data Redis已经将Jedis集成进去了。

Jedis连接池分为两种:

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

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

下面是介绍详细的集成方式:

POM:

    <!-- 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为:

#最大分配的对象数
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;
}

参考:

http://www.cnblogs.com/red-code/p/6657517.html(以上内容转自此篇文章)

http://blog.csdn.net/javaloveiphone/article/details/52355180

http://blog.csdn.net/w410589502/article/details/54341040

http://blog.csdn.net/tomli2017/article/details/69929371

http://www.cnblogs.com/hz-cww/p/6030504.html

http://blog.csdn.net/wang_keng/article/details/51753637

最新文章

  1. org.springframework.beans.MutablePropertyValues.add
  2. 复利计算- 结对2.0--复利计算WEB升级版
  3. iOS第三方推送-极光推送
  4. 【不怕坑】之 Node.js加密 C#解密
  5. WAMP环境的安装与测试
  6. java后台正则验证
  7. java疯狂演义----简单java IDE工具
  8. 开源通讯组件ec
  9. 四维dp 或者 剪枝 + dfs Codeforces Beta Round #6 (Div. 2 Only) D
  10. tensorflow例子-【老鱼学tensorflow】
  11. python -- 函数进阶
  12. SQLserver 使用网络驱动器恢复数据库
  13. Windows Server 2008服务器上测试几个站点,改完host居然没有生效
  14. Linux 下crontab 详解转
  15. Ejb3 + Jboss8 出现Session id hasn&#39;t been set for stateful component
  16. Linux下编辑利器vim,vimrc,viminfo的高级用法
  17. Liunx 下载文件夹下所有文件
  18. WCF基础:绑定(二)
  19. dojo学习教程
  20. Redis面试考点

热门文章

  1. 网盘资源搜索的一些知识 C#
  2. Android开发中使用startActivityForResult()方法从Activity A跳转Activity B出现B退出时A也同时退出的解决办法
  3. Android开发中使用代码删除数据库
  4. Linux下Eclipse连接小米手机真机调试
  5. CREATE GROUP - 定义一个新的用户组
  6. Drop和Truncate与Delete的区别
  7. Timer时钟(之一)
  8. 通过反编译小程序来学习前端:wxappUnpacker
  9. iphone X 的适配
  10. CSU1018: Avatar