基于springboot 2.1.5.RELEASE,springboot2.X默认redis客户端是lettuce。本文使用的是jedis配置(建议使用lettuce)
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.47version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-pool2artifactId>
<version>2.6.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
dependency>
dependencies>
/**
* 单机版properties
* @author warApe
* @date 2018/5/14 14:25
*/
@Data
public class JedisClientPoolProperties {
private int maxTotal;
private int maxIdle;
private int maxWaitMillis;
}
/**
* @author warApe
* @date 2018/5/14 14:23
*/
@Data
public class JedisClientProperties {
private int port;
private String hostName;
private int connectTimeout;
private int readTimeout;
}
/**
* @program: springboot-share
* @description: bean加载config
* @author: warApe
* @create: 2019-05-31 14:58
**/
@Configuration
public class BeanConfig {
@Bean
@ConfigurationProperties(prefix = "com.warape.redis.standalone.pool")
public JedisClientPoolProperties jedisClientPoolProperties() {
return new JedisClientPoolProperties();
}
@Bean
@ConfigurationProperties(prefix = "com.warape.redis.standalone.client")
public JedisClientProperties jedisClientProperties() {
return new JedisClientProperties();
}
}
/**
* redis单机版配置
*
* @author warApe
* @date 2018/5/11 15:50
* @see SpringbootShareApplication
*/
//@Profile("local")//指定运行环境
@Configuration
public class JedisClientConfig extends RedisAutoConfiguration {
private JedisClientPoolProperties jedisClientPoolProperties;
private JedisClientProperties jedisClientProperties;
@Autowired
public JedisClientConfig(JedisClientPoolProperties jedisClientPoolProperties, JedisClientProperties jedisClientProperties) {
this.jedisClientPoolProperties = jedisClientPoolProperties;
this.jedisClientProperties = jedisClientProperties;
}
/**
* 单节点
*
* @return
*/
@Bean
public RedisConnectionFactory defaultJedisConnectionFactory() {
//如果什么参数都不设置,默认连接本地6379端口
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setHostName(jedisClientProperties.getHostName());
standaloneConfiguration.setPort(jedisClientProperties.getPort());
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
//最大空闲
genericObjectPoolConfig.setMaxIdle(jedisClientPoolProperties.getMaxIdle());
//链接池中最大连接数
genericObjectPoolConfig.setMaxTotal(jedisClientPoolProperties.getMaxTotal());
//最小空闲
genericObjectPoolConfig.setMinIdle(jedisClientPoolProperties.getMaxIdle());
//连接池资源耗尽后 最大等待时间
genericObjectPoolConfig.setMaxWaitMillis(jedisClientPoolProperties.getMaxWaitMillis());
JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder()
.connectTimeout(Duration.ofMillis(jedisClientProperties.getConnectTimeout()))
.readTimeout(Duration.ofMillis(jedisClientProperties.getReadTimeout()))
.usePooling().poolConfig(genericObjectPoolConfig)
.build();
return new JedisConnectionFactory(standaloneConfiguration, jedisClientConfiguration);
}
@Bean
public RedisTemplate<String, Object> defaultRedisTemplate(RedisConnectionFactory defaultJedisConnectionFactory) {
//创建一个模板类
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//将刚才的redis连接工厂设置到模板类中
template.setConnectionFactory(defaultJedisConnectionFactory);
StringRedisSerializer hashKeySerializer = new StringRedisSerializer();
template.setKeySerializer(hashKeySerializer);
template.setHashKeySerializer(hashKeySerializer);
return template;
}
@Bean
public StringRedisTemplate defaultStringRedisTemplate(RedisConnectionFactory defaultJedisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(defaultJedisConnectionFactory);
return stringRedisTemplate;
}
/**
* 序列化不同(对象)
*
* @param defaultRedisTemplate
* @return
*/
@Bean
public RedisUtil defaultRedisUtil(RedisTemplate<String, Object> defaultRedisTemplate) {
return new RedisUtil(defaultRedisTemplate);
}
/**
* (可视的数据)
*
* @param defaultStringRedisTemplate
* @return
*/
@Bean
public StringTemplateRedisUtil defaultStringTemplateRedisUtil(StringRedisTemplate defaultStringRedisTemplate) {
return new StringTemplateRedisUtil(defaultStringRedisTemplate);
}
}
#单机版配置
com:
warape:
redis:
standalone:
client:
host-name: 10.20.29.71
port: 6379
connectTimeout: 2000
readTimeout: 1000
pool:
max-total: 100
max-idle: 10
max-wait-millis: 1000
/**
* Jedis集群配置类
*
* @author warApe
*/
@Data
public class JedisClusterConfigProperties {
private int connectTimeout;
private int readTimeout;
private int maxRedirections;
private List<String> jedisClusterNodes;
}
/**
* @author warApe
* @date 2018/5/14 14:25
*/
@Data
public class JedisClusterPoolProperties {
private int maxTotal;
private int maxIdle;
private int maxWaitMillis;
}
/**
* @program: springboot-share
* @description: bean加载config
* @author: warApe
* @create: 2019-05-31 14:58
**/
@Configuration
public class BeanConfig {
@Bean
@ConfigurationProperties(prefix = "com.warape.redis.pool")
public JedisClusterPoolProperties jedisClusterPoolProperties() {
return new JedisClusterPoolProperties();
}
@Bean
@ConfigurationProperties(prefix = "com.warape.redis.cluster")
public JedisClusterConfigProperties jedisClusterConfigProperties() {
return new JedisClusterConfigProperties();
}
}
package com.warape.springbootshare.integrated.configs;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.warape.springbootshare.SpringbootShareApplication;
import com.warape.springbootshare.integrated.properties.JedisClusterConfigProperties;
import com.warape.springbootshare.integrated.properties.JedisClusterPoolProperties;
import com.warape.springbootshare.integrated.utils.RedisUtil;
import com.warape.springbootshare.integrated.utils.StringTemplateRedisUtil;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
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.StringRedisSerializer;
import java.time.Duration;
/**
* 集群版
*
* @author warApe
* @date 2018/7/29 18:03
* @see SpringbootShareApplication @EnableRedisConfigType
*/
//@Profile({"dev","pro"})
public class JedisClusterConfig {
private JedisClusterPoolProperties jedisClusterPoolProperties;
private JedisClusterConfigProperties JedisClusterConfigProperties;
@Autowired
public JedisClusterConfig(JedisClusterPoolProperties jedisClusterPoolProperties, JedisClusterConfigProperties jedisClusterConfigProperties) {
this.jedisClusterPoolProperties = jedisClusterPoolProperties;
JedisClusterConfigProperties = jedisClusterConfigProperties;
}
@Bean
public RedisConnectionFactory jedisConnectionFactoryCluster1() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(JedisClusterConfigProperties.getJedisClusterNodes());
redisClusterConfiguration.setMaxRedirects(JedisClusterConfigProperties.getMaxRedirections());
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
//最大空闲
genericObjectPoolConfig.setMaxIdle(jedisClusterPoolProperties.getMaxIdle());
//链接池中最大连接数
genericObjectPoolConfig.setMaxTotal(jedisClusterPoolProperties.getMaxTotal());
//最小空闲
genericObjectPoolConfig.setMinIdle(jedisClusterPoolProperties.getMaxIdle());
//连接池资源耗尽后 最大等待时间
genericObjectPoolConfig.setMaxWaitMillis(jedisClusterPoolProperties.getMaxWaitMillis());
JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder()
.connectTimeout(Duration.ofMillis(JedisClusterConfigProperties.getConnectTimeout()))
.readTimeout(Duration.ofMillis(JedisClusterConfigProperties.getReadTimeout()))
.usePooling().poolConfig(genericObjectPoolConfig)
.build();
return new JedisConnectionFactory(redisClusterConfiguration, jedisClientConfiguration);
}
/**
* redisClusterTemplateCluster1
*
* @param jedisConnectionFactoryCluster1 参数名为beanName
* @return
*/
@Bean
public RedisTemplate<String, Object> redisClusterTemplateCluster1(RedisConnectionFactory jedisConnectionFactoryCluster1) {
//创建一个模板类
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//将刚才的redis连接工厂设置到模板类中
template.setConnectionFactory(jedisConnectionFactoryCluster1);
//初始化redis key中的序列化方式 StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
//初始化redis value中的序列化方式 fastjson序列化
GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();
template.setHashValueSerializer(serializer);
template.setValueSerializer(serializer);
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplateCluster1(RedisConnectionFactory jedisConnectionFactoryCluster1) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(jedisConnectionFactoryCluster1);
return stringRedisTemplate;
}
/**
* 序列化不同(对象)
*
* @param redisClusterTemplateCluster1
* @return
*/
@Bean
public RedisUtil redisUtilCluster1(RedisTemplate<String, Object> redisClusterTemplateCluster1) {
return new RedisUtil(redisClusterTemplateCluster1);
}
/**
* (可视的数据)
*
* @param stringRedisTemplateCluster1
* @return
*/
@Bean
public StringTemplateRedisUtil StringRedisUtilCluster1(StringRedisTemplate stringRedisTemplateCluster1) {
return new StringTemplateRedisUtil(stringRedisTemplateCluster1);
}
}
#集群版配置
com:
warape:
redis:
pool:
maxTotal: 100
maxIdle: 10
maxWaitMillis: 3000
cluster:
connectTimeout: 2000
readTimeout: 1000
maxRedirections: 5
jedisClusterNodes: 10.143.131.63:6391,10.143.131.63:6392,10.143.131.63:6393,10.143.131.63:6394,10.143.131.63:6395,10.143.131.63:6396
#如果为多个集群 一次类推
# cluster1:
# connectTimeout:
# readTimeout:
# maxRedirections:
# jedisClusterNodes:
# pool1:
# maxTotal: 100
# maxIdle: 10
# maxWaitMillis: 3000
有2个工具类我没有贴出来,太长了,请小伙伴们去github上去找一下
分别为:StringTemplateRedisUtil 和 RedisUtil
https://github.com/warApePoint/springboot-share