目录
只贴代码
ssm整合redis集群
applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!--配置注解驱动-->
<mvc:annotation-driven />
<!--这是扫描我的组件-->
<context:component-scan base-package="epsilon.ssm.util"/>
<!--集群配置结点-->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<!--如果有密码-->
<property name="password" >
<bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
<constructor-arg index="0" value="chen"/>
</bean>
</property>
<property name="maxRedirects" value="3"/>
<property name="clusterNodes">
<set>
<bean id="node1" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.87.2.18"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean id="node2" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.87.2.18"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean id="node3" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.87.2.18"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean id="node4" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.87.2.18"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean id="node5" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.87.2.18"/>
<constructor-arg name="port" value="7005"/>
</bean>
<bean id="node6" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.87.2.18"/>
<constructor-arg name="port" value="7006"/>
</bean>
</set>
</property>
</bean>
<!--连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大连接数-->
<property name="maxTotal" value="30"/>
<!--最大空闲数-->
<property name="maxIdle" value="10"/>
<!--最小空闲数-->
<property name="minIdle" value="2"/>
<property name="blockWhenExhausted" value="false"/>
<property name="numTestsPerEvictionRun" value="1024"/>
<property name="testOnBorrow" value="true"/>
<property name="softMinEvictableIdleTimeMillis" value="10000"/>
<property name="timeBetweenEvictionRunsMillis" value="30000"/>
<property name="maxWaitMillis" value="1500"/>
<property name="testWhileIdle" value="true"/>
</bean>
<!--连接工厂-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg index="0" ref="redisClusterConfiguration"/>
<constructor-arg index="1" ref="poolConfig"/>
</bean>
<!--模板-->
<!--序列化为字符串-->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!--序列化为字节码-->
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<!--序列化为json-->
<bean id="genericJackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!--设置连接工厂-->
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<!--设置序列化-->
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/>
<property name="hashKeySerializer" ref="genericJackson2JsonRedisSerializer"/>
<property name="hashValueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
</beans>
pom.xml
<!--redis集群依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.10.RELEASE</version>
<!--去除Lettuce-->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Redis 连接工具 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
web.xml
<!-- 其余略过 -->
<context-param>
<!-- needed for ContextLoaderListener -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 其余略过 -->
直接使用模板
//直接在工具类中注入使用模板
@Autowired
@Qualifier("stringRedisTemplate")
private StringRedisTemplate redisTemplate;
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate<String, Object> redisTemplate;
使用springboot整合redis集群
将applicationContext-redis.xml转换为配置类与yml文件结合
package com.example.conf;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableConfigurationProperties(Config.class)
@ConfigurationProperties("spring.redis.cluster") //直接应用配置文件数据
public class Config {
List<String> nodes;
JedisPoolConfig poolConfig;
//配置集群
@Bean
RedisClusterConfiguration redisClusterConfiguration(){
RedisClusterConfiguration configuration=new RedisClusterConfiguration();
List<RedisNode> redisNodes=new ArrayList<>();
for(String node:nodes){
System.out.println(node);
redisNodes.add(new RedisNode((node.split(":"))[0],
Integer.valueOf((node.split(":"))[1])));
}
//configuration.setPassword(RedisPassword.of("rjgc2016"));
configuration.setClusterNodes(redisNodes);
return configuration;
}
//配置连接工厂
@Bean
JedisConnectionFactory jedisConnectionFactory(){
JedisConnectionFactory factory=new JedisConnectionFactory(redisClusterConfiguration(),poolConfig);
return factory;
}
//配置模板
@Bean
RedisTemplate redisTemplate(){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
/**将HashKey HashValue 进行序列化*/
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
StringRedisTemplate stringRedisTemplate(){
StringRedisTemplate stringRedisTemplate=new StringRedisTemplate(jedisConnectionFactory());
stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
return stringRedisTemplate;
}
//-----需要getter和setter
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
public JedisPoolConfig getPoolConfig() {
return poolConfig;
}
public void setPoolConfig(JedisPoolConfig poolConfig) {
this.poolConfig = poolConfig;
}
}
application.yml
#redis集群
spring:
redis:
cluster:
#集群信息
nodes: 112.74.40.181:6080,112.74.40.181:6083,112.74.40.181:6081,203.195.182.48:6082,203.195.182.48:6085,203.195.182.48:6084
#默认值是5 一般当此值设置过大时,容易报:Too many Cluster redirections
maxRedirects: 5
jedis:
#pool我简单的设了几个参数,其余的看applicationContext-redis的配置或百度
pool:
max-idle: 8
min-idle: 0
timeout: 10000
database: 0
password: rjgc2016
application:
name: spring-boot-redis
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!--去除Lettuce-->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引用Jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
直接使用模板
//直接在工具类中注入使用模板
@Autowired
@Qualifier("stringRedisTemplate")
private StringRedisTemplate redisTemplate;
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate<String, Object> redisTemplate;