springmvc integrated Redis Cluster Cluster

springmvc integrated Redis Cluster Cluster

   点关注不迷路,欢迎再访!		

cluster relative to the sentry mode is decentralized, which each node stores information of other cluster, each node can be used as the center of the cluster, strong fault tolerance, higher availability and online capacity expansion.

The introduction of dependence

<--2.9.0 以下版本不支持cluster密码认证-->
<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.1.RELEASE</version>
</dependency>

Configuration redis.properties

###############################redis数据库的相关配置##################################
##访问密码
redis.auth = xxxxxx
##控制一个pool最多可以有多少个状态为Idle()的jedis实例默认值为8
redis.maxIdle = 200
##可用的最大连接实例数 默认为8
redis.maxActive = 1024
##等待可用连接的最大时间单位为毫秒  默认为-1表示永不超时,一旦超过等待时间则直接抛出
redis.maxWait = 10000
redis.timeOut = 10000
##设置为true则会在borrow一个jedis实例时,提前做validate操作
redis.testOnBorrow = true
##连接最小空闲时间(毫秒)
redis.minEvictableIdleTimeMillis=1800000
##释放连接的扫描间隔(毫秒)
redis.timeBetweenEvictionRunsMillis=30000
##每次释放连接的最大数目
redis.numTestsPerEvictionRun=1024
##最大连接数
redis.maxTotal=30
##在空闲时检查有效性, 默认false 
redis.testWhileIdle=true

#集群节点
cluster.host1=ip
cluster.port1=7001
cluster.port2=7002
cluster.port3=7003
cluster.port4=7004
cluster.port5=7005
cluster.port6=7006

Redis.xml configuration file

If redis clusters set a password, the password must be added in the configuration file in redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.2.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
			http://www.springframework.org/schema/mvc
			http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
			
	<!-- 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.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow }" />
		<property name="testWhileIdle" value="${redis.testWhileIdle}" />
		<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
	</bean>	
	
    <!-- redis集群 -->
	<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
		<property name="maxRedirects" value="6"></property>
 		<property name="clusterNodes">
			<set>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${cluster.host1}" />
					<constructor-arg name="port" value="${cluster.port1}" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${cluster.host1}" />
					<constructor-arg name="port" value="${cluster.port2}" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode ">
					<constructor-arg name="host" value="${cluster.host1}" />
					<constructor-arg name="port" value="${cluster.port3}" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${cluster.host1}" />
					<constructor-arg name="port" value="${cluster.port4}" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${cluster.host1}" />
					<constructor-arg name="port" value="${cluster.port5}" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${cluster.host1}" />
					<constructor-arg name="port" value="${cluster.port6}" />
				</bean>
			</set>
		</property>
	</bean>
	
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
		<constructor-arg name="clusterConfig" ref="redisClusterConfiguration" />
		<property name="password" value="${redis.auth}" />
	</bean>
	
	<!-- StringRedisSerializer模板-->
	<!-- <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
		<property name="keySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
	</bean> -->
	<!-- RedisTemplate模板 -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
	</bean>
	
</beans>

Stepped pit link

1.spring-data-redis 1.8.0 version does not support the following password
to start the project will be reported Jedis does not support password protected Redis Cluster configurations!

Cause: When using the spring-data-redis redis clusters because the cluster operation redis set a password.
Solution: Upgrade spring-data-redis version can be solved

2.spring-data-redis2.0 versions of the above can not be arranged directly at the password when the password value as the old version, the need to inject a RedisPassword bean constructor RedisPassword disposed in the password, the environment need spring5

<!--spring-data-redis2.0以上的配置-->
<bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
        <constructor-arg name="thePassword" value="testpw"></constructor-arg>
</bean>

<bean id="redisStandaloneConfiguration"
class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
        <property name="password" ref="redisPassword" />
</bean>    

Testing and certification

@Autowired
private RedisTemplate redisTemplate;

 public void test(){       
      redisTemplate.opsForValue().set("hello", "你好");
      redisTemplate.opsForValue().get("hello");
 }
Published 101 original articles · won praise 33 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_39443053/article/details/103652498