redis 做session共享遇到连接数不够用的问题。

本文详细解析了在高并发场景下,配置了较大连接数后仍出现Redis连接不足的问题。发现原因是Spring配置中开启了Redis事务,导致连接资源被过度占用。通过关闭事务支持,有效解决了连接池异常,保障了系统的稳定运行。

现象:测试时候 配置了100的最大连接数一会儿redis就获取不到连接了, 配置了500 问题依旧。

原因: redis 的spring配置文件 设置成了开启事务 。

解决方式:不开启事务

配置如下:

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
                     http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-4.0.xsd ">


<!-- redis数据源 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!-- 最大等待时间 -->
    <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
    <!-- 返回连接时,检测连接是否成功 -->
    <property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>

<!-- Spring-redis连接池管理工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <!-- IP地址 -->
    <property name="hostName" value="${redis.host}" />
    <!-- 端口号 -->
    <property name="port" value="${redis.port}" />
    <property name="password" value="${redis.password}" />
    <!-- 超时时间 默认2000-->
    <property name="timeout" value="${redis.timeout}" />
    <property name="database" value="${redis.database}"/>
    <!-- 连接池配置引用 -->
    <property name="poolConfig" ref="poolConfig" />
    <!-- usePool:是否使用连接池 -->
    <property name="usePool" value="true"/>
</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.GenericJackson2JsonRedisSerializer" />
    </property>
    <property name="hashKeySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property>
    <property name="hashValueSerializer">
        <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
    </property>
    <!--开启事务  -->
    <!--<property name="enableTransactionSupport" value="true"></property>-->
</bean>

    <!--shiro 序列化的redis 模板-->
    <bean id="redisObjectTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->
        <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>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
        <constructor-arg ref="poolConfig"/>
        <constructor-arg value="${redis.host}"/>
        <constructor-arg type="int" value="${redis.port}"/>
        <constructor-arg type="int" value="${redis.timeout}"/>
        <constructor-arg type="java.lang.String" value="${redis.password}"/>
        <constructor-arg type="int" value="${redis.database}"/>
    </bean>
    <bean id="redisClient" class="com.cao.web.login.realm.RedisClient">
        <constructor-arg name="jedisPool" ref="jedisPool"/>
        <property name="expire" value="1800"/>
    </bean>
</beans>
 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值