zoukankan      html  css  js  c++  java
  • 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”的各种操作也是线程安全的,源码如下:
    获取其 valueOpslistOpssetOpszSetOpshllOps属性:
    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. // 省略部分......
    其属性“ valueOpslistOpssetOpszSetOpshllOps”的各种操作也是线程安全的,例如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. }
    -------------------------------------------------------------------------------------------------------------------------------




  • 相关阅读:
    Cg:访问OpenGL的状态
    C++ Exception Handling
    C语言的调用规约(Calling Convension)之参数传递和返回值
    Why is FBX readonly in animation editor when imported?
    如何在Visual Studio中编译wxWidgets
    ICU字符集编码转换一例
    VisTools
    关于数值分析和LCP问题的一些开源项目
    C++: The Case Against Global Variables
    老男孩筷子兄弟
  • 原文地址:https://www.cnblogs.com/LiZhiW/p/4853486.html
Copyright © 2011-2022 走看看