spring整合redis集群

淡淡的烟草味﹌ 2022-05-22 11:15 335阅读 0赞

1、首先是需要引入的pom.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.redis</groupId>
  5. <artifactId>spring-redis</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <name>spring-redis</name>
  8. <!-- FIXME change it to the project's website --> <url>http://www.example.com</url>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <maven.compiler.source>1.7</maven.compiler.source>
  12. <maven.compiler.target>1.7</maven.compiler.target>
  13. <spring.version>4.3.17.RELEASE</spring.version>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>4.11</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <!-- 日志 --> <dependency>
  23. <groupId>org.slf4j</groupId>
  24. <artifactId>slf4j-api</artifactId>
  25. <version>1.7.25</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.slf4j</groupId>
  29. <artifactId>slf4j-log4j12</artifactId>
  30. <version>1.7.25</version>
  31. </dependency>
  32. <!-- redis --> <dependency>
  33. <groupId>org.springframework.data</groupId>
  34. <artifactId>spring-data-redis</artifactId>
  35. <version>1.7.2.RELEASE</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>redis.clients</groupId>
  39. <artifactId>jedis</artifactId>
  40. <version>2.9.0</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-core</artifactId>
  45. <version>${spring.version}</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-aop</artifactId>
  50. <version>${spring.version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-context</artifactId>
  55. <version>${spring.version}</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework</groupId>
  59. <artifactId>spring-tx</artifactId>
  60. <version>${spring.version}</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework</groupId>
  64. <artifactId>spring-jdbc</artifactId>
  65. <version>${spring.version}</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.springframework</groupId>
  69. <artifactId>spring-beans</artifactId>
  70. <version>${spring.version}</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>org.springframework</groupId>
  74. <artifactId>spring-context-support</artifactId>
  75. <version>${spring.version}</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>com.google.code.gson</groupId>
  79. <artifactId>gson</artifactId>
  80. <version>2.8.5</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>com.alibaba</groupId>
  84. <artifactId>fastjson</artifactId>
  85. <version>1.2.45</version>
  86. </dependency>
  87. <dependency>
  88. <groupId>com.fasterxml.jackson.core</groupId>
  89. <artifactId>jackson-databind</artifactId>
  90. <version>2.8.8</version>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.apache.commons</groupId>
  94. <artifactId>commons-lang3</artifactId>
  95. <version>3.7</version>
  96. </dependency>
  97. </dependencies>
  98. <build>
  99. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins>
  100. <plugin>
  101. <groupId>org.apache.maven.plugins</groupId>
  102. <artifactId>maven-compiler-plugin</artifactId>
  103. <version>3.5.1</version>
  104. <configuration>
  105. <source>1.7</source>
  106. <target>1.7</target>
  107. </configuration>
  108. </plugin>
  109. <plugin>
  110. <artifactId>maven-clean-plugin</artifactId>
  111. <version>3.0.0</version>
  112. </plugin>
  113. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin>
  114. <artifactId>maven-resources-plugin</artifactId>
  115. <version>3.0.2</version>
  116. </plugin>
  117. <plugin>
  118. <artifactId>maven-compiler-plugin</artifactId>
  119. <version>3.7.0</version>
  120. </plugin>
  121. <plugin>
  122. <artifactId>maven-surefire-plugin</artifactId>
  123. <version>2.20.1</version>
  124. </plugin>
  125. <plugin>
  126. <artifactId>maven-jar-plugin</artifactId>
  127. <version>3.0.2</version>
  128. </plugin>
  129. <plugin>
  130. <artifactId>maven-install-plugin</artifactId>
  131. <version>2.5.2</version>
  132. </plugin>
  133. <plugin>
  134. <artifactId>maven-deploy-plugin</artifactId>
  135. <version>2.8.2</version>
  136. </plugin>
  137. </plugins>
  138. </pluginManagement>
  139. </build>
  140. </project>
  141. 2、redis集群参数配置:redis-cluster.properties
  142. redis.host1=127.0.0.1 redis.port1=6379 redis.host2=127.0.0.1 redis.port2=6380 redis.host3=127.0.0.1 redis.port3=6381 redis.host4=127.0.0.1 redis.port4=6382 redis.host5=127.0.0.1 redis.port5=6383 redis.host6=127.0.0.1 redis.port6=6384 redis.expiration=3000 #最大空闲数 redis.maxIdle=300 #连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal redis.maxActive=600 #最大建立连接等待时间 redis.maxWait=1000 #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 redis.testOnBorrow=true #客户端超时时间单位是毫秒 默认是2000 redis.timeout=10000 #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性 redis.maxTotal=1000 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 redis.maxWaitMillis=1000 #连接的最小空闲时间 默认1800000毫秒(30分钟) redis.minEvictableIdleTimeMillis=300000 #每次释放连接的最大数目,默认3 redis.numTestsPerEvictionRun=1024 #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1 redis.timeBetweenEvictionRunsMillis=30000 #在空闲时检查有效性, 默认false

redis.testWhileIdle=true

3、redis集群配置:redis-cluster.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  3. <description>redis集群</description>
  4. <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 --> <context:component-scan base-package="com.custom">
  5. <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
  6. </context:component-scan>
  7. <!-- 加载properties文件 --> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  8. <property name="locations">
  9. <list>
  10. <value>classpath*:/redis-cluster.properties</value>
  11. </list>
  12. </property>
  13. </bean>
  14. <!-- 配置JedisPoolConfig实例 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  15. <!--最大空闲数--> <property name="maxIdle" value="${redis.maxIdle}" />
  16. <!--连接池的最大数据库连接数 --> <property name="maxTotal" value="${redis.maxActive}" />
  17. <!--最大建立连接等待时间--> <property name="maxWaitMillis" value="${redis.maxWait}" />
  18. <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--> <property name="testOnBorrow" value="${redis.testOnBorrow}" />
  19. <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)--> <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
  20. <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3--> <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
  21. <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1--> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
  22. </bean>
  23. <!-- Redis集群配置 --> <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
  24. <property name="maxRedirects" value="6"></property>
  25. <property name="clusterNodes">
  26. <set>
  27. <bean class="org.springframework.data.redis.connection.RedisNode">
  28. <constructor-arg name="host" value="${redis.host1}"></constructor-arg>
  29. <constructor-arg name="port" value="${redis.port1}"></constructor-arg>
  30. </bean>
  31. <bean class="org.springframework.data.redis.connection.RedisNode">
  32. <constructor-arg name="host" value="${redis.host2}"></constructor-arg>
  33. <constructor-arg name="port" value="${redis.port2}"></constructor-arg>
  34. </bean>
  35. <bean class="org.springframework.data.redis.connection.RedisNode">
  36. <constructor-arg name="host" value="${redis.host3}"></constructor-arg>
  37. <constructor-arg name="port" value="${redis.port3}"></constructor-arg>
  38. </bean>
  39. <bean class="org.springframework.data.redis.connection.RedisNode">
  40. <constructor-arg name="host" value="${redis.host4}"></constructor-arg>
  41. <constructor-arg name="port" value="${redis.port4}"></constructor-arg>
  42. </bean>
  43. <bean class="org.springframework.data.redis.connection.RedisNode">
  44. <constructor-arg name="host" value="${redis.host5}"></constructor-arg>
  45. <constructor-arg name="port" value="${redis.port5}"></constructor-arg>
  46. </bean>
  47. <bean class="org.springframework.data.redis.connection.RedisNode">
  48. <constructor-arg name="host" value="${redis.host6}"></constructor-arg>
  49. <constructor-arg name="port" value="${redis.port6}"></constructor-arg>
  50. </bean>
  51. </set>
  52. </property>
  53. </bean>
  54. <!-- 配置JedisConnectionFactory --> <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  55. <constructor-arg name="clusterConfig" ref="redisClusterConfig" />
  56. <property name="poolConfig" ref="poolConfig"/>
  57. <property name="timeout" value="${redis.timeout}" />
  58. <!--<property name="password" value="123456"></property>--> </bean>
  59. <!-- 配置RedisTemplate --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  60. <property name="connectionFactory" ref="jeidsConnectionFactory" />
  61. <property name="keySerializer" >
  62. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  63. </property>
  64. <property name="valueSerializer" >
  65. <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
  66. </property>
  67. <property name="hashKeySerializer">
  68. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  69. </property>
  70. <property name="hashValueSerializer">
  71. <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
  72. </property>
  73. </bean>
  74. </beans>

4、将redis集群配置文件加入到applicationContext中:applicationContext-basic.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  3. <description>Spring公共配置 </description>
  4. <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 --> <context:component-scan base-package="com.custom">
  5. <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
  6. </context:component-scan>
  7. <!-- redis集群配置 --> <import resource="classpath*:redis-cluster.xml" />
  8. </beans>

发表评论

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

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

相关阅读

    相关 Redisspring整合

    上一篇详细的赘述了Redis的curd操作及集群的搭建。下面我们开始将他整合到我们实际的项目中去。我的项目采用的是标准的ssm框架,ssm框架这里不说,直接开始整合。