Spring Data Redis 单节点和集群配置和RedisTemplate用法

£神魔★判官ぃ 2022-06-01 05:06 398阅读 0赞

#

Spring Data Redis 单节点和集群配置和RedisTemplate用法

标签(空格分隔): Spring-Data


使用SpringData更加方便我们对关系型数据库和非关系型数据库更好的操作,封装了通用的代码,使得操作更加快捷简单。

一、Spring Data Redis的配置

  1. 引入相关jar包,注意依赖和冲突问题。
  2. maven 引入pom.xml
  3. 1
  4. 2
  5. 3
  6. <dependency>
  7. <groupId>junit</groupId>
  8. <artifactId>junit</artifactId>
  9. <version>4.12</version>
  10. </dependency>
  11. <!--spring-data-redis相关,会自动引入相关jar-->
  12. <dependency>
  13. <groupId>org.springframework.data</groupId>
  14. <artifactId>spring-data-redis</artifactId>
  15. <version>1.7.4.RELEASE</version>
  16. </dependency>
  17. <!--spring测试相关-->
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-test</artifactId>
  21. <version>4.2.8.RELEASE</version>
  22. </dependency>
  23. <!--redis相关-->
  24. <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
  25. <dependency>
  26. <groupId>redis.clients</groupId>
  27. <artifactId>jedis</artifactId>
  28. <version>2.9.0</version>
  29. </dependency>
  30. </dependencies>
  31. 1
  32. 2
  33. 3
  34. 4
  35. 5
  36. 6
  37. 7
  38. 8
  39. 9
  40. 10
  41. 11
  42. 12
  43. 13
  44. 14
  45. 15
  46. 16
  47. 17
  48. 18
  49. 19
  50. 20
  51. 21
  52. 22
  53. 23
  54. 24
  55. 25
  56. 26
  57. 27
  58. 28

手动添加jar包 相关jar下载地址

这里写图片描述

配置文件

  1. 可以使用引入外部文件的方式也可以通过在xml文件中配置的方式
  2. 1
  3. 2

单节点配置方式

properties文件

  1. #JedisPoolConfig的参数
  2. #最大连接数
  3. redis.pool.maxTotal=30
  4. #最大空闲时间
  5. redis.pool.maxIdle=10
  6. #每次最大连接数
  7. redis.pool.numTestsPerEvictionRun=1024
  8. #释放扫描的扫描间隔
  9. redis.pool.timeBetweenEvictionRunsMillis=30000
  10. #连接的最小空闲时间
  11. redis.pool.minEvictableIdleTimeMillis=1800000
  12. #连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
  13. redis.pool.softMinEvictableIdleTimeMillis=10000
  14. #获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
  15. redis.pool.maxWaitMillis=1500
  16. #在获得链接的时候检查有效性,默认false
  17. redis.pool.testOnBorrow=true
  18. #在空闲时检查有效性,默认false
  19. redis.pool.testWhileIdle=true
  20. #连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
  21. redis.pool.blockWhenExhausted=false
  22. #JedisConnectionFactory的参数
  23. #主机地址,默认:localhost
  24. redis.hostName=192.168.200.128
  25. #主机端口,默认:6379
  26. redis.port=6379
  27. #超时时间,默认:2000
  28. redis.timeout=3000
  29. #密码
  30. #redis.password
  31. #是否使用连接池,默认true
  32. redis.usePool=true
  33. #使用数据库的索引,0-15之间的数字,默认:0
  34. redis.dbIndex=0
  35. #是否使用数据类型的转换,默认:true
  36. #redis.convertPipelineAndTxResults
  37. #哨兵配置
  38. #redis.sentinelConfig
  39. #集群配置
  40. #redis.clusterConfig
  41. 1
  42. 2
  43. 3
  44. 4
  45. 5
  46. 6
  47. 7
  48. 8
  49. 9
  50. 10
  51. 11
  52. 12
  53. 13
  54. 14
  55. 15
  56. 16
  57. 17
  58. 18
  59. 19
  60. 20
  61. 21
  62. 22
  63. 23
  64. 24
  65. 25
  66. 26
  67. 27
  68. 28
  69. 29
  70. 30
  71. 31
  72. 32
  73. 33
  74. 34
  75. 35
  76. 36
  77. 37
  78. 38
  79. 39
  80. 40
  81. 41

xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  3. <!--引入配置文件-->
  4. <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  5. <property name="order" value="1"/>
  6. <property name="ignoreUnresolvablePlaceholders" value="true"/>
  7. <property name="locations">
  8. <list>
  9. <value>classpath:redis.properties</value>
  10. </list>
  11. </property>
  12. </bean>
  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. <!--spring data redis -->
  37. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  38. <property name="hostName" value="${redis.hostName}"/>
  39. <property name="port" value="${redis.port}"/>
  40. <property name="timeout" value="${redis.timeout}"/>
  41. <property name="database" value="${redis.dbIndex}"/>
  42. <property name="usePool" value="${redis.usePool}"/>
  43. <!--可以通过构造注入或者Set注入两种方式-->
  44. <property name="poolConfig" ref="jedisPoolConfig"/>
  45. </bean>
  46. <!--redisTemplate-->
  47. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  48. <property name="connectionFactory" ref="jedisConnectionFactory"/>
  49. </bean>
  50. </beans>
  51. 1
  52. 2
  53. 3
  54. 4
  55. 5
  56. 6
  57. 7
  58. 8
  59. 9
  60. 10
  61. 11
  62. 12
  63. 13
  64. 14
  65. 15
  66. 16
  67. 17
  68. 18
  69. 19
  70. 20
  71. 21
  72. 22
  73. 23
  74. 24
  75. 25
  76. 26
  77. 27
  78. 28
  79. 29
  80. 30
  81. 31
  82. 32
  83. 33
  84. 34
  85. 35
  86. 36
  87. 37
  88. 38
  89. 39
  90. 40
  91. 41
  92. 42
  93. 43
  94. 44
  95. 45
  96. 46
  97. 47
  98. 48
  99. 49
  100. 50
  101. 51
  102. 52
  103. 53
  104. 54
  105. 55
  106. 56
  107. 57
  108. 58

集群配置方式
properties文件

  1. #JedisPoolConfig的参数
  2. #最大连接数
  3. redis.pool.maxTotal=30
  4. #最大空闲时间
  5. redis.pool.maxIdle=10
  6. #每次最大连接数
  7. redis.pool.numTestsPerEvictionRun=1024
  8. #释放扫描的扫描间隔
  9. redis.pool.timeBetweenEvictionRunsMillis=30000
  10. #连接的最小空闲时间
  11. redis.pool.minEvictableIdleTimeMillis=1800000
  12. #连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
  13. redis.pool.softMinEvictableIdleTimeMillis=10000
  14. #获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
  15. redis.pool.maxWaitMillis=1500
  16. #在获得链接的时候检查有效性,默认false
  17. redis.pool.testOnBorrow=true
  18. #在空闲时检查有效性,默认false
  19. redis.pool.testWhileIdle=true
  20. #连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
  21. redis.pool.blockWhenExhausted=false
  22. #RedisClusterConfiguration配置
  23. redis.maxRedirects=5
  24. #主机和端口号
  25. redis.host1=192.168.200.128
  26. redis.port1=7000
  27. redis.host2=192.168.200.128
  28. redis.port2=7001
  29. redis.host3=192.168.200.128
  30. redis.port3=7002
  31. redis.host4=192.168.200.128
  32. redis.port4=7003
  33. redis.host5=192.168.200.128
  34. redis.port5=7004
  35. redis.host6=192.168.200.128
  36. redis.port6=7005
  37. 1
  38. 2
  39. 3
  40. 4
  41. 5
  42. 6
  43. 7
  44. 8
  45. 9
  46. 10
  47. 11
  48. 12
  49. 13
  50. 14
  51. 15
  52. 16
  53. 17
  54. 18
  55. 19
  56. 20
  57. 21
  58. 22
  59. 23
  60. 24
  61. 25
  62. 26
  63. 27
  64. 28
  65. 29
  66. 30
  67. 31
  68. 32
  69. 33
  70. 34
  71. 35
  72. 36

xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  3. <!--引入配置文件-->
  4. <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  5. <property name="order" value="1"/>
  6. <property name="ignoreUnresolvablePlaceholders" value="true"/>
  7. <property name="locations">
  8. <list>
  9. <value>classpath:redis-cluster.properties</value>
  10. </list>
  11. </property>
  12. </bean>
  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. <!--配置RedisClusterConfiguration-->
  37. <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
  38. <property name="maxRedirects" value="${redis.maxRedirects}"></property>
  39. <property name="clusterNodes">
  40. <set>
  41. <bean class="org.springframework.data.redis.connection.RedisNode">
  42. <constructor-arg name="host" value="${redis.host1}"/>
  43. <constructor-arg name="port" value="${redis.port1}"/>
  44. </bean>
  45. <bean class="org.springframework.data.redis.connection.RedisNode">
  46. <constructor-arg name="host" value="${redis.host2}"/>
  47. <constructor-arg name="port" value="${redis.port2}"/>
  48. </bean>
  49. <bean class="org.springframework.data.redis.connection.RedisNode">
  50. <constructor-arg name="host" value="${redis.host3}"/>
  51. <constructor-arg name="port" value="${redis.port3}"/>
  52. </bean>
  53. <bean class="org.springframework.data.redis.connection.RedisNode">
  54. <constructor-arg name="host" value="${redis.host4}"/>
  55. <constructor-arg name="port" value="${redis.port4}"/>
  56. </bean>
  57. <bean class="org.springframework.data.redis.connection.RedisNode">
  58. <constructor-arg name="host" value="${redis.host5}"/>
  59. <constructor-arg name="port" value="${redis.port5}"/>
  60. </bean>
  61. <bean class="org.springframework.data.redis.connection.RedisNode">
  62. <constructor-arg name="host" value="${redis.host6}"/>
  63. <constructor-arg name="port" value="${redis.port6}"/>
  64. </bean>
  65. </set>
  66. </property>
  67. </bean>
  68. <!--配置JedisConnectionFactory-->
  69. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  70. <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
  71. <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
  72. </bean>
  73. <!--redisTemplate-->
  74. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  75. <property name="connectionFactory" ref="jedisConnectionFactory"/>
  76. </bean>
  77. </beans>
  78. 1
  79. 2
  80. 3
  81. 4
  82. 5
  83. 6
  84. 7
  85. 8
  86. 9
  87. 10
  88. 11
  89. 12
  90. 13
  91. 14
  92. 15
  93. 16
  94. 17
  95. 18
  96. 19
  97. 20
  98. 21
  99. 22
  100. 23
  101. 24
  102. 25
  103. 26
  104. 27
  105. 28
  106. 29
  107. 30
  108. 31
  109. 32
  110. 33
  111. 34
  112. 35
  113. 36
  114. 37
  115. 38
  116. 39
  117. 40
  118. 41
  119. 42
  120. 43
  121. 44
  122. 45
  123. 46
  124. 47
  125. 48
  126. 49
  127. 50
  128. 51
  129. 52
  130. 53
  131. 54
  132. 55
  133. 56
  134. 57
  135. 58
  136. 59
  137. 60
  138. 61
  139. 62
  140. 63
  141. 64
  142. 65
  143. 66
  144. 67
  145. 68
  146. 69
  147. 70
  148. 71
  149. 72
  150. 73
  151. 74
  152. 75
  153. 76
  154. 77
  155. 78
  156. 79
  157. 80
  158. 81
  159. 82
  160. 83
  161. 84
  162. 85
  163. 86
  164. 87

二、RedisTemplate的用法

  1. Operations

    1. opsForXXXboundXXXOps的区别?
    2. XXXValue的类型,前者获取一个operator,但是没有指定操作的对象(key),可以在一个连接(事务)内操作多个key以及对应的value;后者获取了一个指定的对象(key)的operator,在一个连接(事务)内只能操作这个对应的key
    3. 关于计数APIincrement)中有个小问题需要注意,通过increment计数后,通过get方式获取计数值的时候可能会抛出EOF异常(和本地的jdk以及redis的编译版本有关),可以考虑使用boundValueOps(key).get(0,-1)获得计数值。
  1. 1
  2. 2
  3. 3
  4. 4
  1. ValueOperations和BoundValueOperations

    1. ValueOperations operations = redisTemplate.opsForValue();
    2. String key = "XXX";
    3. BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key);
  1. 1
  2. 2
  3. 3
  4. 类:ValueOperations可以理解为:Map<Object,Object>
  5. 方法:set(key,value)保存
  6. get(key)获取
  7. 类:BoundValueOperations可以理解为,预先设置好了Key,可以对此key进行其他操作,如设置value,修改value,获得value
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. 简单示例:
  14. ValueOperations operations = redisTemplate.opsForValue();
  15. String key = "name";
  16. //存入key-value
  17. operations.set("name","zhangsan");
  18. //根据key取出Value
  19. Object name = operations.get("name");
  20. System.out.println("name:"+name);
  21. //追加
  22. operations.append("name","is man");
  23. //获得并修改
  24. operations.getAndSet("name","zhangsan-1");
  25. Object name1 = operations.get("name");
  26. System.out.println("修改后:"+name1);
  27. String key = "name-1";
  28. BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key);
  29. //和ValueOperations类似
  30. 1
  31. 2
  32. 3
  33. 4
  34. 5
  35. 6
  36. 7
  37. 8
  38. 9
  39. 10
  40. 11
  41. 12
  42. 13
  43. 14
  44. 15
  45. 16
  46. 17
  1. ListOperations 和 BoundListOperations

    1. ListOperations listOperations = redisTemplate.opsForList();
    2. String key = "springData";
    3. BoundListOperations boundListOperations = redisTemplate.boundListOps(key);
  1. 1
  2. 2
  3. 3
  4. 类:ListOperations可以理解为:List<Object>
  5. BoundListOperations可以理解为,预先设置好了Key,可以对此key进行其他操作,如设置value,修改value,获得value
  6. 1
  7. 2
  8. 3
  9. 简单示例:
  10. ListOperations listOperations = redisTemplate.opsForList();
  11. //push键值对
  12. listOperations.leftPush(key,"aaa");
  13. listOperations.leftPush(key,"bbb");
  14. listOperations.index(1,1000);
  15. //pop值
  16. Object leftPop = listOperations.leftPop(key);
  17. System.out.println(leftPop);
  18. listOperations.set(key,100,"测试数据");
  19. //ListOperations可以设置泛型
  20. ListOperations<String,Integer> listOperations1 = redisTemplate.opsForList();
  21. String key = "springData";
  22. BoundListOperations boundListOperations = redisTemplate.boundListOps(key);
  23. //和ListOperations类似
  24. 1
  25. 2
  26. 3
  27. 4
  28. 5
  29. 6
  30. 7
  31. 8
  32. 9
  33. 10
  34. 11
  35. 12
  36. 13
  37. 14
  38. 15
  39. 16
  40. 17
  41. 18
  42. 19
  1. SetOperations 和 BoundSetOperations

    1. SetOperations setOperations = redisTemplate.opsForSet();
    2. String key = "springData";
    3. BoundSetOperations boundSetOperations = redisTemplate.boundSetOps(key);
  1. 1
  2. 2
  3. 3
  4. 类:SetOperations可以理解为:Set<Object>
  5. BoundSetOperations可以理解为,预先设置好了Key,可以对此key进行其他操作,如设置value,修改value,获得value
  6. 1
  7. 2
  8. 3
  9. 简单示例和解释:
  10. /** * Set集合,类似于 * * Map<Object, Set<Object>> map = new HashMap<Object, Set<Object>>(); * Set<Object> set = new HashSet<Object>(); */
  11. @Test
  12. public void test04(){
  13. SetOperations setOperations = redisTemplate.opsForSet();
  14. /** * 添加,类似于 * * Map<Object, Set<Object>> map = new HashMap<Object, Set<Object>>(); * Set<Object> set = new HashSet<Object>(); * map.put(key,set); * Set<Object> set = map.get(key); * set.add(value); */
  15. setOperations.add("springData","redis");
  16. //获取
  17. Set springData = setOperations.members("springData");
  18. //获取两个key的value(Set)交集
  19. setOperations.intersect("springData","SpringData1");
  20. //获取两个key的value(Set)补集
  21. setOperations.difference("springData","SpringData1");
  22. //获取两个key的value(Set)并集
  23. setOperations.union("springData","SpringData1");
  24. String key = "springData";
  25. BoundSetOperations boundSetOperations = redisTemplate.boundSetOps(key);
  26. //和SetOperations操作类似
  27. }
  28. 1
  29. 2
  30. 3
  31. 4
  32. 5
  33. 6
  34. 7
  35. 8
  36. 9
  37. 10
  38. 11
  39. 12
  40. 13
  41. 14
  42. 15
  43. 16
  44. 17
  45. 18
  46. 19
  47. 20
  48. 21
  49. 22
  50. 23
  51. 24
  52. 25
  53. 26
  54. 27
  55. 28
  56. 29
  57. 30
  58. 31
  59. 32
  60. 33
  61. 34
  62. 35
  63. 36
  1. ZSetOperations 和 BoundZSetOperations

    1. ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    2. String key = "springData";
    3. BoundZSetOperations boundZSetOperations = redisTemplate.boundZSetOps(key);
  1. 1
  2. 2
  3. 3
  4. 类:SetOperations可以理解为:Set<Object>
  5. BoundSetOperations可以理解为,预先设置好了Key,可以对此key进行其他操作,如设置value,修改value,获得value
  6. 1
  7. 2
  8. 3
  1. HashOperations 和 BoundHashOperations

    1. HashOperations hashOperations = redisTemplate.opsForHash();
    2. String key = "springData";
    3. BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key);
  1. 1
  2. 2
  3. 3
  4. 说明:APi中的方法很多,大家可以自行测试。源码下载 【[源码下载连接][Link 1]】

发表评论

表情:
评论列表 (有 0 条评论,398人围观)

还没有评论,来说两句吧...

相关阅读