package com.lpl.springboot1.config;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
//@EnableAutoConfiguration
@EnableCaching
@PropertySource("classpath:config/redis.properties")
public class RedisConfig extends CachingConfigurerSupport {
@Value("${redis.maxIdle}")
private Integer maxIdle;
@Value("${redis.maxTotal}")
private Integer maxTotal;
@Value("${redis.maxWaitMillis}")
private Integer maxWaitMillis;
@Value("${redis.minEvictableIdleTimeMillis}")
private Integer minEvictableIdleTimeMillis;
@Value("${redis.numTestsPerEvictionRun}")
private Integer numTestsPerEvictionRun;
@Value("${redis.timeBetweenEvictionRunsMillis}")
private long timeBetweenEvictionRunsMillis;
@Value("${redis.testOnBorrow}")
private boolean testOnBorrow;
@Value("${redis.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
@Value("${spring.redis.cluster.max-redirects}")
private Integer mmaxRedirectsac;
/**
* JedisPoolConfig 连接池
* @return
*/
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大空闲数
jedisPoolConfig.setMaxIdle(maxIdle);
// 连接池的最大数据库连接数
jedisPoolConfig.setMaxTotal(maxTotal);
// 最大建立连接等待时间
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
// 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
// 在空闲时检查有效性, 默认false
jedisPoolConfig.setTestWhileIdle(testWhileIdle);
return jedisPoolConfig;
}
/**
* Redis集群的配置
* @return RedisClusterConfiguration
* @autor lpl
* @date 2017年12月22日
* @throws
*/
@Bean
public RedisClusterConfiguration redisClusterConfiguration(){
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
//Set<RedisNode> clusterNodes
String[] serverArray = clusterNodes.split(",");
Set<RedisNode> nodes = new HashSet<RedisNode>();
for(String ipPort:serverArray){
String[] ipAndPort = ipPort.split(":");
nodes.add(new RedisNode(ipAndPort[0].trim(),Integer.valueOf(ipAndPort[1])));
}
redisClusterConfiguration.setClusterNodes(nodes);
redisClusterConfiguration.setMaxRedirects(mmaxRedirectsac);
return redisClusterConfiguration;
}
/**
*
* @Title: JedisConnectionFactory
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param jedisPoolConfig
* @param @return
* @return JedisConnectionFactory
* @autor lpl
* @date 2017年12月22日
* @throws
*/
@Bean
public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig jedisPoolConfig,RedisClusterConfiguration redisClusterConfiguration){
JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration,jedisPoolConfig);
return JedisConnectionFactory;
}
/**
* 实例化 RedisTemplate 对象
*
* @return
*/
@Bean(name="redisTemplate")
public RedisTemplate<String, Object> functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
return redisTemplate;
}
/**
* 设置数据存入 redis 的序列化方式,并开启事务
*
* @param redisTemplate
* @param factory
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
//如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// 开启事务
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setConnectionFactory(factory);
}
/**
* 设置RedisCacheManager
* 使用cache注解管理redis缓存
*
* @return
*/
@Bean
public RedisCacheManager cacheManager(@SuppressWarnings("rawtypes")RedisTemplate redisTemplate) {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
return redisCacheManager;
}
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName()).append(".");
sb.append(method.getName()).append(".");
for (Object obj : objects) {
sb.append(obj.toString());
}
System.out.println("keyGenerator=" + sb.toString());
return sb.toString();
}
};
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SpringBoot+Mybatis+redis-cluster+Swagger2
共51个文件
class:11个
java:10个
properties:6个
5星 · 超过95%的资源 需积分: 10 17 下载量 34 浏览量
2017-12-26
15:06:54
上传
评论
收藏 156KB RAR 举报
温馨提示
项目中SpringBoot整合Mybatis、redis-cluster集群、Swagger2接口说明
资源推荐
资源详情
资源评论
收起资源包目录
springboot-1.rar (51个子文件)
springboot-1
springboot-1
pom.xml 3KB
target
test-classes
com
lpl
springboot1
Springboot1ApplicationTests.class 5KB
mybatis
TestMyBatis.class 1KB
m2e-wtp
web-resources
META-INF
maven
com.lpl
springboot-1
pom.xml 3KB
pom.properties 249B
MANIFEST.MF 339B
classes
config
redis.properties 2KB
application.properties 2KB
com
lpl
springboot1
dao
StudentMapper.class 579B
config
Swagger2.class 2KB
RedisConfig.class 6KB
RedisConfig$1.class 2KB
pojo
Student.class 1KB
controller
HelloController.class 3KB
service
StudentService.class 550B
StudentServiceImpl.class 2KB
Springboot1Application.class 822B
mapping
StudentMapper.xml 1KB
logs
spring-boot-logging.log 2.16MB
riskcontrol-risk-monitoring
log_info.log 16KB
log_error.log 5KB
log_warn.log 488B
mvnw 6KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 155B
org.eclipse.wst.common.component 677B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 572B
org.eclipse.jdt.core.prefs 430B
src
test
java
com
lpl
springboot1
Springboot1ApplicationTests.java 3KB
mybatis
TestMyBatis.java 728B
main
webapp
resources
config
redis.properties 2KB
application.properties 2KB
mapping
StudentMapper.xml 1KB
java
com
lpl
springboot1
dao
StudentMapper.java 516B
config
Swagger2.java 1KB
RedisConfig.java 7KB
Springboot1Application.java 541B
pojo
Student.java 726B
controller
HelloController.java 2KB
service
StudentServiceImpl.java 2KB
StudentService.java 423B
.project 1KB
.classpath 1KB
.gitignore 249B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
共 51 条
- 1
资源评论
- yy60602019-05-16谢谢啊,资源可用性很高w奔跑的蜗牛2019-05-16多谢支持
- Code'Slave2018-01-17谢谢分享好人啊
w奔跑的蜗牛
- 粉丝: 270
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功