当前位置:   article > 正文

26、RedisCluster的java客户端_redis cluster java客户端

redis cluster java客户端

学习目标:

1、Spring 配置文件整合Redis的Cluster模式

2、Spring Boot整合Redis的Cluster模式

学习过程:

redis的Cluster的链接方式非常简单,比哨兵模式还要更简单一下。直接看配置文件就可以了,这里就不罗嗦了。

一、Spring 使用配置文件整合 Redis Cluster

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:redis="http://www.springframework.org/schema/redis"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  8. 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">
  9. <description>Redis配置</description>
  10. <!-- 加载配置属性文件 -->
  11. <!--  <context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" /> -->
  12. <!-- 连接池配置 -->
  13. <!--配置 jedis pool -->
  14. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  15. <!-- 最大连接数 -->
  16. <property name="maxTotal" value="${redis.pool.maxTotal}" />
  17. <!-- 最大空闲时间 -->
  18. <property name="maxIdle" value="${redis.pool.maxIdle}" />
  19. <!-- 每次最大连接数 -->
  20. <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" />
  21. <!-- 释放扫描的扫描间隔 -->
  22. <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
  23. <!-- 连接的最小空闲时间 -->
  24. <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
  25. <!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
  26. <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />
  27. <!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
  28. <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
  29. <!-- 在获得链接的时候检查有效性,默认false -->
  30. <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
  31. <!-- 在空闲时检查有效性,默认false -->
  32. <property name="testWhileIdle" value="${redis.pool.testWhileIdle}" />
  33. <!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true -->
  34. <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}" />
  35. </bean>
  36. <bean id="redisSerializer"
  37. class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
  38. <redis:listener-container connection-factory="connectionFactory">
  39. <redis:listener ref="shiroSessionService" topic="shiro.session.uncache"
  40. serializer="redisSerializer" />
  41. </redis:listener-container>
  42. <!-- 集群配置 -->
  43. <!-- Redis集群配置 这里使用的是spring-data-redis 包中内容 -->
  44. <bean id="redisClusterConfig"
  45. class="org.springframework.data.redis.connection.RedisClusterConfiguration">
  46. <property name="maxRedirects" value="${redis.cluster.max-redirects}"></property>
  47. <property name="clusterNodes">
  48. <set>
  49. <bean class="org.springframework.data.redis.connection.RedisNode">
  50. <constructor-arg name="host" value="${redis.cluster.host1}"></constructor-arg>
  51. <constructor-arg name="port" value="${redis.cluster.port1}"></constructor-arg>
  52. </bean>
  53. <bean class="org.springframework.data.redis.connection.RedisNode">
  54. <constructor-arg name="host" value="${redis.cluster.host2}"></constructor-arg>
  55. <constructor-arg name="port" value="${redis.cluster.port2}"></constructor-arg>
  56. </bean>
  57. <bean class="org.springframework.data.redis.connection.RedisNode">
  58. <constructor-arg name="host" value="${redis.cluster.host3}"></constructor-arg>
  59. <constructor-arg name="port" value="${redis.cluster.port3}"></constructor-arg>
  60. </bean>
  61. <bean class="org.springframework.data.redis.connection.RedisNode">
  62. <constructor-arg name="host" value="${redis.cluster.host4}"></constructor-arg>
  63. <constructor-arg name="port" value="${redis.cluster.port4}"></constructor-arg>
  64. </bean>
  65. <bean class="org.springframework.data.redis.connection.RedisNode">
  66. <constructor-arg name="host" value="${redis.cluster.host5}"></constructor-arg>
  67. <constructor-arg name="port" value="${redis.cluster.port5}"></constructor-arg>
  68. </bean>
  69. <bean class="org.springframework.data.redis.connection.RedisNode">
  70. <constructor-arg name="host" value="${redis.cluster.host6}"></constructor-arg>
  71. <constructor-arg name="port" value="${redis.cluster.port6}"></constructor-arg>
  72. </bean>
  73. </set>
  74. </property>
  75. </bean>
  76. <!-- Redis Cluster连接工厂 -->
  77. <bean id="connectionFactory"
  78. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  79. <constructor-arg name="clusterConfig" ref="redisClusterConfig" />
  80. <property name="timeout" value="${redis.cluster.timeout}" />
  81. <property name="poolConfig" ref="jedisPoolConfig" />
  82. </bean>
  83. <!-- 集群Resis使用模板 -->
  84. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  85. <property name="connectionFactory" ref="connectionFactory" />
  86. <property name="valueSerializer" ref="redisSerializer" />
  87. </bean>
  88. </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、定义配置类

  1. @Configuration
  2. @ConfigurationProperties(prefix = "spring.redis.cluster")
  3. public class RedisClusterConfig {
  4.     List<String> nodes;
  5.     public List<StringgetNodes() {
  6.         return nodes;
  7.     }
  8.     public void setNodes(List<String> nodes) {
  9.         this.nodes = nodes;
  10.     }
  11.     @Bean
  12.     public JedisCluster redisCluster() {
  13.         Set<HostAndPort> nodesHostAndPort = new HashSet<HostAndPort>();
  14.         for (String node : nodes) {
  15.             String[] parts = StringUtils.split(node, ":");
  16.             nodesHostAndPort.add(new HostAndPort(parts[0], Integer.valueOf(parts[1])));
  17.         }
  18.         return new JedisCluster(nodesHostAndPort);
  19.     }
  20. }

3、测试

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class SpringfirstApplicationTests {
  4.     @Autowired
  5.     private JedisCluster jedisCluster;
  6.     @Test
  7.     public void testResis() {
  8.         jedisCluster.set("name""clusterSpringBoot");
  9.         String name = (String) jedisCluster.get("name");
  10.         System.out.println("name:" + name);
  11.     }
  12. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号