首先是redis各个参数的配置,在 application-dev.properties中配置如下:
#redis配置
redis.server.host=192.168.50.162
redis.server.port=6379
redis.server.password=password
redis.server.timeOut=5000
redis.server.maxIdle=50
redis.server.maxWaitMillis=5000
redis.server.maxTotal=500
对 redis 配置参数进行读取和绑定,配置属性注入到 JedisProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = JedisProperties.JEDIS_PREFIX)
public class JedisProperties {
public static final String JEDIS_PREFIX = "redis.server";
private String host;
private int port;
private String password;
private int maxTotal;
private int maxIdle;
private int maxWaitMillis;
private int timeOut;
public int getTimeOut() {
return timeOut;
}
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(int maxTotal) {
this.maxTotal = maxTotal;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMaxWaitMillis() {
return maxWaitMillis;
}
public void setMaxWaitMillis(int maxWaitMillis) {
this.maxWaitMillis = maxWaitMillis;
}
}
配置了 Redis 连接池之后,将 Redis 连接池 注入到 RedisClient 中,并生成 RedisClient Bean
import com.mljr.auth.config.properties.JedisProperties;
import com.mljr.auth.util.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableConfigurationProperties(JedisProperties.class)
@ConditionalOnClass(RedisClient.class)
public class JedisConfig {
private Logger logger = LoggerFactory.getLogger(JedisConfig.class);
@Autowired
private JedisProperties prop;
@Bean(name = "jedisPool")
public JedisPool jedisPool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(prop.getMaxTotal());
config.setMaxIdle(prop.getMaxIdle());
config.setMaxWaitMillis(prop.getMaxWaitMillis());
return new JedisPool(config, prop.getHost(), prop.getPort(), prop.getTimeOut(), prop.getPassword());
}
@Bean
@ConditionalOnMissingBean(RedisClient.class)
public RedisClient redisClient(@Qualifier("jedisPool") JedisPool pool) {
logger.info("初始化……Redis Client==Host={},Port={}", prop.getHost(), prop.getPort());
RedisClient redisClient = new RedisClient();
redisClient.setJedisPool(pool);
return redisClient;
}
}
配置一些常用的 redis 的操作:
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class RedisClient {
private JedisPool jedisPool;
public void set(String key, Object value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
jedis.close();
}
}
/**
* 设置过期时间
* @param key
* @param value
* @param exptime
* @throws Exception
*/
public void setWithExpireTime(String key, String value, int exptime) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value, "NX", "EX", 300);
} catch (Exception e){
e.printStackTrace();
}finally {
jedis.close();
}
}
public String get(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} catch (Exception e){
e.printStackTrace();
}finally {
if (jedis != null)
jedis.close();
}
return null;
}
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
大功告成,使用注解的方式引入就可以使用了
@Autowired
private RedisClient redisClient;
....
....
....