Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)

来源:互联网 发布:c语言整数四则运算程序 编辑:程序博客网 时间:2024/06/05 05:49

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=50redis.pool.minIdle=10redis.pool.maxWaitMillis=20000#当池内没有返回对象时,最大等待时间redis.pool.maxWait=300#格式:redis://:[密码]@[服务器地址]:[端口]/[db index]redis.uri = redis://:12345@127.0.0.1:6379/0redis.host = 127.0.0.1redis.port = 6379redis.timeout=30000redis.password = 12345redis.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 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 初川南 中文视频 网红主播一边搞一边打电话 初中生第20页 偷怕自怕三页 迪巴士网站 亚拍1区 36d大奶闷 排列5预测_排列五十位杀号最准确 51呦 双色球贴吧_3d图谜字谜 26UUU. 田园贵妻纪青青免费看 终结者6高清 一进一出国语对白 草草剧院第20页 j色酷电影院 苏晚晴冷夜冥全文阅读 k频道国产系统 分享 国产 德国老太德国老太太granny 经典家庭伦txt丝母韵欲彩小说 诸天末日再线 灵媒by风书呆攻是哪个 从化枫叶林 丰县古墓 七界传说张傲雪乳喷 内原美智子视频6o岁女 打电话全集国产 林岚31岁全文免费阅读 徐州有T3出行吗 図监大全第一页 化龙帝仙 台湾 电子书 www.6800.com17 母爱1-15 学生第 34 页 无限资源无限源国产好片2019_任你躁国语版视频 学姐的奶水 邳州各镇排名 浮力影院大全 遂昌千佛山景区导览图 小野亚麻里在线中文视频