spring redis 单点配置

pom引入jar包

<!-- redis start -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.8.6.RELEASE</version>
    </dependency>
    <!-- redis end -->

spring配置

<!-- 配置文件 -->
    <context:property-placeholder
        location="classpath:redis.properties"
        ignore-unresolvable="true" />

    <!-- redis单点开始 -->
    <!-- string redis template definition -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>

            <!-- redis template definition -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
    </bean>

        <!-- Spring-redis链接池管理工厂 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!-- IP地址 -->
        <property name="hostName" value="${spring.redis.host}" />
        <!-- 端口号 -->
        <property name="port" value="${spring.redis.port}" />
        <!-- 超时时间 默认2000-->
        <property name="timeout" value="${spring.redis.timeout}" />
        <!-- 链接池配置引用 -->
        <property name="poolConfig" ref="jedisPoolConfig" />
        <!-- Redis数据库索引(默认为0) -->
        <property name="database" value="${spring.redis.database}"/>
    </bean>

        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${spring.redis.pool.max-active}" />
        <property name="maxIdle" value="${spring.redis.pool.max-idle}" />
        <property name="minIdle" value="${spring.redis.pool.min-idle}" />
        <property name="maxWaitMillis" value="${spring.redis.pool.max-wait}" />
        <property name="testOnBorrow" value="true" />
    </bean>

配置文件

redis.propertiesredis

##Redis单点ip
spring.redis.host=127.0.0.1
##Redis单点端口
spring.redis.port=6379
## Redis数据库索引(默认为0) 
spring.redis.database=0
## 链接超时时间(毫秒) 
spring.redis.timeout=60000
## 最大重试次数
spring.redis.maxRedirects=3
## 链接池最大链接数(使用负值表示没有限制) 
spring.redis.pool.max-active=300

## 链接池最大阻塞等待时间(使用负值表示没有限制) 
spring.redis.pool.max-wait=-1
## 链接池中的最大空闲链接 
spring.redis.pool.max-idle=100
## 链接池中的最小空闲链接 
spring.redis.pool.min-idle=20

使用方法

@Autowired
    @Resource(name="stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;
相关文章
相关标签/搜索