Spring整合redis方法总结

来源:互联网 发布:王健林八字 知乎 编辑:程序博客网 时间:2024/06/06 02:32

目的、总结spring总结redis的几种方法

一、与spring整合成为template

1、properties文件

# Redisredis.host=192.168.***.***redis.port=6379redis.password=redis.maxIdle=100redis.maxTotal=200redis.maxWaitMillis=1000redis.testOnBorrow=trueredis.timeout=10000juti

具体其中每项的含义不在这里过多的赘述

2、Spring与redis整合的xml配置文件

<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.0.xsd      ">    <!-- jedis连接池配置 -->    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxIdle" value="${redis.maxIdle}"/>        <property name="maxTotal" value="${redis.maxTotal}"/>        <property name="blockWhenExhausted" value="true"/>        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>    </bean>    <!-- jedis连接工程的配置 -->    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <property name="hostName" value="${redis.host}"/>        <property name="port" value="${redis.port}"/>        <property name="password" value="${redis.password}"/>        <property name="poolConfig" ref="jedisPoolConfig"/>        <property name="usePool" value="true"/>    </bean>    <bean name="jackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">        <constructor-arg name="type" value="java.lang.Object"/>    </bean>    <bean name="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>    <!-- redisTemplate配置 -->    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">        <property name="connectionFactory" ref="jedisConnectionFactory"/>        <property name="keySerializer" ref="stringRedisSerializer"/>        <property name="valueSerializer" ref="jackson2JsonRedisSerializer"/>        <property name="hashKeySerializer" ref="stringRedisSerializer"/>        <property name="hashValueSerializer" ref="jackson2JsonRedisSerializer"/>    </bean></beans>

注意:每个bean使用的是那个类去初始化这些配置

二、使用ShardedJedisPool接口配置进行初始操作,这样可以配置选择redis数据库的操作

1、properties操作   

#================================== Redis=====================================# 绑定的主机地址#redis.host=192.168.121.252#指定Redis监听端口,默认端口为6379#redis.port=6379#通过url进行配置redis.url=http://192.168.121.252:6379/0#授权密码(可以不使用)redis.password=Yplsec.com#最大空闲数:空闲链接数大于maxIdle时,将进行回收redis.maxIdle=100#最大连接数:能够同时建立的“最大链接个数”redis.maxTotal=200#最大等待时间:单位msredis.maxWaitMillis=1000#使用连接时,检测连接是否成功redis.testOnBorrow=trueredis.testOnReturn=true#当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能redis.timeout=10000
 2、Spring整合的ShardedJedisPool配置文件

<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.0.xsd      ">    <!-- jedis连接池配置 -->    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxIdle" value="${redis.maxIdle}"/>        <property name="maxTotal" value="${redis.maxTotal}"/>        <property name="blockWhenExhausted" value="true"/>        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>        <property name="testOnReturn" value="${redis.testOnReturn}"/>    </bean>    <!-- jedis连接工程的配置 -->    <bean id="jedisConnectionFactory" class="redis.clients.jedis.ShardedJedisPool">         <constructor-arg index="0" ref="jedisPoolConfig"></constructor-arg>        <constructor-arg index="1">              <list>           <bean class="redis.clients.jedis.JedisShardInfo">             <constructor-arg index="0" value="${redis.url}"> </constructor-arg>             <property name="password" value="${redis.password}"/>             <property name="connectionTimeout" value="${redis.timeout}"/>              <property name="soTimeout" value="${redis.timeout}"/>            </bean>         </list>      </constructor-arg>    </bean>   <!--  <bean name="jackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">        <constructor-arg name="type" value="java.lang.Object"/>    </bean>    <bean name="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> -->    <!-- redisTemplate配置 -->   <!--  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">        <property name="connectionFactory" ref="jedisConnectionFactory"/>        <property name="keySerializer" ref="stringRedisSerializer"/>        <property name="valueSerializer" ref="jackson2JsonRedisSerializer"/>        <property name="hashKeySerializer" ref="stringRedisSerializer"/>        <property name="hashValueSerializer" ref="jackson2JsonRedisSerializer"/>    </bean> --></beans>

总结:以上就是Spring整合redis的两种方法。但是在springboot中配置又不会一样,如有问题请进行留言,要是觉得可以请支持一下小编






原创粉丝点击