Spring集成jedis的配置与使用简单实例
作者:8blues 时间:2023-07-02 04:11:39
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;
}
来源:https://blog.csdn.net/u014320421/article/details/80576232
标签:spring,redis,jedis
0
投稿
猜你喜欢
android自定义view制作圆形进度条效果
2021-09-14 08:05:54
Java对象的内存布局详细介绍
2021-07-28 05:11:38
C#实现顺序表(线性表)完整实例
2022-06-04 15:42:31
Java多线程回调方法实例解析
2023-11-04 01:40:01
java排查一个线上死循环cpu暴涨的过程分析
2022-07-20 01:27:08
使用Spring Data Redis实现数据缓存的方法
2021-08-02 10:19:25
一款适用于Android平台的俄罗斯方块
2023-02-25 02:38:35
SpringBoot后端接口的实现(看这一篇就够了)
2021-11-05 04:07:41
Android 基于Socket的聊天应用实例(二)
2023-11-02 21:48:32
SpringBoot使用validation做参数校验说明
2022-04-24 09:18:03
SpringBoot+Nacos+Kafka微服务流编排的简单实现
2023-03-21 10:34:53
深入理解Java高级特性——注解
2021-05-23 20:28:54
Java 实战项目之CRM客户管理系统的实现流程
2022-12-01 22:50:54
Java调用wsdl接口的两种方法(axis和wsimport)
2023-06-23 14:41:22
java使用listIterator逆序arraylist示例分享
2022-06-16 05:33:10
Java关键字详解之final static this super的用法
2022-01-19 09:24:39
Unity中C#和Java的相互调用实例代码
2022-02-28 13:40:53
java springmvc实现验证码功能
2022-09-07 12:12:41
JAVA如何调用Shell脚本
2022-11-25 01:13:13
Android开发笔记之:对实践TDD的一些建议说明
2023-11-25 11:45:18