04.spring-data-redis與Jedis整合使用



1.spring-data-redis與Jedis簡單整合

spring-data-redis與Jedis簡單整合,Redis沒有任何集群只是單節點工作,使用連接池
1.創建spring-context-jedis.xml配置文件
   
   
   
           
  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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/mvc
  8. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd"
  13. default-lazy-init="false">
  14. <!-- 連接池配置. -->
  15. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  16. <!-- 連接池中最大連接數。高版本:maxTotal,低版本:maxActive -->
  17. <property name="maxTotal" value="8" />
  18. <!-- 連接池中最大空閑的連接數. -->
  19. <property name="maxIdle" value="4" />
  20. <!-- 連接池中最少空閑的連接數. -->
  21. <property name="minIdle" value="1" />
  22. <!-- 當連接池資源耗盡時,調用者最大阻塞的時間,超時將跑出異常。單位,毫秒數;默認為-1.表示永不超時。高版本:maxWaitMillis,低版本:maxWait -->
  23. <property name="maxWaitMillis" value="5000" />
  24. <!-- 連接空閑的最小時間,達到此值后空閑連接將可能會被移除。負值(-1)表示不移除. -->
  25. <property name="minEvictableIdleTimeMillis" value="300000" />
  26. <!-- 對於“空閑鏈接”檢測線程而言,每次檢測的鏈接資源的個數。默認為3 -->
  27. <property name="numTestsPerEvictionRun" value="3" />
  28. <!-- “空閑鏈接”檢測線程,檢測的周期,毫秒數。如果為負值,表示不運行“檢測線程”。默認為-1. -->
  29. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  30. <!-- testOnBorrow:向調用者輸出“鏈接”資源時,是否檢測是有有效,如果無效則從連接池中移除,並嘗試獲取繼續獲取。默認為false。建議保持默認值. -->
  31. <!-- testOnReturn:向連接池“歸還”鏈接時,是否檢測“鏈接”對象的有效性。默認為false。建議保持默認值.-->
  32. <!-- testWhileIdle:向調用者輸出“鏈接”對象時,是否檢測它的空閑超時;默認為false。如果“鏈接”空閑超時,將會被移除。建議保持默認值. -->
  33. <!-- whenExhaustedAction:當“連接池”中active數量達到閥值時,即“鏈接”資源耗盡時,連接池需要采取的手段, 默認為1(0:拋出異常。1:阻塞,直到有可用鏈接資源。2:強制創建新的鏈接資源) -->
  34. </bean>
  35. <!-- Spring提供的Redis連接工廠 -->
  36. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
  37. <!-- 連接池配置. -->
  38. <property name="poolConfig" ref="jedisPoolConfig" />
  39. <!-- Redis服務主機. -->
  40. <property name="hostName" value="192.168.110.101" />
  41. <!-- Redis服務端口號. -->
  42. <property name="port" value="6379" />
  43. <!-- Redis服務連接密碼. -->
  44. <!-- <property name="password" value="${redis.password}" /> -->
  45. <!-- 連超時設置. -->
  46. <property name="timeout" value="15000" />
  47. <!-- 是否使用連接池. -->
  48. <property name="usePool" value="true" />
  49. </bean>
  50. <!-- Spring提供的訪問Redis類. -->
  51. <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  52. <property name="connectionFactory" ref="jedisConnectionFactory" />
  53. <property name="keySerializer">
  54. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  55. </property>
  56. <property name="valueSerializer">
  57. <!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
  58. <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
  59. </property>
  60. </bean>
  61. </beans>
2.使用Spring提供的RedisTemplate類
   
   
   
           
  1. public static void main(String[] args)
  2. {
  3. ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context-jedis.xml");
  4. // 獲取Spring提供的RedisTemplate類此類封裝了Jedis,簡化操作
  5. RedisTemplate<String, List<String>> redisTemplate = applicationContext.getBean("jedisTemplate", RedisTemplate.class);
  6. // Spring 提供的各種Redis結構的key-value操作類
  7. ValueOperations<String, List<String>> value = redisTemplate.opsForValue();
  8. HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
  9. ListOperations<String, List<String>> list = redisTemplate.opsForList();
  10. HyperLogLogOperations<String, List<String>> hyperLogLog = redisTemplate.opsForHyperLogLog();
  11. SetOperations<String, List<String>> set = redisTemplate.opsForSet();
  12. ZSetOperations<String, List<String>> zSet = redisTemplate.opsForZSet();
  13. List<String> listValue = new ArrayList<String>();
  14. listValue.add("001");
  15. listValue.add("002");
  16. value.set("list", listValue);
  17. System.out.println(value.get("list"));
  18. // 關閉Spring容器釋放資源
  19. applicationContext.close();
  20. }
3.關於RedisTemplate類源碼學習
RedisTemplate類的屬性
   
   
   
           
  1. private boolean enableTransactionSupport = false;
  2. private boolean exposeConnection = false;
  3. private boolean initialized = false;
  4. private boolean enableDefaultSerializer = true;
  5. // 默認的序列化實現
  6. private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();
  7. // 各種操作的序列化方式定義
  8. private RedisSerializer keySerializer = null;
  9. private RedisSerializer valueSerializer = null;
  10. private RedisSerializer hashKeySerializer = null;
  11. private RedisSerializer hashValueSerializer = null;
  12. private RedisSerializer<String> stringSerializer = new StringRedisSerializer();
  13. private ScriptExecutor<K> scriptExecutor;
  14. // Spring 提供的各種Redis結構的key-value操作類
  15. // cache singleton objects (where possible)
  16. private ValueOperations<K, V> valueOps;
  17. private ListOperations<K, V> listOps;
  18. private SetOperations<K, V> setOps;
  19. private ZSetOperations<K, V> zSetOps;
  20. private HyperLogLogOperations<K, V> hllOps;
在一個應用中RedisTemplate類對象可以是單例的,因為其屬性“ valueOpslistOpssetOpszSetOpshllOps ”的各種操作也是線程安全的,源碼如下:
獲取其 valueOps listOps setOps zSetOps hllOps 屬性:
   
   
   
           
  1. public ValueOperations<K, V> opsForValue()
  2. {
  3. if (valueOps == null)
  4. {
  5. valueOps = new DefaultValueOperations<K, V>(this);
  6. }
  7. return valueOps;
  8. }
  9. public ListOperations<K, V> opsForList()
  10. {
  11. if (listOps == null)
  12. {
  13. listOps = new DefaultListOperations<K, V>(this);
  14. }
  15. return listOps;
  16. }
  17. // 省略部分......
其屬性“ valueOps listOps setOps zSetOps hllOps ”的各種操作也是線程安全的,例如 valueOps 屬性對象默認實現類 DefaultValueOperations < K , V > 源碼:
   
   
   
           
  1. public void set(K key, V value)
  2. {
  3. final byte[] rawValue = rawValue(value);
  4. execute(new ValueDeserializingRedisCallback(key)
  5. {
  6. protected byte[] inRedis(byte[] rawKey, RedisConnection connection)
  7. {
  8. connection.set(rawKey, rawValue);
  9. return null;
  10. }
  11. }, true);
  12. }
  13. // 省略其他操作......
再看看 execute 的實現如下:
   
   
   
           
  1. //org.springframework.data.redis.core.AbstractOperations<K, V>
  2. <T> T execute(RedisCallback<T> callback, boolean b) {
  3. return template.execute(callback, b);
  4. }
  5. // template.execute實現如下:
  6. // org.springframework.data.redis.core.RedisTemplate<K, V>
  7. public <T> T execute(RedisCallback<T> action, boolean exposeConnection) {
  8. return execute(action, exposeConnection, false);
  9. }
  10. // execute實現如下:
  11. // org.springframework.data.redis.core.RedisTemplate<K, V> --- 最終實現
  12. public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {
  13. Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");
  14. Assert.notNull(action, "Callback object must not be null");
  15. RedisConnectionFactory factory = getConnectionFactory();
  16. RedisConnection conn = null;
  17. try {
  18. if (enableTransactionSupport) {
  19. // only bind resources in case of potential transaction synchronization
  20. conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);
  21. } else {
  22. conn = RedisConnectionUtils.getConnection(factory);
  23. }
  24. boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);
  25. RedisConnection connToUse = preProcessConnection(conn, existingConnection);
  26. boolean pipelineStatus = connToUse.isPipelined();
  27. if (pipeline && !pipelineStatus) {
  28. connToUse.openPipeline();
  29. }
  30. RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
  31. T result = action.doInRedis(connToExpose);
  32. // close pipeline
  33. if (pipeline && !pipelineStatus) {
  34. connToUse.closePipeline();
  35. }
  36. // TODO: any other connection processing?
  37. return postProcessResult(result, connToUse, existingConnection);
  38. } finally {
  39. if (!enableTransactionSupport) {
  40. RedisConnectionUtils.releaseConnection(conn, factory);
  41. }
  42. }
  43. }
此時已經可以看出每次操作都會創建一個新的 RedisConnection 對象使用完成會調用 RedisConnectionUtils . releaseConnection ( conn , factory ) 方法釋放連接,若想查看其創建 RedisConnection 連接和 RedisConnectionUtils . releaseConnection ( conn , factory ) 釋放連接過程可繼續查看其源碼,此處就不贅述了。

2.JedisConnectionFactory中使用sentinel集群

1.在 spring-context-jedis.xml 中配置sentinel信息
   
   
   
           
  1. <!-- Redis sentinel集群配置 -->
  2. <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
  3. <constructor-arg index="0" type="java.lang.String" value="master001" />
  4. <constructor-arg index="1" type="java.util.Set">
  5. <set>
  6. <value>192.168.110.100:26379</value>
  7. <value>192.168.110.100:36379</value>
  8. <value>192.168.110.100:46379</value>
  9. </set>
  10. </constructor-arg>
  11. </bean>
  12. <!-- Spring提供的Redis連接工廠 -->
  13. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
  14. <!-- Redis sentinel集群配置 -->
  15. <constructor-arg index="0" type="org.springframework.data.redis.connection.RedisSentinelConfiguration" ref="sentinelConfig" />
  16. <!-- 連接池配置. -->
  17. <constructor-arg index="1" type="redis.clients.jedis.JedisPoolConfig" ref="jedisPoolConfig" />
  18. <!-- Redis服務主機. -->
  19. <property name="hostName" value="192.168.110.101" />
  20. <!-- Redis服務端口號. -->
  21. <property name="port" value="6379" />
  22. <!-- Redis服務連接密碼. -->
  23. <!-- <property name="password" value="${redis.password}" /> -->
  24. <!-- 連超時設置. -->
  25. <property name="timeout" value="15000" />
  26. <!-- 是否使用連接池. -->
  27. <property name="usePool" value="true" />
  28. </bean>
2.使用測試代碼
   
   
   
           
  1. public static void main(String[] args)
  2. {
  3. ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context-jedis.xml");
  4. // 獲取Spring提供的RedisTemplate類此類封裝了Jedis,簡化操作
  5. RedisTemplate<String, String> redisTemplate = applicationContext.getBean("jedisTemplate", RedisTemplate.class);
  6. ValueOperations<String, String> value = redisTemplate.opsForValue();
  7. value.set("K001", "V001");
  8. System.out.println(value.get("K001"));
  9. // 關閉Redis Master服務
  10. Scanner scanner = new Scanner(System.in);
  11. String input = scanner.nextLine();
  12. System.out.println(input);
  13. value.set("K002", "V002");
  14. System.out.println(value.get("K002"));
  15. // 關閉Spring容器釋放資源
  16. applicationContext.close();
  17. }
代碼輸出,注意中間的打印:
   
   
   
           
  1. 2015-10-3 15:10:59 redis.clients.jedis.JedisSentinelPool initSentinels
  2. 信息: Trying to find master from available Sentinels...
  3. 2015-10-3 15:10:59 redis.clients.jedis.JedisSentinelPool initSentinels
  4. 信息: Redis master running at 192.168.110.101:6379, starting Sentinel listeners...
  5. 2015-10-3 15:10:59 redis.clients.jedis.JedisSentinelPool initPool
  6. 信息: Created JedisPool to master at 192.168.110.101:6379
  7. V001
  8. 2015-10-3 15:11:38 redis.clients.jedis.JedisSentinelPool initPool
  9. 信息: Created JedisPool to master at 192.168.110.103:6379
  10. V002

3.JedisConnectionFactory中使用JedisShardInfo

Spring-Data-Redis好像並不支持Redis分片集群,但是JedisConnectionFactory源碼中有一個JedisShardInfo屬性,源碼如下:
   
   
   
           
  1. private JedisShardInfo shardInfo;
  2. // 省略......
  3. public void afterPropertiesSet() {
  4. if (shardInfo == null) {
  5. shardInfo = new JedisShardInfo(hostName, port);
  6. if (StringUtils.hasLength(password)) {
  7. shardInfo.setPassword(password);
  8. }
  9. if (timeout > 0) {
  10. setTimeoutOn(shardInfo, timeout);
  11. }
  12. }
  13. if (usePool) {
  14. this.pool = createPool();
  15. }
  16. }
  17. // 省略......
  18. protected Jedis fetchJedisConnector() {
  19. try {
  20. if (usePool && pool != null) {
  21. return pool.getResource();
  22. }
  23. Jedis jedis = new Jedis(getShardInfo());
  24. // force initialization (see Jedis issue #82)
  25. jedis.connect();
  26. return jedis;
  27. } catch (Exception ex) {
  28. throw new RedisConnectionFailureException("Cannot get Jedis connection", ex);
  29. }
  30. }
-------------------------------------------------------------------------------------------------------------------------------





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM