学习目标:
1、Spring 配置文件整合Redis的Cluster模式
2、Spring Boot整合Redis的Cluster模式
学习过程:
redis的Cluster的链接方式非常简单,比哨兵模式还要更简单一下。直接看配置文件就可以了,这里就不罗嗦了。
一、Spring 使用配置文件整合 Redis Cluster
<?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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:redis="http://www.springframework.org/schema/redis"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd">
<description>Redis配置</description>
<!-- 加载配置属性文件 -->
<!-- <context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" /> -->
<!-- 连接池配置 -->
<!--配置 jedis pool -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<!-- 最大空闲时间 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!-- 每次最大连接数 -->
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" />
<!-- 释放扫描的扫描间隔 -->
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
<!-- 连接的最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
<!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />
<!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<!-- 在获得链接的时候检查有效性,默认false -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<!-- 在空闲时检查有效性,默认false -->
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}" />
<!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true -->
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}" />
</bean>
<bean id="redisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<redis:listener-container connection-factory="connectionFactory">
<redis:listener ref="shiroSessionService" topic="shiro.session.uncache"
serializer="redisSerializer" />
</redis:listener-container>
<!-- 集群配置 -->
<!-- Redis集群配置 这里使用的是spring-data-redis 包中内容 -->
<bean id="redisClusterConfig"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.cluster.max-redirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.cluster.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.cluster.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.cluster.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.cluster.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.cluster.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.cluster.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.cluster.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.cluster.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.cluster.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.cluster.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.cluster.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.cluster.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<!-- Redis Cluster连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="redisClusterConfig" />
<property name="timeout" value="${redis.cluster.timeout}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- 集群Resis使用模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="valueSerializer" ref="redisSerializer" />
</bean>
</beans>
二、Spring Boot整合 Redis cluster
事实上使用Cluster,对客户端而言更加简单。因为分片和主从切换都是服务端完成的了。
1、修改application.properties
spring.redis.cluster.nodes=192.168.137.101:6379,192.168.137.101:7379,192.168.137.102:6379,192.168.137.102:7379,192.168.137.103:6379,192.168.137.103:7379
2、定义配置类
@Configuration
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterConfig {
List<String> nodes;
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
@Bean
public JedisCluster redisCluster() {
Set<HostAndPort> nodesHostAndPort = new HashSet<HostAndPort>();
for (String node : nodes) {
String[] parts = StringUtils.split(node, ":");
nodesHostAndPort.add(new HostAndPort(parts[0], Integer.valueOf(parts[1])));
}
return new JedisCluster(nodesHostAndPort);
}
}
3、测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringfirstApplicationTests {
@Autowired
private JedisCluster jedisCluster;
@Test
public void testResis() {
jedisCluster.set("name", "clusterSpringBoot");
String name = (String) jedisCluster.get("name");
System.out.println("name:" + name);
}
}