Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
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
Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)的更多相关文章
- springboot redis(单机/集群)
前言 前面redis弄了那么多, 就是为了在项目中使用. 那这里, 就分别来看一下, 单机版和集群版在springboot中的使用吧. 在里面, 我会同时贴出Jedis版, 作为比较. 单机版 1. ...
- Redis cluster集群模式的原理
redis cluster redis cluster是Redis的分布式解决方案,在3.0版本推出后有效地解决了redis分布式方面的需求 自动将数据进行分片,每个master上放一部分数据 提供内 ...
- Redis进阶实践之十 Redis哨兵集群模式
一.引言 上一篇文章我们详细的讲解了Redis的主从集群模式,其实这个集群模式配置很简单,只需要在Slave的节点上进行配置,Master主节点的配置不需要做任何更改,但是有一 ...
- redis cluster(集群)模式的创建方式
redis常用的架构有三种,单例.哨兵.集群,其他的都说过了,这里只简单介绍集群搭建. 单例最简单没什么好说的. 哨兵之前说过,该模式下有哨兵节点监视master和slave,若master宕机可自动 ...
- 单台服务器-利用docker搭建Redis哨兵集群模式
前言:只有一台华为云服务器,所以打算创建三个容器来模拟三个服务器了. 一:拉取redis镜像 二:拉取redis.conf文件 放在自定义的目录下:wget -c http://download.re ...
- springboot2.x版本整合redis(单机/集群)(使用lettuce)
在springboot1.x系列中,其中使用的是jedis,但是到了springboot2.x其中使用的是Lettuce. 此处springboot2.x,所以使用的是Lettuce.关于jedis跟 ...
- 面向接口编程实现不改代码实现Redis单机/集群之间的切换
开发中一般使用Redis单机,线上使用Redis集群,因此需要实现单机和集群之间的灵活切换 pom配置: <!-- Redis客户端 --> <dependency> < ...
- Redis的集群模式
集群 即使使用哨兵,此时的Redis集群的每个数据库依然存有集群中的所有数据,从而导致集群的总数据存储量受限于可用存储内存最小的数据库节点,形成木桶效应.由于Redis中的所有数据都是基于内存存储,这 ...
- 170103、Redis官方集群方案 Redis Cluster
前面我们谈了Redis Sharding多服务器集群技术,Redis Sharding是客户端Sharding技术,对于服务端来说,各个Redis服务器彼此是相互独立的,这对于服务端根据需要灵活部署R ...
随机推荐
- .NET 出现参数化查询 需要参数但未提供该参数的错误
1.问题的来源 在.NET或者C#中,我们一般执行sql语句的话,推荐使用参数化查询,这样可以避免sql注入的攻击,但是,我在使用参数化查询的时候 出现了以下的错误,详细如下图: 图一这是写sql语句 ...
- Python学习 Day 9 property 多重继承 Mixin
在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 为了限制score的范围,可以通过一 ...
- (1) Jenkins + Subversion + Maven + TestNG - 软件
软件需求 1 Jenkins http://jenkins-ci.org/ 2 Visual SVN Server https://www.visualsvn.com/server/ 3 Tortoi ...
- laravel模块 目录设计
- java_线程的同步机制
1.同步代码块: 使用synchronized包住可能会出现安全问题的代码 import static java.lang.Thread.sleep; class Test01{ public sta ...
- 禁止浏览器static files缓存篇
由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. Meta法 目前在chrome调试还没有遇到问题,好用!此方法假设浏览器是个好人!很听我们的话! <meta http ...
- 计算几何——认识基本object:点、线、面 。
认识基本object:点.线.面 一.点 点用P(x, y)来表示:如: typedef pair<double, double> _pair; _pair point[MAXN]; 二 ...
- Oracle 数据库实例启动关闭过程
Oracle数据库实例的启动,严格来说应该是实例的启动,数据库仅仅是在实例启动后进行装载.Oracle数据启动的过程被划分为 几个不同的步骤,在不同的启动过程中,我们可以对其实现不同的操作,系统修复等 ...
- kubernetes 知识点及常用命令
一.附上一个Deployment文件 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selec ...
- 11.Spring通过工厂方法配置Bean
通过工厂方法配置Bean暴扣静态工厂方法和实例工厂方法. 1.静态工厂方法 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不去关心创建 ...