一. 我已经在linux端做好了master以及两个slave的redis,sentinel配置
二. 但是关于spring集成方面(即redis.xml的配置),我用了网上两种方法都没有成功。
1.参考于http://sumory.com/2014/08/15/spring-redis-sentinel/
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"></property>
<property name="hostName" value="192.168.86.5" />
<property name="port" value="6371" />
<property name="password" value="" />
<property name="timeout" value="100000" />
<property name="database" value="0"></property>
<constructor-arg index="0" ref="jedisPoolConfig" />
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="300" />
<property name="maxIdle" value="100" />
<property name="maxWaitMillis" value="1000" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="true" />
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg index="0" value="mymaster" />
<constructor-arg index="1">
<set>
<value>192.168.86.5:26370</value>
<value>192.168.86.5:26372</value>
</set>
</constructor-arg>
<constructor-arg index="2" ref="jedisPoolConfig" />
</bean>
<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>
</bean>
我想了想,应该是我的以上配置没有写正确,配置了jedispool但是没有调用。但是我的确不知道该怎么改,求懂sentinel的大神帮我看一下。
2.参考于http://blog.csdn.net/peterwanghao/article/details/44980085
<bean id="redisSentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster"></property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.86.5"></constructor-arg>
<constructor-arg name="port" value="26370"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.86.5" />
<constructor-arg name="port" value="26371" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.86.5" />
<constructor-arg name="port" value="26372" />
</bean>
</set>
</property>
</bean>
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisSentinelConfiguration" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jeidsConnectionFactory" />
用这个方法就更离奇了,我新建了一个项目用以测试这个配置时是可以使用的,整个redis的sentinel功能一切正常。但是我把这个代码移植到要做的项目运行中,始终都会报错( org.springframework.beans.NotWritablePropertyException: Invalid property 'sentinels' of bean class [org.springframework.data.redis.connection.RedisSentinelConfiguration]: Bean property 'sentinels' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?)
按理说sentinel不可写这个错是因为RedisSentinelConfiguration中没有sentinel的set、get方法,但确实是有的,不然测试的时候也不会成功。实在是不知道怎么解决了,查遍了百度,求大神帮我看看
今天终于解决问题了,终于知道第二种方法为什么会出错。
今天我把公司项目里其余的依赖全删掉,一个一个排查,终于找到是spring-web,spring-webmvc这两个依赖导致的问题,之前的版本是3.2.2,然后我把改成4.0.0之后就可以使用了。我想是因为版本低导致有冲突。
太感谢了,我也卡在这2天了,找不出原因,换了spring的版本确实可以了。