問題來由
一個老系統使用頻率很低,但是一旦用,就是很多人一起用。每次這個時候,服務都會掛掉。
原因是使用mysql數據庫做復雜計算。沒有使用緩存。
着手解決
框架版本
struts 2.0
spring 3.2
集成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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.0.xsd">
<!-- 加載配置文件 -->
<!-- <context:property-placeholder location="classpath:redis.properties"
/> -->
<!-- redis連接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空閑數 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--連接池的最大數據庫連接數 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<!--最大建立連接等待時間 -->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!--逐出連接的最小空閑時間 默認1800000毫秒(30分鍾) -->
<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
<!--每次逐出檢查時 逐出的最大數目 如果為負數就是 : 1/abs(n), 默認3 -->
<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
<!--逐出掃描的時間間隔(毫秒) 如果為負數,則不運行逐出線程, 默認-1 -->
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
<!--是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
<!--在空閑時檢查有效性, 默認false -->
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
</bean>
<!-- redis集群配置 哨兵模式 -->
<bean id="sentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<!-- 這個值要和Sentinel中指定的master的值一致,不然啟動時找不到Sentinel會報錯的 -->
<property name="name" value="mymaster"></property>
<!-- 配置注master節點 <constructor-arg name="host" value="${redis.hostName}"/>
<constructor-arg name="port" value="${redis.port}"/> -->
</bean>
</property>
<!-- 記住了,這里是指定Sentinel的IP和端口,不是Master和Slave的 -->
<property name="sentinels">
<set>
<!-- 我這里就配置一個哨兵 -->
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port3}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:password="${redis.password}">
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<!--redis操作模版,使用該對象可以操作redis -->
<bean id="redisTemplate" 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.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="false"></property>
</bean>
<!-- 配置RedisCacheManager -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate"></constructor-arg>
</bean>
<!--自定義redis工具類,在需要緩存的地方注入此類 -->
<bean id="redisUtil" class="com.redoor.comm.utils.RedisUtil">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
</beans>
代碼優化
添加緩存。
重新方法。減少不要的數據庫查詢。
異步入庫。
並發測試
遇到的問題
高並發測試下,總是報“unexpected end of stream”
找了很多資料,發現高並發下會產生大量的time_wait的socket連接。
最后查看redisTemplater源碼,發現是redis配置了enableTransactionSupport開啟,這種情況下,redis不會歸還連接池的連接。如果不需要使用redis事務,關閉即可。
可參考:
SpringDataRedis事務 專題
Sping Data Redis 使用事務時,不關閉連接的問題
過程中經歷
開始根據報錯,以為是參數配置的問題,做了一下幾件事 。最后終於找到問題。