springboot 中 集成druid ,redis
1,导入druid jar包
<!--引入drud--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
2,配置文件
#datasource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.driverClassName=com.mysql.jdbc.Driver spring.datasource.druid.url=jdbc:mysql:///xxxxxx?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull spring.datasource.druid.username=root spring.datasource.druid.password=xxxxx spring.datasource.druid.initialSize=5 spring.datasource.druid.minIdle=5 spring.datasource.druid.maxActive=20 spring.datasource.druid.maxWait=60000 spring.datasource.druid.timeBetweenEvictionRunsMillis=60000 spring.datasource.druid.min-evictableIdleTimeMillis=300000 spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL spring.datasource.druid.testWhileIdle=true spring.datasource.druid.testOnBorrow=false spring.datasource.druid.testOnReturn=false spring.datasource.druid.poolPreparedStatements=true spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.druid.filters=stat,wall ## druid 过滤 spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.web-stat-filter.url-pattern=/* spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* ## druid 监控 spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.login-username=druid spring.datasource.druid.stat-view-servlet.login-password=xxxxxx
3,配置redis
@Value("${redis.hostName}") private String host; @Value("${redis.port}") private Integer port; @Value("${redis.password}") private String password; @Value("${redis.timeout}") private Integer timeout; @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; @Bean public RedisTemplate redisTemplate(){ JedisPoolConfig poolConfig = new JedisPoolConfig(); //最大空闲 poolConfig.setMaxIdle(maxIdle); //最大连接 poolConfig.setMaxTotal(maxTotal); //最大等待时间 poolConfig.setMaxWaitMillis(maxWaitMillis); //创建Jedis 连接工厂 JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig); connectionFactory.setHostName(host); connectionFactory.setPort(port); //调用后初始化方法,没有将抛出异常 connectionFactory.afterPropertiesSet(); //自定义Redis 序列化器 RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(); RedisSerializer stringRedisSerializer = new StringRedisSerializer(); //定义RedisTemplate RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(connectionFactory); //设置序列化器 redisTemplate.setDefaultSerializer(stringRedisSerializer); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jdkSerializationRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer); return redisTemplate; } @Bean(name="redisCacheManager") public CacheManager cacheManager(@Autowired RedisTemplate redisTemplate){ RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); //设置超时时间,10分种 cacheManager.setDefaultExpiration(600); List<String> cacheName = new ArrayList<>(); cacheName.add("redisCacheManger"); cacheManager.setCacheNames(cacheName); return cacheManager; }