spring集成jedis支持redis3.0集群

た 入场券 2022-08-10 00:54 248阅读 0赞

接着上一节,我们通过spring FactoryBean实现redis 3.0集群JedisCluster与spring集成。

引用依赖:

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.7.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-pool2</artifactId>
  9. <version>2.4.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-context</artifactId>
  14. <version>4.1.7.RELEASE</version>
  15. </dependency>

java实现 JedisCluster的FactoryBean。

  1. package cn.slimsmart.jediscluster.spring;
  2. import java.text.ParseException;
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
  6. import org.springframework.beans.factory.FactoryBean;
  7. import org.springframework.beans.factory.InitializingBean;
  8. import redis.clients.jedis.HostAndPort;
  9. import redis.clients.jedis.JedisCluster;
  10. public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {
  11. private GenericObjectPoolConfig genericObjectPoolConfig;
  12. private JedisCluster jedisCluster;
  13. private int connectionTimeout = 2000;
  14. private int soTimeout = 3000;
  15. private int maxRedirections = 5;
  16. private Set<String> jedisClusterNodes;
  17. @Override
  18. public void afterPropertiesSet() throws Exception {
  19. if (jedisClusterNodes == null || jedisClusterNodes.size() == 0) {
  20. throw new NullPointerException("jedisClusterNodes is null.");
  21. }
  22. Set<HostAndPort> haps = new HashSet<HostAndPort>();
  23. for (String node : jedisClusterNodes) {
  24. String[] arr = node.split(":");
  25. if (arr.length != 2) {
  26. throw new ParseException("node address error !",node.length()-1);
  27. }
  28. haps.add(new HostAndPort(arr[0], Integer.valueOf(arr[1])));
  29. }
  30. jedisCluster = new JedisCluster(haps, connectionTimeout, soTimeout, maxRedirections, genericObjectPoolConfig);
  31. }
  32. @Override
  33. public JedisCluster getObject() throws Exception {
  34. return jedisCluster;
  35. }
  36. @Override
  37. public Class<?> getObjectType() {
  38. return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
  39. }
  40. @Override
  41. public boolean isSingleton() {
  42. return true;
  43. }
  44. public GenericObjectPoolConfig getGenericObjectPoolConfig() {
  45. return genericObjectPoolConfig;
  46. }
  47. public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
  48. this.genericObjectPoolConfig = genericObjectPoolConfig;
  49. }
  50. public JedisCluster getJedisCluster() {
  51. return jedisCluster;
  52. }
  53. public void setJedisCluster(JedisCluster jedisCluster) {
  54. this.jedisCluster = jedisCluster;
  55. }
  56. public int getConnectionTimeout() {
  57. return connectionTimeout;
  58. }
  59. public void setConnectionTimeout(int connectionTimeout) {
  60. this.connectionTimeout = connectionTimeout;
  61. }
  62. public int getSoTimeout() {
  63. return soTimeout;
  64. }
  65. public void setSoTimeout(int soTimeout) {
  66. this.soTimeout = soTimeout;
  67. }
  68. public int getMaxRedirections() {
  69. return maxRedirections;
  70. }
  71. public void setMaxRedirections(int maxRedirections) {
  72. this.maxRedirections = maxRedirections;
  73. }
  74. public Set<String> getJedisClusterNodes() {
  75. return jedisClusterNodes;
  76. }
  77. public void setJedisClusterNodes(Set<String> jedisClusterNodes) {
  78. this.jedisClusterNodes = jedisClusterNodes;
  79. }
  80. }

spring配置使用JedisCluster

  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:cache="http://www.springframework.org/schema/cache"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/cache
  8. http://www.springframework.org/schema/cache/spring-cache.xsd"
  9. default-destroy-method="close" default-lazy-init="false">
  10. <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
  11. <property name="maxWaitMillis" value="-1" />
  12. <property name="maxTotal" value="8" />
  13. <property name="minIdle" value="0" />
  14. <property name="maxIdle" value="8" />
  15. </bean>
  16. <bean id="jedisCluster" class="cn.slimsmart.jediscluster.spring.JedisClusterFactory">
  17. <property name="connectionTimeout" value="3000" />
  18. <property name="soTimeout" value="3000" />
  19. <property name="maxRedirections" value="5" />
  20. <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
  21. <property name="jedisClusterNodes">
  22. <set>
  23. <value>192.168.36.54:6380</value>
  24. <value>192.168.36.54:6381</value>
  25. <value>192.168.36.54:6382</value>
  26. <value>192.168.36.54:6383</value>
  27. <value>192.168.36.189:6380</value>
  28. <value>192.168.36.189:6381</value>
  29. <value>192.168.36.189:6382</value>
  30. <value>192.168.36.189:6383</value>
  31. </set>
  32. </property>
  33. </bean>
  34. </beans>

可以在应用中注入JedisCluster处理业务。

发表评论

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

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

相关阅读