注:记载springboot集成redis配置、pom、和jedisAPI
reids工具类下载地址:https://download.csdn.net/download/qq_38493490/10434917
1、pom文件加上依赖
<!-- springboot整合 redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
2、application.properties配置文件中加上:具体含义:百度一下这里就不做注解了
######################################################## ###Redis (RedisConfiguration) ######################################################## spring.redis.database=1 spring.redis.host=192.168.7.229 spring.redis.port=6379 spring.redis.password=qhht123456 spring.redis.pool.max-idle=1000 spring.redis.pool.max-active=-1 spring.redis.pool.max-wait=2000 spring.redis.timeout=5000
3、配置启动配置获取jedis
package com; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.cache.interceptor.KeyGenerator; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; /** * Created with IntelliJ IDEA. * Date: 2017/12/25 0025 * Time: 10:37 * To change this template use File | Settings | File Templates. */ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { private Logger logger = Logger.getLogger(getClass()); @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.pool.max-wait}") private long maxWaitMillis; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.database}") private int dataBase; @Bean public JedisPool redisPoolFactory() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password,dataBase); logger.info("JedisPool注入成功!!"); logger.info("redis地址:" + host + ":" + port); return jedisPool; } }
4、参考jedisAPI链接:此处使用的redis工具类上文中已提供下载链接
jedisAPI链接:http://tool.oschina.net/uploads/apidocs/redis/clients/jedis/Jedis.html